module Preact::Component::Api

Public Class Methods

included(base) click to toggle source
# File lib/preact/component/api.rb, line 4
def self.included(base)
  base.instance_exec do
    base_module = base.to_s.deconstantize
    if base_module != ''
      base_module.constantize.define_singleton_method(base.to_s.demodulize) do |*args, &block|
        `Opal.Preact.internal_prepare_args_and_render(#{base}.preact_component, args, block)`
      end
    else
      Object.define_method(base.to_s) do |*args, &block|
        `Opal.Preact.internal_prepare_args_and_render(#{base}.preact_component, args, block)`
      end
    end

    attr_accessor :props
    attr_accessor :state

    def ref(ref_name, &block)
      defined_refs.JS[ref_name] = block_given? ? block : `null`
    end

    def defined_refs
      @defined_ref ||= `{}`
    end

    def default_state_defined
      @default_state_defined
    end

    def state
      return @default_state if @default_state
      @default_state_defined = true
      %x{
        var native_state = {state: {}};
        native_state.setState = function(new_state, callback) {
          for (var key in new_state) {
            this.state[key] = new_state[key];
          }
          if (callback) { callback.call(); }
        }
      }
      @default_state = Preact::Component::State.new(`native_state`)
    end

    def render(&block)
      `base.render_block = #{block}`
    end

    def should_component_update?(&block)
      `base.should_component_update_block = block`
    end
  end
end

Public Instance Methods

default_state_defined() click to toggle source
# File lib/preact/component/api.rb, line 28
def default_state_defined
  @default_state_defined
end
defined_refs() click to toggle source
# File lib/preact/component/api.rb, line 24
def defined_refs
  @defined_ref ||= `{}`
end
display_name() click to toggle source
# File lib/preact/component/api.rb, line 57
def display_name
  @native.JS[:displayName]
end
force_update(&block) click to toggle source
# File lib/preact/component/api.rb, line 61
def force_update(&block)
  if block_given?
    # this maybe needs instance_exec too
    @native.JS.forceUpdate(`function() { block.$call(); }`)
  else
    @native.JS.forceUpdate
  end
end
get_preact_element(arg, &block) click to toggle source
# File lib/preact/component/api.rb, line 70
def get_preact_element(arg, &block)
  if block_given?
    # execute block, fetch last element from buffer
    %x{
      let last_buffer_length = Opal.Preact.render_buffer[Opal.Preact.render_buffer.length - 1].length;
      let last_buffer_element = Opal.Preact.render_buffer[Opal.Preact.render_buffer.length - 1][last_buffer_length - 1];
      block.$call();
      // console.log("get_preact_element popping", Opal.Preact.render_buffer, Opal.Preact.render_buffer.toString())
      let new_element = Opal.Preact.render_buffer[Opal.Preact.render_buffer.length - 1].pop();
      if (last_buffer_element === new_element) { #{Isomorfeus.raise_error(message: "Block did not create any Preact element!")} }
      return new_element;
    }
  else
    # element was rendered before being passed as arg
    # fetch last element from buffer
    # `console.log("get_preact_element popping", Opal.Preact.render_buffer, Opal.Preact.render_buffer.toString())`
    `Opal.Preact.render_buffer[Opal.Preact.render_buffer.length - 1].pop()`
  end
end
Also aliased as: gpe
gpe(arg, &block)
Alias for: get_preact_element
m_ref(method_symbol, *args)
Alias for: method_ref
method_ref(method_symbol, *args) click to toggle source
# File lib/preact/component/api.rb, line 91
def method_ref(method_symbol, *args)
  method_key = "#{method_symbol}#{args}"
  %x{
    if (#@native.method_refs && #@native.method_refs[#{method_key}]) { return #@native.method_refs[#{method_key}]; }
    if (!#@native.method_refs) { #@native.method_refs = {}; }
    #@native.method_refs[#{method_key}] = { m: #{method(method_symbol)}, a: args };
    return #@native.method_refs[#{method_key}];
  }
end
Also aliased as: m_ref
ref(ref_name, &block) click to toggle source
# File lib/preact/component/api.rb, line 20
def ref(ref_name, &block)
  defined_refs.JS[ref_name] = block_given? ? block : `null`
end
render(&block) click to toggle source
# File lib/preact/component/api.rb, line 47
def render(&block)
  `base.render_block = #{block}`
end
render_preact_element(el) click to toggle source
# File lib/preact/component/api.rb, line 102
def render_preact_element(el)
  # push el to buffer
  `Opal.Preact.render_buffer[Opal.Preact.render_buffer.length - 1].push(el)`
  # `console.log("render_preact_element pushed", Opal.Preact.render_buffer, Opal.Preact.render_buffer.toString())`
  nil
end
Also aliased as: rpe
rpe(el)
ruby_ref(name) click to toggle source
# File lib/preact/component/api.rb, line 114
def ruby_ref(name)
  return `#@native[name]` if `(typeof #@native[name] === 'function')`
  Preact::Ref::new(`#@native[name]`)
end
set_state(updater, &callback) click to toggle source
# File lib/preact/component/api.rb, line 119
def set_state(updater, &callback)
  @state.set_state(updater, &callback)
end
should_component_update?(&block) click to toggle source
# File lib/preact/component/api.rb, line 51
def should_component_update?(&block)
  `base.should_component_update_block = block`
end
state() click to toggle source
# File lib/preact/component/api.rb, line 32
def state
  return @default_state if @default_state
  @default_state_defined = true
  %x{
    var native_state = {state: {}};
    native_state.setState = function(new_state, callback) {
      for (var key in new_state) {
        this.state[key] = new_state[key];
      }
      if (callback) { callback.call(); }
    }
  }
  @default_state = Preact::Component::State.new(`native_state`)
end