Class: Plumb::Deferred

Inherits:
Object
  • Object
show all
Includes:
Composable
Defined in:
lib/plumb/deferred.rb

Instance Method Summary collapse

Methods included from Composable

#&, #/, #>>, #[], #absorb_input, #absorb_output, #accepted_type, #as_node, #build, #check, #children, #defer, #fusable_step?, #fuse_with, #generate, #idempotent?, included, #input_type, #invalid, #invoke, #match, #metadata, #not, #output_type, #pipeline, #policy, resolve_operand, #static, #subtype_identity, #to_json_schema, #to_mermaid, #to_plumb_type, #to_s, #transform, #value, #value_preserving?, #where, #with, wrap, #|

Methods included from Callable

#parse, #resolve

Constructor Details

#initialize(definition) ⇒ Deferred

Returns a new instance of Deferred.



9
10
11
12
13
14
# File 'lib/plumb/deferred.rb', line 9

def initialize(definition)
  @lock = Mutex.new
  @definition = definition
  @cached_type = nil
  # freeze
end

Instance Method Details

#==(other) ⇒ Boolean

Deferred nodes use identity equality to avoid materializing recursive types.

Parameters:

  • other (Object)

Returns:

  • (Boolean)


19
# File 'lib/plumb/deferred.rb', line 19

def ==(other) = equal?(other)

#call(result) ⇒ Object



21
22
23
# File 'lib/plumb/deferred.rb', line 21

def call(result)
  type.call(result)
end

#typeObject



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/plumb/deferred.rb', line 25

def type
  @lock.synchronize do
    @cached_type ||= @definition.call
    # Release the definition closure: it can capture large scopes (eg. a
    # codec rewriter and the type graph it walked) that would otherwise
    # stay reachable for the life of this node.
    @definition = nil
    self.define_singleton_method(:type) do
      @cached_type
    end
    @cached_type
  end
end