isomorfeus-preact 10.6.31 → 10.6.35

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/lib/isomorfeus/preact_view_helper.rb +18 -16
  4. data/lib/isomorfeus-preact.rb +15 -0
  5. data/lib/isomorfeus_preact/lucid_app/api.rb +30 -32
  6. data/lib/isomorfeus_preact/lucid_app/mixin.rb +12 -13
  7. data/lib/isomorfeus_preact/lucid_app/native_component_constructor.rb +88 -90
  8. data/lib/isomorfeus_preact/lucid_component/api.rb +95 -98
  9. data/lib/isomorfeus_preact/lucid_component/initializer.rb +7 -10
  10. data/lib/isomorfeus_preact/lucid_component/mixin.rb +11 -12
  11. data/lib/isomorfeus_preact/lucid_component/native_component_constructor.rb +77 -79
  12. data/lib/isomorfeus_preact/lucid_func/initializer.rb +6 -9
  13. data/lib/isomorfeus_preact/lucid_func/mixin.rb +8 -9
  14. data/lib/isomorfeus_preact/lucid_func/native_component_constructor.rb +49 -51
  15. data/lib/isomorfeus_preact/preact/function_component/api.rb +102 -106
  16. data/lib/isomorfeus_preact/preact/function_component/base.rb +5 -7
  17. data/lib/isomorfeus_preact/preact/function_component/initializer.rb +4 -8
  18. data/lib/isomorfeus_preact/preact/function_component/mixin.rb +6 -10
  19. data/lib/isomorfeus_preact/preact/function_component/native_component_constructor.rb +41 -45
  20. data/lib/lucid_app/context.rb +1 -1
  21. data/lib/nano_css.rb +10 -10
  22. data/lib/preact/component/api.rb +99 -103
  23. data/lib/preact/component/base.rb +5 -7
  24. data/lib/preact/component/callbacks.rb +99 -103
  25. data/lib/preact/component/initializer.rb +5 -9
  26. data/lib/preact/component/mixin.rb +9 -13
  27. data/lib/preact/component/native_component_constructor.rb +67 -71
  28. data/lib/preact/component_resolution.rb +78 -80
  29. data/lib/preact/elements.rb +55 -57
  30. data/lib/preact/version.rb +1 -1
  31. data/node_modules/.package-lock.json +3 -3
  32. data/node_modules/preact/compat/LICENSE +21 -0
  33. data/node_modules/preact/compat/jsx-dev-runtime.js +2 -0
  34. data/node_modules/preact/compat/jsx-dev-runtime.mjs +2 -0
  35. data/node_modules/preact/compat/jsx-runtime.js +2 -0
  36. data/node_modules/preact/compat/jsx-runtime.mjs +2 -0
  37. data/node_modules/preact/debug/LICENSE +21 -0
  38. data/node_modules/preact/devtools/LICENSE +21 -0
  39. data/node_modules/preact/devtools/dist/devtools.js +1 -1
  40. data/node_modules/preact/devtools/dist/devtools.js.map +1 -1
  41. data/node_modules/preact/devtools/dist/devtools.mjs +1 -1
  42. data/node_modules/preact/devtools/dist/devtools.module.js +1 -1
  43. data/node_modules/preact/devtools/dist/devtools.module.js.map +1 -1
  44. data/node_modules/preact/devtools/dist/devtools.umd.js +1 -1
  45. data/node_modules/preact/devtools/dist/devtools.umd.js.map +1 -1
  46. data/node_modules/preact/devtools/src/devtools.js +1 -1
  47. data/node_modules/preact/dist/preact.js +1 -1
  48. data/node_modules/preact/dist/preact.js.map +1 -1
  49. data/node_modules/preact/dist/preact.min.js +1 -1
  50. data/node_modules/preact/dist/preact.min.js.map +1 -1
  51. data/node_modules/preact/dist/preact.mjs +1 -1
  52. data/node_modules/preact/dist/preact.module.js +1 -1
  53. data/node_modules/preact/dist/preact.module.js.map +1 -1
  54. data/node_modules/preact/dist/preact.umd.js +1 -1
  55. data/node_modules/preact/dist/preact.umd.js.map +1 -1
  56. data/node_modules/preact/hooks/LICENSE +21 -0
  57. data/node_modules/preact/jsx-runtime/LICENSE +21 -0
  58. data/node_modules/preact/package.json +1 -1
  59. data/node_modules/preact/src/diff/index.js +6 -3
  60. data/package.json +1 -1
  61. metadata +16 -12
  62. data/lib/isomorfeus_preact/lucid_component/instance_store_proxy.rb +0 -44
