Skip to content

Commit 74245ae

Browse files
committed
added new style params are instance variables
1 parent 5077638 commit 74245ae

File tree

8 files changed

+486
-79
lines changed

8 files changed

+486
-79
lines changed

ruby/hyper-component/lib/hyperstack/component.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def component_will_mount
6363
IsomorphicHelpers.load_context(true) if IsomorphicHelpers.on_opal_client?
6464
observing(immediate_update: true) do
6565
Hyperstack::Internal::Component.mounted_components << self
66-
run_callback(:before_mount)
66+
run_callback(:before_mount, props)
6767
end
6868
end
6969

@@ -74,7 +74,6 @@ def component_did_mount
7474
end
7575

7676
def component_will_receive_props(next_props)
77-
@__hyperstack_component_params_wrapper.reload
7877
# need to rethink how this works in opal-react, or if its actually that useful within the react.rb environment
7978
# for now we are just using it to clear processed_params
8079
observing(immediate_update: true) { run_callback(:before_receive_props, next_props) }
@@ -83,7 +82,10 @@ def component_will_receive_props(next_props)
8382

8483
def component_will_update(next_props, next_state)
8584
observing { run_callback(:before_update, next_props, next_state) }
86-
params._reset_all_others_cache if @__hyperstack_component_receiving_props
85+
if @__hyperstack_component_receiving_props
86+
params._reset_all_others_cache
87+
@__hyperstack_component_params_wrapper.reload(next_props)
88+
end
8789
@__hyperstack_component_receiving_props = false
8890
end
8991

ruby/hyper-component/lib/hyperstack/internal/component/class_methods.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,13 @@ def collect_other_params_as(name)
121121
alias other_params collect_other_params_as
122122
alias others collect_other_params_as
123123

124+
def raises(name, opts = {})
125+
aka = opts[:alias] || "#{name}!"
126+
name = name =~ /^<(.+)>$/ ? name.gsub(/^<(.+)>$/, '\1') : "on_#{name}"
127+
validator.event(name)
128+
define_method(aka) { props[name]&.call }
129+
end
130+
124131
def define_state(*states, &block)
125132
deprecation_warning "'define_state' is deprecated. Use the 'state' macro to declare states."
126133
default_initial_value = (block && block.arity == 0) ? yield : nil

ruby/hyper-component/lib/hyperstack/internal/component/props_wrapper.rb

Lines changed: 37 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,88 +5,78 @@ class PropsWrapper
55
attr_reader :component
66

77
def self.param_definitions
8-
@param_definitions if @param_definitions
9-
if superclass.respond_to? :param_definitions
10-
@param_definitions = superclass.param_definitions.dup
11-
else
12-
@param_definitions = Hash.new
13-
end
8+
@param_definitions ||=
9+
if superclass.respond_to? :param_definitions
10+
superclass.param_definitions.dup
11+
else
12+
Hash.new
13+
end
1414
end
1515

1616
def self.define_param(name, param_type, aka = nil)
17-
param_definitions[name] = [param_type, aka || name]
17+
aka ||= name
18+
param_definitions[name] = ->(props) { @component.instance_variable_set :"@#{aka}", fetch_from_cache(name, param_type, props) } #[param_type, aka || name]
1819
if param_type == Proc
19-
define_method("#{name}") do |*args, &block|
20+
define_method(aka.to_sym) do |*args, &block|
2021
props[name].call(*args, &block) if props[name]
2122
end
2223
else
23-
define_method("#{name}") do
24-
fetch_from_cache(name) do
25-
if param_type.respond_to? :_react_param_conversion
26-
param_type._react_param_conversion props[name], nil
27-
elsif param_type.is_a?(Array) &&
28-
param_type[0].respond_to?(:_react_param_conversion)
29-
props[name].collect do |param|
30-
param_type[0]._react_param_conversion param, nil
31-
end
32-
else
33-
props[name]
34-
end
35-
end
24+
define_method(aka.to_sym) do
25+
fetch_from_cache(name, param_type, props)
3626
end
3727
end
3828
end
3929

4030
def self.define_all_others(name)
41-
define_method("#{name}") do
31+
param_definitions[name] = -> (props) { puts "setting @#{name} props = #{props} self = #{self}"; @component.instance_variable_set :"@#{name}", yield(props) } #[:__hyperstack_component_all_others_flag, name, block]
32+
define_method(name.to_sym) do
33+
puts "calling good ole params.#{name} props = #{props} self = #{self}"
4234
@_all_others_cache ||= yield(props)
4335
end
4436
end
4537

46-
47-
def initialize(component)
38+
def initialize(component, incoming = nil)
4839
@component = component
49-
self.class.param_definitions.each do |name, memo|
50-
param_type, aka = memo
51-
val = fetch_from_cache(name) do
52-
if param_type.respond_to? :_react_param_conversion
53-
param_type._react_param_conversion props[name], nil
54-
elsif param_type.is_a?(Array) &&
55-
param_type[0].respond_to?(:_react_param_conversion)
56-
props[name].collect do |param|
57-
param_type[0]._react_param_conversion param, nil
58-
end
59-
else
60-
props[name]
61-
end
62-
@component.instance_variable_set(:"@#{aka}", val)
63-
end
40+
self.class.param_definitions.each_value do |initializer|
41+
instance_exec(incoming || props, &initializer)
6442
end
6543
end
6644

67-
def reload
68-
initialize(@component)
45+
def reload(next_props)
46+
initialize(@component, next_props)
6947
end
7048

7149
def [](prop)
7250
props[prop]
7351
end
7452

75-
7653
def _reset_all_others_cache
7754
@_all_others_cache = nil
7855
end
7956

8057
private
8158

82-
def fetch_from_cache(name)
83-
last, value = cache[name]
84-
return value if last.equal?(props[name])
85-
yield.tap do |value|
59+
def fetch_from_cache(name, param_type, props)
60+
last, cached_value = cache[name]
61+
return cached_value if last.equal?(props[name])
62+
convert_param(name, param_type).tap do |value|
8663
cache[name] = [props[name], value]
8764
end
8865
end
8966

67+
def convert_param(name, param_type)
68+
if param_type.respond_to? :_react_param_conversion
69+
param_type._react_param_conversion props[name], nil
70+
elsif param_type.is_a?(Array) &&
71+
param_type[0].respond_to?(:_react_param_conversion)
72+
props[name].collect do |param|
73+
param_type[0]._react_param_conversion param, nil
74+
end
75+
else
76+
props[name]
77+
end
78+
end
79+
9080
def cache
9181
@cache ||= Hash.new { |h, k| h[k] = [] }
9282
end
@@ -96,7 +86,7 @@ def props
9686
end
9787

9888
def value_for(name)
99-
self[name].instance_variable_get("@value") if self[name]
89+
self[name].instance_variable_get('@value') if self[name]
10090
end
10191
end
10292
end

ruby/hyper-component/lib/hyperstack/internal/component/validator.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ def optional(name, options = {})
3838
define_rule(name, options)
3939
end
4040

41+
def event(name)
42+
rules[name] = coerce_native_hash_values(default: nil)
43+
end
44+
4145
def all_other_params(name)
4246
@allow_undefined_props = true
4347
props_wrapper.define_all_others(name) { |props| props.reject { |name, value| rules[name] } }
@@ -77,7 +81,7 @@ def rules
7781

7882
def define_rule(name, options = {})
7983
rules[name] = coerce_native_hash_values(options)
80-
props_wrapper.define_param(name, options[:type])
84+
props_wrapper.define_param(name, options[:type], options[:alias])
8185
end
8286

8387
def errors

0 commit comments

Comments
 (0)