class Preact::Component::State

Public Class Methods

new(native) click to toggle source
# File lib/preact/component/state.rb, line 6
def initialize(native)
  @native = native
end

Public Instance Methods

method_missing(key, *args, &block) click to toggle source
# File lib/preact/component/state.rb, line 10
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
set_state(updater, &block) click to toggle source
# File lib/preact/component/state.rb, line 27
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
size() click to toggle source
# File lib/preact/component/state.rb, line 39
def size
  `Object.keys(#@native.state).length`;
end
to_n() click to toggle source
# File lib/preact/component/state.rb, line 43
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