@@ -1,12 +1,9 @@
1
- module LucidComponent
2
- module Initializer
3
- def initialize(native_component)
4
- @native = native_component
5
- @app_store = LucidComponent::AppStoreProxy.new(self)
6
- @class_store = LucidComponent::ClassStoreProxy.new(self.class.to_s, self, @native)
7
- @store = LucidComponent::InstanceStoreProxy.new(self)
8
- @props = `Opal.Preact.Props.$new(#@native)`
9
- @state = `Opal.Preact.State.$new(#@native)`
10
- end
1
+ module LucidComponent::Initializer
2
+ def initialize(native_component)
3
+ @native = native_component
4
+ @app_store = LucidComponent::AppStoreProxy.new(self)
5
+ @class_store = LucidComponent::ClassStoreProxy.new(self.class.to_s, self, @native)
6
+ @props = `Opal.Preact.Props.$new(#@native)`
7
+ @state = `Opal.Preact.State.$new(#@native)`
11
8
  end
12
9
  end
@@ -1,14 +1,13 @@
1
- module LucidComponent
2
- module Mixin
3
- def self.included(base)
4
- base.include(::Native::Wrapper)
5
- base.extend(::LucidComponent::NativeComponentConstructor)
6
- base.include(::Preact::Elements)
7
- base.extend(::LucidPropDeclaration::Mixin)
8
- base.include(::Preact::Component::Api)
9
- base.include(::Preact::Component::Callbacks)
10
- base.include(::LucidComponent::Api)
11
- base.include(::LucidComponent::Initializer)
12
- end
1
+ module LucidComponent::Mixin
2
+ def self.included(base)
3
+ base.include(::Native::Wrapper)
4
+ base.extend(::LucidComponent::NativeComponentConstructor)
5
+ base.include(::Preact::Elements)
6
+ base.extend(::LucidPropDeclaration::Mixin)
7
+ base.include(::Preact::Component::Api)
8
+ base.include(::Preact::Component::Callbacks)
9
+ base.include(::LucidComponent::Api)
10
+ base.include(::LucidComponent::Initializer)
11
+ base.include(::LucidI18n::Mixin) if `("lucid_i18n/mixin" in Opal.modules)`
13
12
  end
14
13
  end
