module FiberHook::ClassMethods

Class methods that will be added to Fiber.

Public Instance Methods

hook(**options) click to toggle source

@see FiberHook.add

# File lib/fiber_hook.rb, line 63
def hook(**options)
  FiberHook.add(**options)
end
hook?(hook_id) click to toggle source

@see FiberHook.has?

# File lib/fiber_hook.rb, line 73
def hook?(hook_id)
  FiberHook.has?(hook_id)
end
new(*args, &block) click to toggle source
Calls superclass method
# File lib/fiber_hook.rb, line 47
def new(*args, &block)
  # In Fiber.new, call the :new methods of all the hooks. Save the results.
  values = FiberHook.hooks.transform_values { |hook| hook[:new]&.call }

  fiber_proc = proc do |*block_args|
    # In Fiber.resume, call the :resume methods of all the hooks.
    # Pass in the values returned by the :new methods.
    FiberHook.hooks.each { |id, hook| hook[:resume]&.call(values[id]) }
    # Then call the original fiber block.
    block.call(*block_args)
  end

  super(*args, &fiber_proc)
end
unhook(hook_id) click to toggle source

@see FiberHook.remove

# File lib/fiber_hook.rb, line 68
def unhook(hook_id)
  FiberHook.remove(hook_id)
end