module FiberHook

Allows you to hook fiber creation so you can call a method from the parent fiber immediately before any child fiber is created, return a value, and then call another method from inside the child fiber the first time the fiber is resumed, passing in the value that was returned from the first method.

Constants

VERSION

Attributes

hooks[R]

Public Class Methods

add(new: nil, resume: nil) click to toggle source

Add a hook and return its id. @param new [Proc] Method to be called in parent fiber context when Fiber.new is called.

Takes no params. Its return value will be passed into +resume+.

@param resume [Proc] Method to be called in child fiber’s context when +Fiber#resume+

is called for the first time. Takes a single param: the value returned by +new+.

@return [Integer] The id of the newly-created hook. Can be passed in to has? or

+remove+.
# File lib/fiber_hook.rb, line 28
def self.add(new: nil, resume: nil)
  @prev_id += 1
  @hooks[@prev_id] = { new: new, resume: resume }
  @prev_id
end
has?(hook_id) click to toggle source

Is this hook id valid?

# File lib/fiber_hook.rb, line 35
def self.has?(hook_id)
  @hooks.key?(hook_id)
end
remove(hook_id) click to toggle source

Remove a hook by its id. Afterward, newly-created fibers won’t have this hook.

# File lib/fiber_hook.rb, line 40
def self.remove(hook_id)
  value = @hooks.delete(hook_id)
  raise Error, "Hook #{hook_id} not found" unless value
end