Class: Core::Async::Channel
- Inherits:
-
Object
- Object
- Core::Async::Channel
- Includes:
- Is::Inspectable
- Defined in:
- lib/core/async/channel.rb
Overview
- public
-
Channels let fibers wait on future published values.
Instance Method Summary collapse
-
#initialize ⇒ Channel
constructor
A new instance of Channel.
-
#publish(value = nil) ⇒ Object
- public
-
Publish the given value to all subscribers.
-
#subscribe ⇒ Object
- public
-
Subscribe the current fiber, blocking until a value is published.
Constructor Details
#initialize ⇒ Channel
Returns a new instance of Channel.
13 14 15 |
# File 'lib/core/async/channel.rb', line 13 def initialize @subscribers = [] end |
Instance Method Details
#publish(value = nil) ⇒ Object
- public
-
Publish the given value to all subscribers.
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/core/async/channel.rb', line 26 def publish(value = nil) subscribers = @subscribers @subscribers = [] while (fiber = subscribers.shift) if fiber.alive? Fiber.scheduler.resume(fiber, value) end end end |
#subscribe ⇒ Object
- public
-
Subscribe the current fiber, blocking until a value is published.
19 20 21 22 |
# File 'lib/core/async/channel.rb', line 19 def subscribe @subscribers << Fiber.current Fiber.scheduler.transfer end |