Class: LucidComponent::AppStoreProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/lucid_component/app_store_proxy.rb

Instance Method Summary collapse

Constructor Details

#initialize(component_instance) ⇒ AppStoreProxy

Returns a new instance of AppStoreProxy.



3
4
5
6
# File 'lib/lucid_component/app_store_proxy.rb', line 3

def initialize(component_instance)
  @native = component_instance.to_n
  @component_instance = component_instance
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(key, *args, &block) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/lucid_component/app_store_proxy.rb', line 8

def method_missing(key, *args, &block)
  if `args.length > 0`
    # set class state, simply a dispatch
    action = { type: 'APPLICATION_STATE', name: (`key.endsWith('=')` ? key.chop : key), value: args[0] }
    Isomorfeus.store.dispatch(action)
  else
    # check if we have a component local state value
    if `#@native.props.store`
      if `#@native.props.store.application_state && #@native.props.store.application_state.hasOwnProperty(key)`
        return @native.JS[:props].JS[:store].JS[:application_state].JS[key]
      end
    else
      a_state = Isomorfeus.store.get_state
      if a_state.key?(:application_state) && a_state[:application_state].key?(key)
        return a_state[:application_state][key]
      end
    end
    if @component_instance.class.default_app_store_defined && @component_instance.class.app_store.to_h.key?(key)
      # check if a default value was given
      return @component_instance.class.app_store.to_h[key]
    end
    # otherwise return nil
    return nil
  end
end

Instance Method Details

#dispatch(action) ⇒ Object



34
35
36
# File 'lib/lucid_component/app_store_proxy.rb', line 34

def dispatch(action)
  Isomorfeus.store.dispatch(action)
end

#subscribe(&block) ⇒ Object



38
39
40
# File 'lib/lucid_component/app_store_proxy.rb', line 38

def subscribe(&block)
  Isomorfeus.store.subscribe(&block)
end

#unsubscribe(unsubscriber) ⇒ Object



42
43
44
# File 'lib/lucid_component/app_store_proxy.rb', line 42

def unsubscribe(unsubscriber)
  `unsubscriber()`
end