Module: Preact::Component::Api
- Defined in:
- lib/preact/component/api.rb
Class Method Summary collapse
Instance Method Summary collapse
- #display_name ⇒ Object
- #force_update(&block) ⇒ Object
- #get_preact_element(arg, &block) ⇒ Object (also: #gpe)
- #history ⇒ Object
- #method_ref(method_symbol, *args) ⇒ Object (also: #m_ref)
- #ref(name) ⇒ Object
- #render_preact_element(el) ⇒ Object (also: #rpe)
- #ruby_ref(name) ⇒ Object
- #set_state(updater, &callback) ⇒ Object
Class Method Details
.included(base) ⇒ Object
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/preact/component/api.rb', line 2 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_refs ||= `{}` 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 = `Opal.Preact.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 def to_js self.JS[:preact_component] end end end |
Instance Method Details
#display_name ⇒ Object
59 60 61 |
# File 'lib/preact/component/api.rb', line 59 def display_name @native.JS[:displayName] end |
#force_update(&block) ⇒ Object
63 64 65 66 67 68 69 70 |
# File 'lib/preact/component/api.rb', line 63 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) ⇒ Object Also known as: gpe
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/preact/component/api.rb', line 72 def get_preact_element(arg, &block) `const 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
94 95 96 |
# File 'lib/preact/component/api.rb', line 94 def history Isomorfeus.browser_history end |
#method_ref(method_symbol, *args) ⇒ Object Also known as: m_ref
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/preact/component/api.rb', line 98 def method_ref(method_symbol, *args) method_key = "#{method_symbol}#{args}" %x{ if (#@native.method_refs?.[#{method_key}]) { return #@native.method_refs[#{method_key}]; } if (!#@native.method_refs) { #@native.method_refs = {}; } #@native.method_refs[#{method_key}] = { m: null, a: args }; let r = #@native.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 #@native.method_refs[#{method_key}]; } end |
#ref(name) ⇒ Object
125 126 127 |
# File 'lib/preact/component/api.rb', line 125 def ref(name) `#@native[name]` end |
#render_preact_element(el) ⇒ Object Also known as: rpe
117 118 119 120 121 122 |
# File 'lib/preact/component/api.rb', line 117 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 |
#ruby_ref(name) ⇒ Object
129 130 131 132 |
# File 'lib/preact/component/api.rb', line 129 def ruby_ref(name) return `#@native[name]` if `(typeof #@native[name] === 'function')` `Opal.Preact.Ref.$new(#@native[name])` end |
#set_state(updater, &callback) ⇒ Object
134 135 136 |
# File 'lib/preact/component/api.rb', line 134 def set_state(updater, &callback) @state.set_state(updater, &callback) end |