Class: Hibiki::State

Inherits:
Object
  • Object
show all
Includes:
Trackable
Defined in:
lib/hibiki/state.rb

Overview

---- writable signal --------------------------------------------------------

Instance Method Summary collapse

Methods included from Trackable

#changed_from?, #notify, #register_dependency, #subscribers, #unsubscribe

Constructor Details

#initialize(value, equals: nil) ⇒ State

equals: per-signal equality (Solid's createSignal(value, { equals })). nil → ==; false → always notify; callable → comparator(prev, next).



10
11
12
13
# File 'lib/hibiki/state.rb', line 10

def initialize(value, equals: nil)
  @value = value
  @equals = equals
end

Instance Method Details

#callObject

Solid signals are getter functions; sig.() reads (and registers).



25
# File 'lib/hibiki/state.rb', line 25

def call = value

#inspectObject



49
# File 'lib/hibiki/state.rb', line 49

def inspect = "#<Hibiki::State #{@value.inspect}>"

#peekObject

Read without subscribing (per-signal untrack), for read-modify-write effects that must not depend on what they write.



22
# File 'lib/hibiki/state.rb', line 22

def peek = @value

#to_sObject



48
# File 'lib/hibiki/state.rb', line 48

def to_s = value.to_s

#updateObject

sugar for in-place updates: counter.update { it + 1 }



46
# File 'lib/hibiki/state.rb', line 46

def update = self.value = yield(@value)

#valueObject



15
16
17
18
# File 'lib/hibiki/state.rb', line 15

def value
  register_dependency
  @value
end

#value=(new_value) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/hibiki/state.rb', line 27

def value=(new_value)
  # The write gate: the signal's equality decides whether this write is a
  # no-op. Its twin is the flush gate, Trackable#changed_from? — keep them
  # answering the same way.
  case @equals
  when nil then return if new_value == @value
  when false then nil # always notify
  else return if @equals.call(@value, new_value)
  end

  @value = new_value
  # Solid wraps every write in runUpdates; mirroring that, an unbatched
  # write becomes an implicit batch of one. The whole invalidation wave
  # (all diamond branches) lands before effects flush, so an effect runs
  # once per write and never sees a half-updated graph.
  Hibiki.batch { notify }
end