Sha256: 57a6292c637ed7e9c4dc2d5db88285666c5ecd7f1daa882d8d0aa7ee30e84ee2

Contents?: true

Size: 1.35 KB

Versions: 19

Compression:

Stored size: 1.35 KB

Contents

module React
  module Component
    class State
      include ::Native::Wrapper

      def method_missing(key, *args, &block)
        if `args.length > 0`
          new_state = `{}`
          new_state.JS[(`key.endsWith('=')` ? key.chop : key)] = args[0]
          if block_given?
            @native.JS.setState(new_state, `function() { block.$call(); }`)
          else
            @native.JS.setState(new_state, `null`)
          end
        else
          return nil if `typeof #@native.state[key] === "undefined"`
          @native.JS[:state].JS[key]
        end
      end

      def set_state(updater, &block)
        new_state = `{}`
        updater.each do |key, value|
          new_state.JS[key] = value
        end
        if block_given?
          @native.JS.setState(new_state, `function() { block.$call(); }`)
        else
          @native.JS.setState(new_state, `null`)
        end
      end

      def size
        `Object.keys(#@native.state).length`;
      end

      def to_n
        %x{
          var new_native = {};
          for (var key in #@native.state) {
            if (typeof #@native.state[key].$to_n !== "undefined") {
              new_native[key] = #@native.state[key].$to_n();
            } else {
              new_native[key] = #@native.state[key];
            }
          }
          return new_native;
        }
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
isomorfeus-react-16.13.8 lib/react/component/state.rb
isomorfeus-react-16.13.7 lib/react/component/state.rb
isomorfeus-react-16.13.6 lib/react/component/state.rb
isomorfeus-react-16.13.5 lib/react/component/state.rb
isomorfeus-react-16.13.4 lib/react/component/state.rb
isomorfeus-react-16.13.3 lib/react/component/state.rb
isomorfeus-react-16.13.2 lib/react/component/state.rb
isomorfeus-react-16.13.1 lib/react/component/state.rb
isomorfeus-react-16.13.0 lib/react/component/state.rb
isomorfeus-react-16.12.24 lib/react/component/state.rb
isomorfeus-react-16.12.23 lib/react/component/state.rb
isomorfeus-react-16.12.22 lib/react/component/state.rb
isomorfeus-react-16.12.21 lib/react/component/state.rb
isomorfeus-react-16.12.20 lib/react/component/state.rb
isomorfeus-react-16.12.19 lib/react/component/state.rb
isomorfeus-react-16.12.18 lib/react/component/state.rb
isomorfeus-react-16.12.17 lib/react/component/state.rb
isomorfeus-react-16.12.16 lib/react/component/state.rb
isomorfeus-react-16.12.15 lib/react/component/state.rb