module Preact::FunctionComponent::Api

Public Instance Methods

get_preact_element(arg, &block) click to toggle source
# File lib/isomorfeus_preact/preact/function_component/api.rb, line 78
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/isomorfeus_preact/preact/function_component/api.rb, line 107
def method_ref(method_symbol, *args)
  method_key = "#{method_symbol}#{args}"
  %x{
    if (#{self}.method_refs && #{self}.method_refs[#{method_key}]) { return #{self}.method_refs[#{method_key}]; }
    if (!#{self}.method_refs) { #{self}.method_refs = {}; }
    #{self}.method_refs[#{method_key}] = { m: #{method(method_symbol)}, a: args };
    return #{self}.method_refs[#{method_key}];
  }
end
Also aliased as: m_ref
props() click to toggle source
# File lib/isomorfeus_preact/preact/function_component/api.rb, line 4
def props
  @native_props
end
render_preact_element(el) click to toggle source
# File lib/isomorfeus_preact/preact/function_component/api.rb, line 99
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)
to_n() click to toggle source
# File lib/isomorfeus_preact/preact/function_component/api.rb, line 118
def to_n
  self
end
use_callback(*deps, &block) click to toggle source
# File lib/isomorfeus_preact/preact/function_component/api.rb, line 8
def use_callback(*deps, &block)
  `Opal.global.PreactHooks.useCallback(function() { #{block.call} }, deps)`
end
use_context(context) click to toggle source
# File lib/isomorfeus_preact/preact/function_component/api.rb, line 12
def use_context(context)
  native_context = `(typeof context.$is_wrapped_context !== 'undefined')` ? context.to_n : context
  `Opal.global.PreactHooks.useContext(native_context)`
end
use_debug_value(value, formatter) click to toggle source
# File lib/isomorfeus_preact/preact/function_component/api.rb, line 17
def use_debug_value(value, formatter)
  formatter = `null` unless formatter
  `Opal.global.PreactHooks.useDebugValue(value, formatter)`
end
use_effect(*args, &block) click to toggle source
# File lib/isomorfeus_preact/preact/function_component/api.rb, line 22
def use_effect(*args, &block)
  `Opal.global.PreactHooks.useEffect(function() { #{block.call} }, args)`
end
use_error_boundary(&block) click to toggle source
# File lib/isomorfeus_preact/preact/function_component/api.rb, line 26
def use_error_boundary(&block)
  error = nil
  reset_error = nil
  %x{
    let _error;
    let _reset_error;
    if (block) {
      [_error, reset_error] = Opal.global.PreactHooks.useErrorBoundary(function() { #{block.call(Error(_error))} });
    } else {
      [_error, reset_error] = Opal.global.PreactHooks.useErrorBoundary();
    }
    error = #{Error(e)};
  }
  [error, reset_error]
end
use_imperative_handle(ruby_ref, *args, &block) click to toggle source
# File lib/isomorfeus_preact/preact/function_component/api.rb, line 42
def use_imperative_handle(ruby_ref, *args, &block)
  ref = ruby_ref.to_n
  args = `null` if args.empty?
  `Opal.global.PreactHooks.useImperativeHandle(ref, function() { #{block.call} }, args)`
end
use_layout_effect(&block) click to toggle source
# File lib/isomorfeus_preact/preact/function_component/api.rb, line 48
def use_layout_effect(&block)
  `Opal.global.PreactHooks.useLayoutEffect(function() { #{block.call} })`
end
use_memo(*deps, &block) click to toggle source
# File lib/isomorfeus_preact/preact/function_component/api.rb, line 52
def use_memo(*deps, &block)
  `Opal.global.PreactHooks.useMemo(function() { #{block.call} }, deps)`
end
use_reducer(inital_state, &block) click to toggle source
# File lib/isomorfeus_preact/preact/function_component/api.rb, line 56
def use_reducer(inital_state, &block)
  state = nil
  dispatcher = nil
  %x{
    [state, dispatcher] = Opal.global.PreactHooks.useReducer(function(state, action) {
      #{block.call(state, action)}
    }, initial_state);
  }
  [state, proc { |arg| `dispatcher(arg)` }]
end
use_ref(native_ref) click to toggle source
# File lib/isomorfeus_preact/preact/function_component/api.rb, line 67
def use_ref(native_ref)
  Preact::Ref.new(`Opal.global.PreactHooks.useRef(native_ref)`)
end
use_state(initial_value) click to toggle source
# File lib/isomorfeus_preact/preact/function_component/api.rb, line 71
def use_state(initial_value)
  initial = nil
  setter = nil
  `[initial, setter] = Opal.global.PreactHooks.useState(initial_value);`
  [initial, proc { |arg| `setter(arg)` }]
end