Class: Farce::Local::Lazy

Inherits:
Abstract::Lazy show all
Includes:
Scoped, Shareable::Unfreezable
Defined in:
lib/farce/local/lazy.rb

Overview

A shareable lazy value computed once per scope. Results may be mutable.

Instance Attribute Summary

Attributes included from Scoped

#scope

Instance Method Summary collapse

Methods included from Shareable

#ractor_shareable?

Methods inherited from Abstract::Lazy

#value

Methods included from Abstract::Value

#blank?, #unwrap, #value

Methods included from Internal::Copyable

#duplicable?

Constructor Details

#initialize(factory, scope: :ractor) ⇒ Lazy #initialize(scope: :ractor, self: nil) { ... } ⇒ Lazy

Valid scopes are:

  • :ractor - The value is shared by all threads within the same Ractor.
  • :thread_group - The value is shared by all threads within the same thread group.
  • :thread - The value is shared by all fibers within the same thread.
  • :fiber_storage - The value is shared by all fibers using the same storage. Unless explicitly specified, storage is inherited from the parent fiber for blocking fibers, but not for non-blocking fibers (such as those created by Scheduler#schedule).
  • :fiber - Each fiber has its own independent value.

Overloads:

  • #initialize(factory, scope: :ractor) ⇒ Lazy

    Parameters:

    • factory (Class, Proc, #call) —

      a shareable factory for each scope's value

    • scope (Symbol) (defaults to: :ractor) —

      the scope of the lazy value

  • #initialize(scope: :ractor, self: nil) { ... } ⇒ Lazy

    Parameters:

    • scope (Symbol) (defaults to: :ractor) —

      the scope of the lazy value

    • self (BasicObject) (defaults to: nil) —

      the shareable receiver to bind to the block

    Yields:

    • computes the value on first access in each scope

    Yield Returns:

    • (BasicObject) —

      the initial value for the scope, which may be mutable

    Yield Receiver:

    • (BasicObject) —

      the self parameter, or nil if omitted



23
24
25
26
27
# File 'lib/farce/local/lazy.rb', line 23

def initialize(factory = nil, scope: :ractor, **, &)
  @factory   = prepare_factory(factory, **, &)
  @state_key = Object.new.freeze
  super(scope:)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Farce::Abstract::Lazy