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.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/lib/isomorfeus/preact_view_helper.rb +18 -16
- data/lib/isomorfeus-preact.rb +15 -0
- data/lib/isomorfeus_preact/lucid_app/api.rb +30 -32
- data/lib/isomorfeus_preact/lucid_app/mixin.rb +12 -13
- data/lib/isomorfeus_preact/lucid_app/native_component_constructor.rb +88 -90
- data/lib/isomorfeus_preact/lucid_component/api.rb +95 -98
- data/lib/isomorfeus_preact/lucid_component/initializer.rb +7 -10
- data/lib/isomorfeus_preact/lucid_component/mixin.rb +11 -12
- data/lib/isomorfeus_preact/lucid_component/native_component_constructor.rb +77 -79
- data/lib/isomorfeus_preact/lucid_func/initializer.rb +6 -9
- data/lib/isomorfeus_preact/lucid_func/mixin.rb +8 -9
- data/lib/isomorfeus_preact/lucid_func/native_component_constructor.rb +49 -51
- data/lib/isomorfeus_preact/preact/function_component/api.rb +102 -106
- data/lib/isomorfeus_preact/preact/function_component/base.rb +5 -7
- data/lib/isomorfeus_preact/preact/function_component/initializer.rb +4 -8
- data/lib/isomorfeus_preact/preact/function_component/mixin.rb +6 -10
- data/lib/isomorfeus_preact/preact/function_component/native_component_constructor.rb +41 -45
- data/lib/lucid_app/context.rb +1 -1
- data/lib/nano_css.rb +10 -10
- data/lib/preact/component/api.rb +99 -103
- data/lib/preact/component/base.rb +5 -7
- data/lib/preact/component/callbacks.rb +99 -103
- data/lib/preact/component/initializer.rb +5 -9
- data/lib/preact/component/mixin.rb +9 -13
- data/lib/preact/component/native_component_constructor.rb +67 -71
- data/lib/preact/component_resolution.rb +78 -80
- data/lib/preact/elements.rb +55 -57
- data/lib/preact/version.rb +1 -1
- data/node_modules/.package-lock.json +3 -3
- data/node_modules/preact/compat/LICENSE +21 -0
- data/node_modules/preact/compat/jsx-dev-runtime.js +2 -0
- data/node_modules/preact/compat/jsx-dev-runtime.mjs +2 -0
- data/node_modules/preact/compat/jsx-runtime.js +2 -0
- data/node_modules/preact/compat/jsx-runtime.mjs +2 -0
- data/node_modules/preact/debug/LICENSE +21 -0
- data/node_modules/preact/devtools/LICENSE +21 -0
- data/node_modules/preact/devtools/dist/devtools.js +1 -1
- data/node_modules/preact/devtools/dist/devtools.js.map +1 -1
- data/node_modules/preact/devtools/dist/devtools.mjs +1 -1
- data/node_modules/preact/devtools/dist/devtools.module.js +1 -1
- data/node_modules/preact/devtools/dist/devtools.module.js.map +1 -1
- data/node_modules/preact/devtools/dist/devtools.umd.js +1 -1
- data/node_modules/preact/devtools/dist/devtools.umd.js.map +1 -1
- data/node_modules/preact/devtools/src/devtools.js +1 -1
- data/node_modules/preact/dist/preact.js +1 -1
- data/node_modules/preact/dist/preact.js.map +1 -1
- data/node_modules/preact/dist/preact.min.js +1 -1
- data/node_modules/preact/dist/preact.min.js.map +1 -1
- data/node_modules/preact/dist/preact.mjs +1 -1
- data/node_modules/preact/dist/preact.module.js +1 -1
- data/node_modules/preact/dist/preact.module.js.map +1 -1
- data/node_modules/preact/dist/preact.umd.js +1 -1
- data/node_modules/preact/dist/preact.umd.js.map +1 -1
- data/node_modules/preact/hooks/LICENSE +21 -0
- data/node_modules/preact/jsx-runtime/LICENSE +21 -0
- data/node_modules/preact/package.json +1 -1
- data/node_modules/preact/src/diff/index.js +6 -3
- data/package.json +1 -1
- metadata +16 -12
- data/lib/isomorfeus_preact/lucid_component/instance_store_proxy.rb +0 -44
@@ -1,12 +1,9 @@
|
|
1
|
-
module LucidComponent
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
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
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
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
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
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
|
-
|
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
|
-
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
|
-
|
56
|
-
const oper = Opal.Preact;
|
57
|
-
oper.render_buffer.push([]);
|
45
|
+
if (base.preload_block) {
|
58
46
|
oper.register_active_component(this);
|
59
|
-
|
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
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
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
|
-
|
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
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
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
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
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
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
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
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
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
|
-
|
54
|
-
`base.
|
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
|
-
|
3
|
-
|
4
|
-
|
5
|
-
@native_props
|
6
|
-
end
|
1
|
+
module Preact::FunctionComponent::Api
|
2
|
+
def props
|
3
|
+
@native_props
|
4
|
+
end
|
7
5
|
|
8
|
-
|
9
|
-
|
10
|
-
|
6
|
+
def use_callback(*deps, &block)
|
7
|
+
`Opal.global.PreactHooks.useCallback(function() { #{block.call} }, deps)`
|
8
|
+
end
|
11
9
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
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
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
15
|
+
def use_debug_value(value, formatter)
|
16
|
+
formatter = `null` unless formatter
|
17
|
+
`Opal.global.PreactHooks.useDebugValue(value, formatter)`
|
18
|
+
end
|
21
19
|
|
22
|
-
|
23
|
-
|
24
|
-
|
20
|
+
def use_effect(*args, &block)
|
21
|
+
`Opal.global.PreactHooks.useEffect(function() { #{block.call} }, args)`
|
22
|
+
end
|
25
23
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
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
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
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
|
-
|
49
|
-
|
50
|
-
|
46
|
+
def use_layout_effect(&block)
|
47
|
+
`Opal.global.PreactHooks.useLayoutEffect(function() { #{block.call} })`
|
48
|
+
end
|
51
49
|
|
52
|
-
|
53
|
-
|
54
|
-
|
50
|
+
def use_memo(*deps, &block)
|
51
|
+
`Opal.global.PreactHooks.useMemo(function() { #{block.call} }, deps)`
|
52
|
+
end
|
55
53
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
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
|
-
|
68
|
-
|
69
|
-
|
65
|
+
def use_ref(native_ref)
|
66
|
+
Preact::Ref.new(`Opal.global.PreactHooks.useRef(native_ref)`)
|
67
|
+
end
|
70
68
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
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
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
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
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
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
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
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
|
-
|
119
|
-
|
120
|
-
end
|
121
|
-
end
|
116
|
+
def to_n
|
117
|
+
self
|
122
118
|
end
|
123
119
|
end
|
@@ -1,9 +1,7 @@
|
|
1
|
-
module Preact
|
2
|
-
|
3
|
-
|
4
|
-
|
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
|
-
|
3
|
-
|
4
|
-
|
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
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
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
|