Class: Fiber

Inherits:
Object
  • Object
show all
Defined in:
lib/fibre/core_ext/fiber.rb

Overview

Fiber.sync do |fiber|

  fiber.resume response
  fiber.leave StandardError, "Something"
end

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.scope(*a, &b) ⇒ Object



28
29
30
# File 'lib/fibre/core_ext/fiber.rb', line 28

def scope(*a, &b)
  Fibre::Scope.scope(*a, &b)
end

.sync(*a, &b) ⇒ Object



32
33
34
# File 'lib/fibre/core_ext/fiber.rb', line 32

def sync(*a, &b)
  Fibre::Scope.scope? ? Fibre::Scope.sync(*a, &b) : wait(*a, &b)
end

.wait {|Fiber.current| ... } ⇒ Object

Yields:



43
44
45
46
# File 'lib/fibre/core_ext/fiber.rb', line 43

def wait
  yield(Fiber.current) if block_given?
  Fiber.yield!
end

.yield!Object

raise exception if we catch exception



37
38
39
40
41
# File 'lib/fibre/core_ext/fiber.rb', line 37

def yield!
  Fiber.yield.tap do |y|
    raise y if y.is_a?(Exception)
  end
end

Instance Method Details

#[](key) ⇒ Object



18
19
20
# File 'lib/fibre/core_ext/fiber.rb', line 18

def [](key)
  attributes[key]
end

#[]=(key, value) ⇒ Object



22
23
24
# File 'lib/fibre/core_ext/fiber.rb', line 22

def []=(key,value)
  attributes[key] = value
end

#attributesObject



10
11
12
# File 'lib/fibre/core_ext/fiber.rb', line 10

def attributes
  @attributes ||= {}
end

#leave(exception, message = nil) ⇒ Object



49
50
51
# File 'lib/fibre/core_ext/fiber.rb', line 49

def leave(exception, message=nil)
  resume exception.is_a?(Class) ? exception.new(message) : exception
end

#root?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/fibre/core_ext/fiber.rb', line 14

def root?
  self.eql? Fibre.root
end