Sha256: 7a897ecdeaa7b22c89dd273e0f8aff4cfd7b960406a3f129ed5ceca79a225d24

Contents?: true

Size: 1.47 KB

Versions: 12

Compression:

Stored size: 1.47 KB

Contents

module LucidComponent
  class InstanceStoreProxy
    def initialize(component_instance)
      @native = component_instance.to_n
      @component_instance = component_instance
      @component_object_id = component_instance.object_id.to_s
    end

    def [](key)
      method_missing(key)
    end

    def []=(key, value)
      method_missing(key, value)
    end

    def method_missing(key, *args, &block)
      if `args.length > 0`
        # set instance state, simply a dispatch

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

      else
        # get instance state
        if @native.JS[:props].JS[:store]
          if @native.JS[:props].JS[:store].JS[:instance_state] &&
              @native.JS[:props].JS[:store].JS[:instance_state].JS[@component_object_id] &&
              @native.JS[:props].JS[:store].JS[:instance_state].JS[@component_object_id].JS.hasOwnProperty(key)
            # check if we have a component local state value
            return @native.JS[:props].JS[:store].JS[:instance_state].JS[@component_object_id].JS[key]
          end
        else
          a_state = Isomorfeus.store.get_state
          if a_state.key?(:instance_state) && a_state[:instance_state].key?(key)
            return a_state[:instance_state][key]
          end
        end
        # otherwise return nil
        return nil
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
isomorfeus-react-16.13.11 lib/isomorfeus_react/lucid_component/instance_store_proxy.rb
isomorfeus-react-16.13.10 lib/isomorfeus_react/lucid_component/instance_store_proxy.rb
isomorfeus-react-16.13.9 lib/isomorfeus_react/lucid_component/instance_store_proxy.rb
isomorfeus-react-16.13.8 lib/isomorfeus_react/lucid_component/instance_store_proxy.rb
isomorfeus-react-16.13.7 lib/isomorfeus_react/lucid_component/instance_store_proxy.rb
isomorfeus-react-16.13.6 lib/isomorfeus_react/lucid_component/instance_store_proxy.rb
isomorfeus-react-16.13.5 lib/isomorfeus_react/lucid_component/instance_store_proxy.rb
isomorfeus-react-16.13.4 lib/isomorfeus_react/lucid_component/instance_store_proxy.rb
isomorfeus-react-16.13.3 lib/isomorfeus_react/lucid_component/instance_store_proxy.rb
isomorfeus-react-16.13.2 lib/isomorfeus_react/lucid_component/instance_store_proxy.rb
isomorfeus-react-16.13.1 lib/isomorfeus_react/lucid_component/instance_store_proxy.rb
isomorfeus-react-16.13.0 lib/isomorfeus_react/lucid_component/instance_store_proxy.rb