Class: Farce::Unshared::Lazy

Inherits:
Abstract::Lazy show all
Includes:
Farce::Unshareable
Defined in:
lib/farce/unshared/lazy.rb,
lib/farce/unshared/lazy.rb

Overview

A thread-safe lazy value within one Ractor. Its factory may capture mutable state. The result retains its identity. Factory failures can be retried on the next access.

Examples:

Build a mutable cache on first access

lazy = Farce::Unshared::Lazy.new { {} }
lazy.value[:job] = :ready

Instance Method Summary collapse

Methods included from Farce::Unshareable

#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) ⇒ Lazy #initialize(self: nil) { ... } ⇒ Lazy

Without self:, the block keeps its original receiver.

Overloads:

  • #initialize(factory) ⇒ Lazy

    Parameters:

    • factory (Class, Proc, #call) —

      the factory, which may retain mutable state

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

    Parameters:

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

      the receiver to bind to the block

    Yields:

    • computes the value on first access, preserving captured state

    Yield Returns:

    • (BasicObject) —

      the cached result



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/farce/unshared/lazy.rb', line 26

class Lazy < Abstract::Lazy
  include Unshareable

  private

  def new_internal_atom = Internal::UnsharedAtom.new

  def prepare_proc(factory, **options)
    options.empty? ? factory : Farce.rebind(factory, **options)
  end
end

Dynamic Method Handling

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