Class: Core::Async::Channel

Inherits:
Object
  • Object
show all
Includes:
Is::Inspectable
Defined in:
lib/core/async/channel.rb

Overview

public

Channels let fibers wait on future published values.

Instance Method Summary collapse

Constructor Details

#initializeChannel

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

#subscribeObject

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