Class: Tailmix::Runtime::State

Inherits:
Object
  • Object
show all
Defined in:
lib/tailmix/runtime/state.rb

Instance Method Summary collapse

Constructor Details

#initialize(state_definition, initial_values, cache:) ⇒ State

Returns a new instance of State.



6
7
8
9
10
# File 'lib/tailmix/runtime/state.rb', line 6

def initialize(state_definition, initial_values, cache:)
  @definition = state_definition
  @cache = cache
  @data = initialize_data(initial_values)
end

Instance Method Details

#[](key) ⇒ Object



12
13
14
# File 'lib/tailmix/runtime/state.rb', line 12

def [](key)
  @data[key.to_sym]
end

#[]=(key, value) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/tailmix/runtime/state.rb', line 16

def []=(key, value)
  return if @data[key.to_sym] == value

  @data[key.to_sym] = value
  # Main reactive trigger: when the state changes – we clear the cache!
  @cache.clear!
end

#to_hObject



24
25
26
# File 'lib/tailmix/runtime/state.rb', line 24

def to_h
  @data
end