Module: Hibiki::Reactive

Defined in:
lib/hibiki/reactive.rb

Overview

---- class-level reactivity ------------------------------------------------- Svelte 5 allows $state/$derived/$effect as class fields; Reactive is the Ruby analogue — declare signals with class macros, use them as plain attributes:

class Counter
include Hibiki::Reactive
state :count, 0
derived(:doubled) { count * 2 }
effect { puts "count is now #{count}" } # starts on initialize

def increment = self.count += 1
end

Readers and writers are ordinary methods over per-instance signals, so usage sites never touch .value and dependency tracking flows through plain method calls. Signals are created lazily on first touch (read or write): no initialize hook is needed for state/derived, and subclasses inherit declarations because the generated methods inherit.

Defined Under Namespace

Modules: ClassMethods, Initializer

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



24
25
26
27
# File 'lib/hibiki/reactive.rb', line 24

def self.included(base)
  base.extend(ClassMethods)
  base.prepend(Initializer)
end

Instance Method Details

#disposeObject

Dispose every effect this instance started. Instances created inside a running effect don't need this — Effect.new adopts them into the owner tree and the owner's rerun/dispose takes them down. Explicitly needed only for long-lived instances whose effects read signals outside the instance (effects reading only the instance's own signals form a self-contained island that garbage-collects with it).



76
# File 'lib/hibiki/reactive.rb', line 76

def dispose = @__hibiki_effects.each(&:dispose)