class Effects

Public Class Methods

effection(message, *args) click to toggle source
# File lib/effection/effects.rb, line 29
def self.effection(message, *args)
  Fiber.yield -> effector {
    effector.__handle(message, *args)
  }
end
listen(message, &handler) click to toggle source
# File lib/effection/effects.rb, line 23
def self.listen(message, &handler)
  Fiber.yield -> effector {
    effector.__add_handler(message, &handler)
  }
end
new(initial_handlers = {}, parent = nil, &block) click to toggle source
# File lib/effection/effects.rb, line 4
def initialize(initial_handlers = {}, parent = nil, &block)
  @handlers = initial_handlers
  @fib = Fiber.new(&block)
  @parent = parent
  __next()
end
with(**initial_handlers, &block) click to toggle source
# File lib/effection/effects.rb, line 11
def self.with(**initial_handlers, &block)
  parent = nil
  begin
    Fiber.yield -> effector {
      parent = effector
    }
  rescue FiberError
    # Ignore it if it's Can't yield from Root fiber, for now? I guess
  end
  Effects.new(initial_handlers, parent, &block)
end

Public Instance Methods

__add_handler(message, &handler) click to toggle source
# File lib/effection/effects.rb, line 53
def __add_handler(message, &handler)
  @handlers[message] = handler
end
__handle(message, *args) click to toggle source
# File lib/effection/effects.rb, line 57
def __handle(message, *args)
  if @handlers[message]
    res_with = @handlers[message].(*args)
  elsif @parent != nil
    res_with = @parent.effection(message, *args)
  end
end
__next(*newVal) click to toggle source
# File lib/effection/effects.rb, line 41
def __next(*newVal)
  if @fib.alive?
    # This state is paused here and then will only be hit when the containing fiber calls Yield.
    cb = @fib.resume(*newVal)
    ret_val = cb
    if cb != nil and cb.respond_to? :call
      ret_val = cb.(self) 
    end
    __next(ret_val)
  end
end
effection(message, *args) click to toggle source
# File lib/effection/effects.rb, line 35
def effection(message, *args)
  Fiber.yield -> effector {
    effector.__handle(message, *args)
  }
end