Module: Hibiki::Reactive::ClassMethods
- Defined in:
- lib/hibiki/reactive.rb
Instance Method Summary collapse
- #derived(name) ⇒ Object
-
#effect(&block) ⇒ Object
Anonymous, so unlike state/derived it can't ride on method inheritance: blocks are collected per class and gathered up the ancestor chain at initialize.
- #hibiki_effect_blocks ⇒ Object
-
#state(name, default = nil, &default_block) ⇒ Object
state :count, 0 state(:items) { [] } A positional default is shared across instances (it's one object) — use the block form for mutable defaults.
Instance Method Details
#derived(name) ⇒ Object
45 46 47 48 |
# File 'lib/hibiki/reactive.rb', line 45 def derived(name, &) init = proc { Derived.new { instance_exec(&) } } define_method(name) { __hibiki_signal(name, init).value } end |
#effect(&block) ⇒ Object
Anonymous, so unlike state/derived it can't ride on method inheritance: blocks are collected per class and gathered up the ancestor chain at initialize.
53 |
# File 'lib/hibiki/reactive.rb', line 53 def effect(&block) = hibiki_effect_blocks << block |
#hibiki_effect_blocks ⇒ Object
55 |
# File 'lib/hibiki/reactive.rb', line 55 def hibiki_effect_blocks = (@hibiki_effect_blocks ||= []) |
#state(name, default = nil, &default_block) ⇒ Object
state :count, 0 state(:items) { [] } A positional default is shared across instances (it's one object) — use the block form for mutable defaults. The block is evaluated per instance, instance_exec'd, and untracked: first touch may happen inside some effect's tracking window, and a default that reads other signals must not subscribe that outer observer.
37 38 39 40 41 42 43 |
# File 'lib/hibiki/reactive.rb', line 37 def state(name, default = nil, &default_block) init = proc do State.new(default_block ? Hibiki.untrack { instance_exec(&default_block) } : default) end define_method(name) { __hibiki_signal(name, init).value } define_method(:"#{name}=") { |new_value| __hibiki_signal(name, init).value = new_value } end |