Sha256: 9a5c5334eb469ca2cd18af1c728f3bf2494e52060a7743420b8e04b7930c90a3

Contents?: true

Size: 1.6 KB

Versions: 2

Compression:

Stored size: 1.6 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)
      @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[: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

2 entries across 2 versions & 1 rubygems

Version Path
isomorfeus-react-16.9.16 lib/lucid_component/instance_store_proxy.rb
isomorfeus-react-16.9.15 lib/lucid_component/instance_store_proxy.rb