Sha256: d9787351d2b1fdc704161780f32304bb55e2d16aaf84a073bbef6b5d243647b2

Contents?: true

Size: 1.5 KB

Versions: 14

Compression:

Stored size: 1.5 KB

Contents

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

    def method_missing(key, *args, &block)
      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[:context].JS[:component_state].JS[@component_object_id] &&
          @native_component_instance.JS[:context].JS[:component_state].JS[@component_object_id].JS.hasOwnProperty(key)
          # check if we have a component local state value
          return @native_component_instance.JS[:context].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

14 entries across 14 versions & 1 rubygems

Version Path
isomorfeus-react-16.10.6 lib/lucid_component/instance_store_proxy.rb
isomorfeus-react-16.10.5 lib/lucid_component/instance_store_proxy.rb
isomorfeus-react-16.10.4 lib/lucid_component/instance_store_proxy.rb
isomorfeus-react-16.10.3 lib/lucid_component/instance_store_proxy.rb
isomorfeus-react-16.10.2 lib/lucid_component/instance_store_proxy.rb
isomorfeus-react-16.10.1 lib/lucid_component/instance_store_proxy.rb
isomorfeus-react-16.9.24 lib/lucid_component/instance_store_proxy.rb
isomorfeus-react-16.9.23 lib/lucid_component/instance_store_proxy.rb
isomorfeus-react-16.9.22 lib/lucid_component/instance_store_proxy.rb
isomorfeus-react-16.9.21 lib/lucid_component/instance_store_proxy.rb
isomorfeus-react-16.9.20 lib/lucid_component/instance_store_proxy.rb
isomorfeus-react-16.9.19 lib/lucid_component/instance_store_proxy.rb
isomorfeus-react-16.9.18 lib/lucid_component/instance_store_proxy.rb
isomorfeus-react-16.9.17 lib/lucid_component/instance_store_proxy.rb