@@ -1,89 +1,87 @@
1
- module LucidComponent
2
- module NativeComponentConstructor
3
- # for should_component_update we apply ruby semantics for comparing props
4
- # to do so, we convert the props to ruby hashes and then compare
5
- # this makes sure, that for example rubys Nil object gets handled properly
6
- def self.extended(base)
7
- component_name = base.to_s
8
- wrapper_name = component_name + 'Wrapper'
9
- %x{
10
- base.css_styles = null;
11
- base.preload_block = null;
12
- base.while_loading_block = null;
1
+ module LucidComponent::NativeComponentConstructor
2
+ # for should_component_update we apply ruby semantics for comparing props
3
+ # to do so, we convert the props to ruby hashes and then compare
4
+ # this makes sure, that for example rubys Nil object gets handled properly
5
+ def self.extended(base)
6
+ component_name = base.to_s
7
+ wrapper_name = component_name + 'Wrapper'
8
+ %x{
9
+ base.css_styles = null;
10
+ base.preload_block = null;
11
+ base.while_loading_block = null;
13
12
 
14
- base.preact_component = function(props) {
15
- let value = Opal.global.PreactHooks.useContext(Opal.global.LucidApplicationContext);
16
- return Opal.global.Preact.createElement(base.lucid_preact_component, Object.assign({}, props, value));
17
- };
18
- base.preact_component.displayName = #{wrapper_name};
19
- base.lucid_preact_component = class extends Opal.global.Preact.Component {
20
- constructor(props) {
21
- super(props);
22
- const oper = Opal.Preact;
23
- if (base.$default_state_defined()) {
24
- this.state = base.$state().$to_n();
25
- } else {
26
- this.state = {};
27
- };
28
- this.__ruby_instance = base.$new(this);
29
- var defined_refs = base.$defined_refs();
30
- for (var ref in defined_refs) {
31
- if (defined_refs[ref] != null) {
32
- let r = ref; // to ensure closure for function below gets correct ref name
33
- this[ref] = function(element) {
34
- element = oper.native_element_or_component_to_ruby(element);
35
- oper.register_active_component(this);
36
- try {
37
- #{`this.__ruby_instance`.instance_exec(`element`, &`defined_refs[r]`)}
38
- } catch (e) { console.error(e.message === nil ? 'error at' : e.message, e.stack); }
39
- oper.unregister_active_component(this);
40
- }
41
- this[ref] = this[ref].bind(this);
42
- } else {
43
- this[ref] = Opal.global.Preact.createRef();
13
+ base.preact_component = function(props) {
14
+ let value = Opal.global.PreactHooks.useContext(Opal.global.LucidApplicationContext);
15
+ return Opal.global.Preact.createElement(base.lucid_preact_component, Object.assign({}, props, value));
16
+ };
17
+ base.preact_component.displayName = #{wrapper_name};
18
+ base.lucid_preact_component = class extends Opal.global.Preact.Component {
19
+ constructor(props) {
20
+ super(props);
21
+ const oper = Opal.Preact;
22
+ if (base.$default_state_defined()) {
23
+ this.state = base.$state().$to_n();
24
+ } else {
25
+ this.state = {};
26
+ };
27
+ this.__ruby_instance = base.$new(this);
28
+ var defined_refs = base.$defined_refs();
29
+ for (var ref in defined_refs) {
30
+ if (defined_refs[ref] != null) {
31
+ let r = ref; // to ensure closure for function below gets correct ref name
32
+ this[ref] = function(element) {
33
+ element = oper.native_element_or_component_to_ruby(element);
34
+ oper.register_active_component(this);
35
+ try {
36
+ #{`this.__ruby_instance`.instance_exec(`element`, &`defined_refs[r]`)}
37
+ } catch (e) { console.error(e.message === nil ? 'error at' : e.message, e.stack); }
38
+ oper.unregister_active_component(this);
44
39
  }
40
+ this[ref] = this[ref].bind(this);
41
+ } else {
42
+ this[ref] = Opal.global.Preact.createRef();
45
43
  }
46
- if (base.preload_block) {
47
- oper.register_active_component(this);
48
- this.state.preloaded = this.__ruby_instance.$execute_preload_block(); // caught in execute_preload_block itself
49
- oper.unregister_active_component(this);
50
- }
51
- }
52
- static get displayName() {
53
- return #{component_name};
54
44
  }
55
- render(props, state) {
56
- const oper = Opal.Preact;
57
- oper.render_buffer.push([]);
45
+ if (base.preload_block) {
58
46
  oper.register_active_component(this);
59
- let block_result;
60
- try {
61
- if (base.while_loading_block && !state.preloaded) { block_result = #{`this.__ruby_instance`.instance_exec(&`base.while_loading_block`)}; }
62
- else { block_result = #{`this.__ruby_instance`.instance_exec(&`base.render_block`)}; }
63
- if (block_result && block_result !== nil) { oper.render_block_result(block_result); }
64
- } catch (e) {
65
- if (oper.using_did_catch) { throw e; }
66
- else { console.error(e.message === nil ? 'error at' : e.message, e.stack); }
67
- }
47
+ this.state.preloaded = this.__ruby_instance.$execute_preload_block(); // caught in execute_preload_block itself
68
48
  oper.unregister_active_component(this);
69
- let result = oper.render_buffer.pop();
70
- return (result.length === 1) ? result[0] : result;
71
- }
72
- data_access() {
73
- return this.props.iso_store;
74
- }
75
- shouldComponentUpdate(next_props, next_state) {
76
- if (!Opal.Preact.props_are_equal(this.props, next_props)) { return true; }
77
- if (Opal.Preact.state_is_not_equal(this.state, next_state)) { return true; }
78
- return false;
79
49
  }
80
- validateProp(props, propName, componentName) {
81
- try { base.$validate_prop(propName, props[propName]) }
82
- catch (e) { return new Error(componentName + " Error: prop validation failed: " + e.message); }
83
- return null;
50
+ }
51
+ static get displayName() {
52
+ return #{component_name};
53
+ }
54
+ render(props, state) {
55
+ const oper = Opal.Preact;
56
+ oper.render_buffer.push([]);
57
+ oper.register_active_component(this);
58
+ let block_result;
59
+ try {
60
+ if (base.while_loading_block && !state.preloaded) { block_result = #{`this.__ruby_instance`.instance_exec(&`base.while_loading_block`)}; }
61
+ else { block_result = #{`this.__ruby_instance`.instance_exec(&`base.render_block`)}; }
62
+ if (block_result && block_result !== nil) { oper.render_block_result(block_result); }
63
+ } catch (e) {
64
+ if (oper.using_did_catch) { throw e; }
65
+ else { console.error(e.message === nil ? 'error at' : e.message, e.stack); }
84
66
  }
85
- };
86
- }
87
- end
67
+ oper.unregister_active_component(this);
68
+ let result = oper.render_buffer.pop();
69
+ return (result.length === 1) ? result[0] : result;
70
+ }
71
+ data_access() {
72
+ return this.props.iso_store;
73
+ }
74
+ shouldComponentUpdate(next_props, next_state) {
75
+ if (!Opal.Preact.props_are_equal(this.props, next_props)) { return true; }
76
+ if (Opal.Preact.state_is_not_equal(this.state, next_state)) { return true; }
77
+ return false;
78
+ }
79
+ validateProp(props, propName, componentName) {
80
+ try { base.$validate_prop(propName, props[propName]) }
81
+ catch (e) { return new Error(componentName + " Error: prop validation failed: " + e.message); }
82
+ return null;
83
+ }
84
+ };
85
+ }
88
86
  end
89
87
  end
@@ -1,11 +1,8 @@
1
- module LucidFunc
2
- module Initializer
3
- def initialize
4
- self.JS[:native_props] = `{ props: null }`
5
- @native_props = `Opal.Preact.Props.$new(#{self})`
6
- @app_store = LucidComponent::AppStoreProxy.new(self)
7
- @class_store = LucidComponent::ClassStoreProxy.new(self.class.to_s, self, self)
8
- @store = LucidComponent::InstanceStoreProxy.new(self)
9
- end
1
+ module LucidFunc::Initializer
2
+ def initialize
3
+ self.JS[:native_props] = `{ props: null }`
4
+ @native_props = `Opal.Preact.Props.$new(#{self})`
5
+ @app_store = LucidComponent::AppStoreProxy.new(self)
6
+ @class_store = LucidComponent::ClassStoreProxy.new(self.class.to_s, self, self)
10
7
  end
11
8
  end
@@ -1,11 +1,10 @@
1
- module LucidFunc
2
- module Mixin
3
- def self.included(base)
4
- base.include(::LucidFunc::Initializer)
5
- base.include(::Preact::FunctionComponent::Api)
6
- base.extend(::LucidFunc::NativeComponentConstructor)
7
- base.include(::Preact::Elements)
8
- base.include(::LucidComponent::Api)
9
- end
1
+ module LucidFunc::Mixin
2
+ def self.included(base)
3
+ base.include(::LucidFunc::Initializer)
4
+ base.include(::Preact::FunctionComponent::Api)
5
+ base.extend(::LucidFunc::NativeComponentConstructor)
6
+ base.include(::Preact::Elements)
7
+ base.include(::LucidComponent::Api)
8
+ base.include(::LucidI18n::Mixin) if `("lucid_i18n/mixin" in Opal.modules)`
10
9
  end
11
10
  end
@@ -1,58 +1,56 @@
1
- module LucidFunc
2
- module NativeComponentConstructor
3
- def self.extended(base)
4
- component_name = base.to_s
5
- %x{
6
- base.css_styles = null;
7
- base.instance_init = function(initial) {
8
- let ruby_state = { instance: #{base.new(`{}`)} };
9
- ruby_state.instance.__ruby_instance = ruby_state.instance;
10
- ruby_state.instance.data_access = function() { return this.props.iso_store; }
11
- ruby_state.instance.data_access.bind(ruby_state.instance);
12
- return ruby_state;
13
- }
14
- base.instance_reducer = function(state, action) { return state; }
15
- base.preact_component = function(props) {
16
- const og = Opal.global;
17
- const oper = Opal.Preact;
18
- oper.render_buffer.push([]);
19
- // console.log("function pushed", oper.render_buffer, oper.render_buffer.toString());
20
- // Lucid functionality
21
- let context = og.PreactHooks.useContext(og.LucidApplicationContext);
22
- // prepare Ruby instance
23
- const [__ruby_state, __ruby_dispatch] = og.PreactHooks.useReducer(base.instance_reducer, null, base.instance_init);
24
- const __ruby_instance = __ruby_state.instance;
25
- __ruby_instance.props = Object.assign({}, props, context);
26
- oper.register_active_component(__ruby_instance);
27
- try {
28
- let block_result = #{`__ruby_instance`.instance_exec(&`base.render_block`)};
29
- if (block_result && block_result !== nil) { oper.render_block_result(block_result); }
30
- } catch (e) {
31
- if (oper.using_did_catch) { throw e; }
32
- else { console.error(e.message === nil ? 'error at' : e.message, e.stack); }
33
- }
34
- oper.unregister_active_component(__ruby_instance);
35
- // console.log("function popping", oper.render_buffer, oper.render_buffer.toString());
36
- let result = oper.render_buffer.pop();
37
- return (result.length === 1) ? result[0] : result;
38
- };
39
- base.preact_component.displayName = #{component_name};
1
+ module LucidFunc::NativeComponentConstructor
2
+ def self.extended(base)
3
+ component_name = base.to_s
4
+ %x{
5
+ base.css_styles = null;
6
+ base.instance_init = function(initial) {
7
+ let ruby_state = { instance: #{base.new(`{}`)} };
8
+ ruby_state.instance.__ruby_instance = ruby_state.instance;
9
+ ruby_state.instance.data_access = function() { return this.props.iso_store; }
10
+ ruby_state.instance.data_access.bind(ruby_state.instance);
11
+ return ruby_state;
40
12
  }
13
+ base.instance_reducer = function(state, action) { return state; }
14
+ base.preact_component = function(props) {
15
+ const og = Opal.global;
16
+ const oper = Opal.Preact;
17
+ oper.render_buffer.push([]);
18
+ // console.log("function pushed", oper.render_buffer, oper.render_buffer.toString());
19
+ // Lucid functionality
20
+ let context = og.PreactHooks.useContext(og.LucidApplicationContext);
21
+ // prepare Ruby instance
22
+ const [__ruby_state, __ruby_dispatch] = og.PreactHooks.useReducer(base.instance_reducer, null, base.instance_init);
23
+ const __ruby_instance = __ruby_state.instance;
24
+ __ruby_instance.props = Object.assign({}, props, context);
25
+ oper.register_active_component(__ruby_instance);
26
+ try {
27
+ let block_result = #{`__ruby_instance`.instance_exec(&`base.render_block`)};
28
+ if (block_result && block_result !== nil) { oper.render_block_result(block_result); }
29
+ } catch (e) {
30
+ if (oper.using_did_catch) { throw e; }
31
+ else { console.error(e.message === nil ? 'error at' : e.message, e.stack); }
32
+ }
33
+ oper.unregister_active_component(__ruby_instance);
34
+ // console.log("function popping", oper.render_buffer, oper.render_buffer.toString());
35
+ let result = oper.render_buffer.pop();
36
+ return (result.length === 1) ? result[0] : result;
37
+ };
38
+ base.preact_component.displayName = #{component_name};
39
+ }
41
40
 
42
- base_module = base.to_s.deconstantize
43
- if base_module != ''
44
- base_module.constantize.define_singleton_method(base.to_s.demodulize) do |*args, &block|
45
- `Opal.Preact.internal_prepare_args_and_render(#{base}.preact_component, args, block)`
46
- end
47
- else
48
- Object.define_method(base.to_s) do |*args, &block|
49
- `Opal.Preact.internal_prepare_args_and_render(#{base}.preact_component, args, block)`
50
- end
41
+ base_module = base.to_s.deconstantize
42
+ if base_module != ''
43
+ base_module.constantize.define_singleton_method(base.to_s.demodulize) do |*args, &block|
44
+ `Opal.Preact.internal_prepare_args_and_render(#{base}.preact_component, args, block)`
51
45
  end
52
-
53
- def render(&block)
54
- `base.render_block = #{block}`
46
+ else
47
+ Object.define_method(base.to_s) do |*args, &block|
48
+ `Opal.Preact.internal_prepare_args_and_render(#{base}.preact_component, args, block)`
55
49
  end
56
50
  end
51
+
52
+ def render(&block)
53
+ `base.render_block = #{block}`
54
+ end
57
55
  end
58
56
  end
@@ -1,123 +1,119 @@
1
- module Preact
2
- module FunctionComponent
3
- module Api
4
- def props
5
- @native_props
6
- end
1
+ module Preact::FunctionComponent::Api
2
+ def props
3
+ @native_props
4
+ end
7
5
 
8
- def use_callback(*deps, &block)
9
- `Opal.global.PreactHooks.useCallback(function() { #{block.call} }, deps)`
10
- end
6
+ def use_callback(*deps, &block)
7
+ `Opal.global.PreactHooks.useCallback(function() { #{block.call} }, deps)`
8
+ end
11
9
 
12
- def use_context(context)
13
- native_context = `(typeof context.$is_wrapped_context !== 'undefined')` ? context.to_n : context
14
- `Opal.global.PreactHooks.useContext(native_context)`
15
- end
10
+ def use_context(context)
11
+ native_context = `(typeof context.$is_wrapped_context !== 'undefined')` ? context.to_n : context
12
+ `Opal.global.PreactHooks.useContext(native_context)`
13
+ end
16
14
 
17
- def use_debug_value(value, formatter)
18
- formatter = `null` unless formatter
19
- `Opal.global.PreactHooks.useDebugValue(value, formatter)`
20
- end
15
+ def use_debug_value(value, formatter)
16
+ formatter = `null` unless formatter
17
+ `Opal.global.PreactHooks.useDebugValue(value, formatter)`
18
+ end
21
19
 
22
- def use_effect(*args, &block)
23
- `Opal.global.PreactHooks.useEffect(function() { #{block.call} }, args)`
24
- end
20
+ def use_effect(*args, &block)
21
+ `Opal.global.PreactHooks.useEffect(function() { #{block.call} }, args)`
22
+ end
25
23
 
26
- def use_error_boundary(&block)
27
- error = nil
28
- reset_error = nil
29
- %x{
30
- let _error;
31
- let _reset_error;
32
- if (block) {
33
- [_error, reset_error] = Opal.global.PreactHooks.useErrorBoundary(function() { #{block.call(Error(_error))} });
34
- } else {
35
- [_error, reset_error] = Opal.global.PreactHooks.useErrorBoundary();
36
- }
37
- error = #{Error(e)};
38
- }
39
- [error, reset_error]
40
- end
24
+ def use_error_boundary(&block)
25
+ error = nil
26
+ reset_error = nil
27
+ %x{
28
+ let _error;
29
+ let _reset_error;
30
+ if (block) {
31
+ [_error, reset_error] = Opal.global.PreactHooks.useErrorBoundary(function() { #{block.call(Error(_error))} });
32
+ } else {
33
+ [_error, reset_error] = Opal.global.PreactHooks.useErrorBoundary();
34
+ }
35
+ error = #{Error(e)};
36
+ }
37
+ [error, reset_error]
38
+ end
41
39
 
42
- def use_imperative_handle(ruby_ref, *args, &block)
43
- ref = ruby_ref.to_n
44
- args = `null` if args.empty?
45
- `Opal.global.PreactHooks.useImperativeHandle(ref, function() { #{block.call} }, args)`
46
- end
40
+ def use_imperative_handle(ruby_ref, *args, &block)
41
+ ref = ruby_ref.to_n
42
+ args = `null` if args.empty?
43
+ `Opal.global.PreactHooks.useImperativeHandle(ref, function() { #{block.call} }, args)`
44
+ end
47
45
 
48
- def use_layout_effect(&block)
49
- `Opal.global.PreactHooks.useLayoutEffect(function() { #{block.call} })`
50
- end
46
+ def use_layout_effect(&block)
47
+ `Opal.global.PreactHooks.useLayoutEffect(function() { #{block.call} })`
48
+ end
51
49
 
52
- def use_memo(*deps, &block)
53
- `Opal.global.PreactHooks.useMemo(function() { #{block.call} }, deps)`
54
- end
50
+ def use_memo(*deps, &block)
51
+ `Opal.global.PreactHooks.useMemo(function() { #{block.call} }, deps)`
52
+ end
55
53
 
56
- def use_reducer(inital_state, &block)
57
- state = nil
58
- dispatcher = nil
59
- %x{
60
- [state, dispatcher] = Opal.global.PreactHooks.useReducer(function(state, action) {
61
- #{block.call(state, action)}
62
- }, initial_state);
63
- }
64
- [state, proc { |arg| `dispatcher(arg)` }]
65
- end
54
+ def use_reducer(inital_state, &block)
55
+ state = nil
56
+ dispatcher = nil
57
+ %x{
58
+ [state, dispatcher] = Opal.global.PreactHooks.useReducer(function(state, action) {
59
+ #{block.call(state, action)}
60
+ }, initial_state);
61
+ }
62
+ [state, proc { |arg| `dispatcher(arg)` }]
63
+ end
66
64
 
67
- def use_ref(native_ref)
68
- Preact::Ref.new(`Opal.global.PreactHooks.useRef(native_ref)`)
69
- end
65
+ def use_ref(native_ref)
66
+ Preact::Ref.new(`Opal.global.PreactHooks.useRef(native_ref)`)
67
+ end
70
68
 
71
- def use_state(initial_value)
72
- initial = nil
73
- setter = nil
74
- `[initial, setter] = Opal.global.PreactHooks.useState(initial_value);`
75
- [initial, proc { |arg| `setter(arg)` }]
76
- end
69
+ def use_state(initial_value)
70
+ initial = nil
71
+ setter = nil
72
+ `[initial, setter] = Opal.global.PreactHooks.useState(initial_value);`
73
+ [initial, proc { |arg| `setter(arg)` }]
74
+ end
77
75
 
78
- def get_preact_element(arg, &block)
79
- if block_given?
80
- # execute block, fetch last element from buffer
81
- %x{
82
- let last_buffer_length = Opal.Preact.render_buffer[Opal.Preact.render_buffer.length - 1].length;
83
- let last_buffer_element = Opal.Preact.render_buffer[Opal.Preact.render_buffer.length - 1][last_buffer_length - 1];
84
- block.$call();
85
- // console.log("get_preact_element popping", Opal.Preact.render_buffer, Opal.Preact.render_buffer.toString())
86
- let new_element = Opal.Preact.render_buffer[Opal.Preact.render_buffer.length - 1].pop();
87
- if (last_buffer_element === new_element) { #{Isomorfeus.raise_error(message: "Block did not create any Preact element!")} }
88
- return new_element;
89
- }
90
- else
91
- # element was rendered before being passed as arg
92
- # fetch last element from buffer
93
- # `console.log("get_preact_element popping", Opal.Preact.render_buffer, Opal.Preact.render_buffer.toString())`
94
- `Opal.Preact.render_buffer[Opal.Preact.render_buffer.length - 1].pop()`
95
- end
96
- end
97
- alias gpe get_preact_element
76
+ def get_preact_element(arg, &block)
77
+ if block_given?
78
+ # execute block, fetch last element from buffer
79
+ %x{
80
+ let last_buffer_length = Opal.Preact.render_buffer[Opal.Preact.render_buffer.length - 1].length;
81
+ let last_buffer_element = Opal.Preact.render_buffer[Opal.Preact.render_buffer.length - 1][last_buffer_length - 1];
82
+ block.$call();
83
+ // console.log("get_preact_element popping", Opal.Preact.render_buffer, Opal.Preact.render_buffer.toString())
84
+ let new_element = Opal.Preact.render_buffer[Opal.Preact.render_buffer.length - 1].pop();
85
+ if (last_buffer_element === new_element) { #{Isomorfeus.raise_error(message: "Block did not create any Preact element!")} }
86
+ return new_element;
87
+ }
88
+ else
89
+ # element was rendered before being passed as arg
90
+ # fetch last element from buffer
91
+ # `console.log("get_preact_element popping", Opal.Preact.render_buffer, Opal.Preact.render_buffer.toString())`
92
+ `Opal.Preact.render_buffer[Opal.Preact.render_buffer.length - 1].pop()`
93
+ end
94
+ end
95
+ alias gpe get_preact_element
98
96
 
99
- def render_preact_element(el)
100
- # push el to buffer
101
- `Opal.Preact.render_buffer[Opal.Preact.render_buffer.length - 1].push(el)`
102
- # `console.log("render_preact_element pushed", Opal.Preact.render_buffer, Opal.Preact.render_buffer.toString())`
103
- nil
104
- end
105
- alias rpe render_preact_element
97
+ def render_preact_element(el)
98
+ # push el to buffer
99
+ `Opal.Preact.render_buffer[Opal.Preact.render_buffer.length - 1].push(el)`
100
+ # `console.log("render_preact_element pushed", Opal.Preact.render_buffer, Opal.Preact.render_buffer.toString())`
101
+ nil
102
+ end
103
+ alias rpe render_preact_element
106
104
 
107
- def method_ref(method_symbol, *args)
108
- method_key = "#{method_symbol}#{args}"
109
- %x{
110
- if (#{self}.method_refs && #{self}.method_refs[#{method_key}]) { return #{self}.method_refs[#{method_key}]; }
111
- if (!#{self}.method_refs) { #{self}.method_refs = {}; }
112
- #{self}.method_refs[#{method_key}] = { m: #{method(method_symbol)}, a: args };
113
- return #{self}.method_refs[#{method_key}];
114
- }
115
- end
116
- alias m_ref method_ref
105
+ def method_ref(method_symbol, *args)
106
+ method_key = "#{method_symbol}#{args}"
107
+ %x{
108
+ if (#{self}.method_refs && #{self}.method_refs[#{method_key}]) { return #{self}.method_refs[#{method_key}]; }
109
+ if (!#{self}.method_refs) { #{self}.method_refs = {}; }
110
+ #{self}.method_refs[#{method_key}] = { m: #{method(method_symbol)}, a: args };
111
+ return #{self}.method_refs[#{method_key}];
112
+ }
113
+ end
114
+ alias m_ref method_ref
117
115
 
118
- def to_n
119
- self
120
- end
121
- end
116
+ def to_n
117
+ self
122
118
  end
123
119
  end
@@ -1,9 +1,7 @@
1
- module Preact
2
- module FunctionComponent
3
- class Base
4
- def self.inherited(base)
5
- base.include(::Preact::FunctionComponent::Mixin)
6
- end
1
+ module Preact::FunctionComponent
2
+ class Base
3
+ def self.inherited(base)
4
+ base.include(::Preact::FunctionComponent::Mixin)
7
5
  end
8
6
  end
9
- end
7
+ end
@@ -1,10 +1,6 @@
1
- module Preact
2
- module FunctionComponent
3
- module Initializer
4
- def initialize
5
- self.JS[:native_props] = `{ props: null }`
6
- @native_props = `Opal.Preact.Props.$new(self)`
7
- end
8
- end
1
+ module Preact::FunctionComponent::Initializer
2
+ def initialize
3
+ self.JS[:native_props] = `{ props: null }`
4
+ @native_props = `Opal.Preact.Props.$new(self)`
9
5
  end
10
6
  end
@@ -1,12 +1,8 @@
1
- module Preact
2
- module FunctionComponent
3
- module Mixin
4
- def self.included(base)
5
- base.include(::Preact::Elements)
6
- base.include(::Preact::FunctionComponent::Initializer)
7
- base.include(::Preact::FunctionComponent::Api)
8
- base.extend(::Preact::FunctionComponent::NativeComponentConstructor)
9
- end
10
- end
1
+ module Preact::FunctionComponent::Mixin
2
+ def self.included(base)
3
+ base.include(::Preact::Elements)
4
+ base.include(::Preact::FunctionComponent::Initializer)
5
+ base.include(::Preact::FunctionComponent::Api)
6
+ base.extend(::Preact::FunctionComponent::NativeComponentConstructor)
11
7
  end
12
8
  end