Sha256: 467c9649752c78ecd60db32e1dbe4daa101953cd4c172dfc9775325de4783c91

Contents?: true

Size: 1.51 KB

Versions: 27

Compression:

Stored size: 1.51 KB

Contents

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

      def initialize(native)
        @native = native
      end
      
      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
          %x{
            if (typeof #@native.state[key] === 'undefined') { return nil; }
            return #@native.state[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

27 entries across 27 versions & 1 rubygems

Version Path
isomorfeus-preact-10.6.14 lib/preact/component/state.rb
isomorfeus-preact-10.6.13 lib/preact/component/state.rb
isomorfeus-preact-10.6.12 lib/preact/component/state.rb
isomorfeus-preact-10.6.11 lib/preact/component/state.rb
isomorfeus-preact-10.6.10 lib/preact/component/state.rb
isomorfeus-preact-10.6.9 lib/preact/component/state.rb
isomorfeus-preact-10.6.8 lib/preact/component/state.rb
isomorfeus-preact-10.6.7 lib/preact/component/state.rb
isomorfeus-preact-10.6.6 lib/preact/component/state.rb
isomorfeus-preact-10.6.5 lib/preact/component/state.rb
isomorfeus-preact-10.6.4 lib/preact/component/state.rb
isomorfeus-preact-10.6.3 lib/preact/component/state.rb
isomorfeus-preact-10.6.2 lib/preact/component/state.rb
isomorfeus-preact-10.6.1 lib/preact/component/state.rb
isomorfeus-preact-10.6.0 lib/preact/component/state.rb
isomorfeus-preact-10.5.11 lib/preact/component/state.rb
isomorfeus-preact-10.5.10 lib/preact/component/state.rb
isomorfeus-preact-10.5.9 lib/preact/component/state.rb
isomorfeus-preact-10.5.8 lib/preact/component/state.rb
isomorfeus-preact-10.5.7 lib/preact/component/state.rb