Sha256: 5f0f8e1c7df27aedce134972ebb9507ac534390494d5e071cab4ca7e819dbeb7

Contents?: true

Size: 1.81 KB

Versions: 8

Compression:

Stored size: 1.81 KB

Contents

module React
  module ReduxComponent
    class ClassStoreProxy

      def initialize(component_instance, access_key = 'state')
        @native_component_instance = component_instance.to_n
        @component_instance = component_instance
        @component_name = component_instance.class.to_s
        @access_key = access_key
      end

      def method_missing(key, *args, &block)
        @native_component_instance.JS.register_used_store_path(['component_class_state', @component_name, key])
        if `args.length > 0`
          # set class state, simply a dispatch
          action = { type: 'COMPONENT_CLASS_STATE', class: @component_name, name: (`key.endsWith('=')` ? key.chop : key), value: args[0] }
          Isomorfeus.store.dispatch(action)
        else
          # get class state
          # check if we have a component local state value
          if @native_component_instance.JS[@access_key].JS[:isomorfeus_store].JS[:component_class_state].JS[@component_name] &&
            @native_component_instance.JS[@access_key].JS[:isomorfeus_store].JS[:component_class_state].JS[@component_name].JS.hasOwnProperty(key)
            return @native_component_instance.JS[@access_key].JS[:isomorfeus_store].JS[:component_class_state].JS[@component_name].JS[key]
          elsif @component_instance.class.default_class_store_defined && @component_instance.class.class_store.to_h.key?(key)
            # check if a default value was given
            return @component_instance.class.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
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
isomorfeus-react-16.8.9 lib/react/redux_component/class_store_proxy.rb
isomorfeus-react-16.8.8 lib/react/redux_component/class_store_proxy.rb
isomorfeus-react-16.8.7 lib/react/redux_component/class_store_proxy.rb
isomorfeus-react-16.8.6 lib/react/redux_component/class_store_proxy.rb
isomorfeus-react-16.8.5 lib/react/redux_component/class_store_proxy.rb
isomorfeus-react-16.8.4 lib/react/redux_component/class_store_proxy.rb
isomorfeus-react-16.8.3 lib/react/redux_component/class_store_proxy.rb
isomorfeus-react-16.8.2 lib/react/redux_component/class_store_proxy.rb