Module: Preact::FunctionComponent::Api
- Defined in:
- lib/isomorfeus_preact/preact/function_component/api.rb
Class Method Summary collapse
Instance Method Summary collapse
- #get_preact_element(arg, &block) ⇒ Object (also: #gpe)
- #history ⇒ Object
- #method_ref(method_symbol, *args) ⇒ Object (also: #m_ref)
- #props ⇒ Object
- #render_preact_element(el) ⇒ Object (also: #rpe)
- #to_n ⇒ Object
- #use_callback(*deps, &block) ⇒ Object
- #use_context(context) ⇒ Object
- #use_debug_value(value, formatter) ⇒ Object
- #use_deferred_value(value) ⇒ Object
- #use_effect(*args, &block) ⇒ Object
- #use_error_boundary(&block) ⇒ Object
- #use_id ⇒ Object
- #use_imperative_handle(ruby_ref, *args, &block) ⇒ Object
- #use_insertion_effect ⇒ Object
- #use_layout_effect(&block) ⇒ Object
- #use_memo(*deps, &block) ⇒ Object
- #use_reducer(inital_state, &block) ⇒ Object
- #use_ref(native_ref) ⇒ Object
- #use_state(initial_value) ⇒ Object
- #use_sync_external_store(subscribe, get_snapshot, get_server_snapshot) ⇒ Object
- #use_transition ⇒ Object
Class Method Details
.included(base) ⇒ Object
2 3 4 5 6 7 8 |
# File 'lib/isomorfeus_preact/preact/function_component/api.rb', line 2 def self.included(base) base.instance_exec do def to_js self.JS[:preact_component] end end end |
Instance Method Details
#get_preact_element(arg, &block) ⇒ Object Also known as: gpe
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/isomorfeus_preact/preact/function_component/api.rb', line 108 def get_preact_element(arg, &block) `let operabu = Opal.Preact.render_buffer` if block_given? # execute block, fetch last element from buffer %x{ let last_buffer_length = operabu[operabu.length - 1].length; let last_buffer_element = operabu[operabu.length - 1][last_buffer_length - 1]; block.$call(); // console.log("get_preact_element popping", operabu, operabu.toString()) let new_element = operabu[operabu.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", operabu, operabu.toString())` `operabu[operabu.length - 1].pop()` end end |
#history ⇒ Object
130 131 132 |
# File 'lib/isomorfeus_preact/preact/function_component/api.rb', line 130 def history Isomorfeus.browser_history end |
#method_ref(method_symbol, *args) ⇒ Object Also known as: m_ref
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/isomorfeus_preact/preact/function_component/api.rb', line 142 def method_ref(method_symbol, *args) method_key = "#{method_symbol}#{args}" %x{ if (#{self}.method_refs?.[#{method_key}]) { return #{self}.method_refs[#{method_key}]; } if (!#{self}.method_refs) { #{self}.method_refs = {}; } #{self}.method_refs[#{method_key}] = { m: null, a: args }; let r = #{self}.method_refs[#{method_key}]; let dev = #{Isomorfeus.development?}; r.preact_event_handler_function = function(event, info) { let ruby_event = Opal.Preact.native_to_ruby_event(event); if (!r.m || dev) { r.m = #{method(method_symbol)} }; if (r.a.length > 0) { r.m.$call.apply(r.m, [ruby_event, info].concat(r.a)); } else { r.m.$call(ruby_event, info); } }; return #{self}.method_refs[#{method_key}]; } end |
#props ⇒ Object
10 11 12 |
# File 'lib/isomorfeus_preact/preact/function_component/api.rb', line 10 def props @native_props end |
#render_preact_element(el) ⇒ Object Also known as: rpe
134 135 136 137 138 139 |
# File 'lib/isomorfeus_preact/preact/function_component/api.rb', line 134 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 |
#to_n ⇒ Object
161 162 163 |
# File 'lib/isomorfeus_preact/preact/function_component/api.rb', line 161 def to_n self end |
#use_callback(*deps, &block) ⇒ Object
14 15 16 |
# File 'lib/isomorfeus_preact/preact/function_component/api.rb', line 14 def use_callback(*deps, &block) `Opal.global.PreactHooks.useCallback(function() { #{block.call} }, deps)` end |
#use_context(context) ⇒ Object
18 19 20 21 |
# File 'lib/isomorfeus_preact/preact/function_component/api.rb', line 18 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) ⇒ Object
23 24 25 26 |
# File 'lib/isomorfeus_preact/preact/function_component/api.rb', line 23 def use_debug_value(value, formatter) formatter = `null` unless formatter `Opal.global.PreactHooks.useDebugValue(value, formatter)` end |
#use_deferred_value(value) ⇒ Object
28 29 30 |
# File 'lib/isomorfeus_preact/preact/function_component/api.rb', line 28 def use_deferred_value(value) `Opal.global.PreactHooks.useDeferredValue(value)` end |
#use_effect(*args, &block) ⇒ Object
32 33 34 |
# File 'lib/isomorfeus_preact/preact/function_component/api.rb', line 32 def use_effect(*args, &block) `Opal.global.PreactHooks.useEffect(function() { #{block.call} }, args)` end |
#use_error_boundary(&block) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/isomorfeus_preact/preact/function_component/api.rb', line 36 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_id ⇒ Object
52 53 54 |
# File 'lib/isomorfeus_preact/preact/function_component/api.rb', line 52 def use_id `Opal.global.PreactHooks.useId()` end |
#use_imperative_handle(ruby_ref, *args, &block) ⇒ Object
56 57 58 59 60 |
# File 'lib/isomorfeus_preact/preact/function_component/api.rb', line 56 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_insertion_effect ⇒ Object
62 63 64 |
# File 'lib/isomorfeus_preact/preact/function_component/api.rb', line 62 def use_insertion_effect `Opal.global.PreactHooks.useInsertionEffect(function() { #{block.call} }, args)` end |
#use_layout_effect(&block) ⇒ Object
66 67 68 |
# File 'lib/isomorfeus_preact/preact/function_component/api.rb', line 66 def use_layout_effect(&block) `Opal.global.PreactHooks.useLayoutEffect(function() { #{block.call} })` end |
#use_memo(*deps, &block) ⇒ Object
70 71 72 |
# File 'lib/isomorfeus_preact/preact/function_component/api.rb', line 70 def use_memo(*deps, &block) `Opal.global.PreactHooks.useMemo(function() { #{block.call} }, deps)` end |
#use_reducer(inital_state, &block) ⇒ Object
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/isomorfeus_preact/preact/function_component/api.rb', line 74 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) ⇒ Object
85 86 87 |
# File 'lib/isomorfeus_preact/preact/function_component/api.rb', line 85 def use_ref(native_ref) Preact::Ref.new(`Opal.global.PreactHooks.useRef(native_ref)`) end |
#use_state(initial_value) ⇒ Object
89 90 91 92 93 94 |
# File 'lib/isomorfeus_preact/preact/function_component/api.rb', line 89 def use_state(initial_value) initial = nil setter = nil `[initial, setter] = Opal.global.PreactHooks.useState(initial_value);` [initial, proc { |arg| `setter(arg)` }] end |
#use_sync_external_store(subscribe, get_snapshot, get_server_snapshot) ⇒ Object
96 97 98 99 |
# File 'lib/isomorfeus_preact/preact/function_component/api.rb', line 96 def use_sync_external_store(subscribe, get_snapshot, get_server_snapshot) gss = get_server_snapshot.nil? ? `null` : `function() { return #{get_server_snapshot.call} }` `Opal.global.PreactHooks.useSyncExternalStore(function() { #{subscribe.call} }, function() { return #{get_snapshot.call}}, gss)` end |
#use_transition ⇒ Object
101 102 103 104 105 106 |
# File 'lib/isomorfeus_preact/preact/function_component/api.rb', line 101 def use_transition is_pending = nil start_transition = nil `[is_pending, start_transition] = Opal.global.PreactHooks.useTransition()` [is_pending, proc { |arg| `start_transition(function(){#{arg&.call}})`}] end |