Sha256: 6bdbacc65168a37e169d513de344da65326b8aa3fa2014168c45698409d77bd5

Contents?: true

Size: 1.73 KB

Versions: 15

Compression:

Stored size: 1.73 KB

Contents

module LucidComponent
  class InstanceStoreProxy
    def initialize(component_instance, access_key = 'state')
      @native_component_instance = component_instance.to_n
      @component_instance = component_instance
      @component_object_id = component_instance.object_id.to_s
      @access_key = access_key
    end

    def method_missing(key, *args, &block)
      @native_component_instance.JS.register_used_store_path(['component_state', @component_object_id, key])
      if `args.length > 0`
        # set instance state, simply a dispatch

        action = { type: 'COMPONENT_STATE', object_id: @component_object_id, name: (`key.endsWith('=')` ? key.chop : key), value: args[0] }
        Isomorfeus.store.dispatch(action)

      else
        # get instance state

        if @native_component_instance.JS[@access_key].JS[:isomorfeus_store].JS[:component_state].JS[@component_object_id] &&
          @native_component_instance.JS[@access_key].JS[:isomorfeus_store].JS[:component_state].JS[@component_object_id].JS.hasOwnProperty(key)
          # check if we have a component local state value
          return @native_component_instance.JS[@access_key].JS[:isomorfeus_store].JS[:component_state].JS[@component_object_id].JS[key]
        elsif @component_instance.class.default_instance_store_defined && @component_instance.class.store.to_h.key?(key)
          # check if a default value was given
          return @component_instance.class.store.to_h[key]
        end

        # otherwise return nil
        return nil
      end
    end

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

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

    def unsubscribe(unsubscriber)
      `unsubscriber()`
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
isomorfeus-react-16.9.14 lib/lucid_component/instance_store_proxy.rb
isomorfeus-react-16.9.13 lib/lucid_component/instance_store_proxy.rb
isomorfeus-react-16.9.12 lib/lucid_component/instance_store_proxy.rb
isomorfeus-react-16.9.11 lib/lucid_component/instance_store_proxy.rb
isomorfeus-react-16.9.10 lib/lucid_component/instance_store_proxy.rb
isomorfeus-react-16.9.9 lib/lucid_component/instance_store_proxy.rb
isomorfeus-react-16.9.8 lib/lucid_component/instance_store_proxy.rb
isomorfeus-react-16.9.7 lib/lucid_component/instance_store_proxy.rb
isomorfeus-react-16.9.6 lib/lucid_component/instance_store_proxy.rb
isomorfeus-react-16.9.5 lib/lucid_component/instance_store_proxy.rb
isomorfeus-react-16.9.4 lib/lucid_component/instance_store_proxy.rb
isomorfeus-react-16.9.3 lib/lucid_component/instance_store_proxy.rb
isomorfeus-react-16.9.2 lib/lucid_component/instance_store_proxy.rb
isomorfeus-react-16.9.1 lib/lucid_component/instance_store_proxy.rb
isomorfeus-react-16.9.0 lib/lucid_component/instance_store_proxy.rb