@@ -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
0 commit comments