Skip to content

Commit a07734a

Browse files
committed
rename all component instance variables to begin with __...
1 parent e58c6f3 commit a07734a

File tree

6 files changed

+60
-27
lines changed

6 files changed

+60
-27
lines changed

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def deprecation_warning(message)
4747
end
4848

4949
def initialize(native_element)
50-
@native = native_element
50+
@__hyperstack_component_native = native_element
5151
end
5252

5353
def emit(event_name, *args)
@@ -59,6 +59,7 @@ def emit(event_name, *args)
5959
end
6060

6161
def component_will_mount
62+
@__hyperstack_component_params_wrapper = self.class.props_wrapper.new(self)
6263
IsomorphicHelpers.load_context(true) if IsomorphicHelpers.on_opal_client?
6364
observing(immediate_update: true) do
6465
Hyperstack::Internal::Component.mounted_components << self
@@ -73,6 +74,7 @@ def component_did_mount
7374
end
7475

7576
def component_will_receive_props(next_props)
77+
@__hyperstack_component_params_wrapper.reload
7678
# need to rethink how this works in opal-react, or if its actually that useful within the react.rb environment
7779
# for now we are just using it to clear processed_params
7880
observing(immediate_update: true) { run_callback(:before_receive_props, next_props) }
@@ -101,8 +103,6 @@ def component_did_catch(error, info)
101103
observing { run_callback(:after_error, error, info) }
102104
end
103105

104-
attr_reader :waiting_on_resources
105-
106106
def mutations(_objects)
107107
# if we have to we may have to require that all objects respond to a "name" method (see legacy method update_react_js_state below)
108108
set_state('***_state_updated_at-***' => `Date.now() + Math.random()`)
@@ -128,10 +128,14 @@ def render
128128
raise 'no render defined'
129129
end unless method_defined?(:render)
130130

131+
def waiting_on_resources
132+
@__hyperstack_component_waiting_on_resources
133+
end
134+
131135
def _render_wrapper
132136
observing(rendering: true) do
133137
element = Hyperstack::Internal::Component::RenderingContext.render(nil) { render || '' }
134-
@waiting_on_resources =
138+
@__hyperstack_component_waiting_on_resources =
135139
element.waiting_on_resources if element.respond_to? :waiting_on_resources
136140
element
137141
end

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ module Component
1515
class NativeLibrary
1616
class << self
1717
def imports(native_name)
18-
@native_prefix = "#{native_name}."
18+
@__hyperstack_component_native_prefix = "#{native_name}."
1919
self
2020
end
2121

@@ -66,7 +66,7 @@ def lookup_native_name(js_name)
6666
end
6767

6868
def scope_native_name(js_name)
69-
"#{@native_prefix}#{js_name}"
69+
"#{@__hyperstack_component_native_prefix}#{js_name}"
7070
end
7171

7272
def create_component_wrapper(klass, native_name, ruby_name)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module Internal
88
module Component
99
class << self
1010
def mounted_components
11-
@mounted_components ||= Set.new
11+
@__hyperstack_component_mounted_components ||= Set.new
1212
end
1313
end
1414
end

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,31 @@ module Internal
55
module Component
66
module InstanceMethods
77
def children
8-
Hyperstack::Component::Children.new(`#{@native}.props.children`)
8+
Hyperstack::Component::Children.new(`#{@__hyperstack_component_native}.props.children`)
99
end
1010

1111
def params
12-
@params ||= self.class.props_wrapper.new(self)
12+
@__hyperstack_component_params_wrapper
1313
end
1414

1515
def props
16-
Hash.new(`#{@native}.props`)
16+
Hash.new(`#{@__hyperstack_component_native}.props`)
1717
end
1818

1919
def refs
20-
Hash.new(`#{@native}.refs`)
20+
Hash.new(`#{@__hyperstack_component_native}.refs`)
2121
end
2222

2323
def dom_node
24-
`ReactDOM.findDOMNode(#{self}.native)` # react >= v0.15.0
24+
`ReactDOM.findDOMNode(#{self}.__hyperstack_component_native)` # react >= v0.15.0
2525
end
2626

2727
def mounted?
2828
`(#{self}.is_mounted === undefined) ? false : #{self}.is_mounted`
2929
end
3030

3131
def force_update!
32-
`#{self}.native.forceUpdate()`
32+
`#{self}.__hyperstack_component_native.forceUpdate()`
3333
self
3434
end
3535

@@ -39,40 +39,40 @@ def set_state(state, &block)
3939

4040
def set_state!(state, &block)
4141
set_or_replace_state_or_prop(state, 'setState', &block)
42-
`#{self}.native.forceUpdate()`
42+
`#{self}.__hyperstack_component_native.forceUpdate()`
4343
end
4444

4545
private
4646

4747
def set_or_replace_state_or_prop(state_or_prop, method, &block)
48-
raise "No native ReactComponent associated" unless @native
48+
raise "No native ReactComponent associated" unless @__hyperstack_component_native
4949
`var state_prop_n = #{state_or_prop.shallow_to_n}`
5050
# the state object is initalized when the ruby component is instantiated
51-
# this is detected by self.native.__opalInstanceInitializedState
51+
# this is detected by self.__hyperstack_component_native.__opalInstanceInitializedState
5252
# which is set in the native component constructor in ReactWrapper
5353
# the setState update callback is not called when initalizing initial state
5454
if block
5555
%x{
56-
if (#{@native}.__opalInstanceInitializedState === true) {
57-
#{@native}[method](state_prop_n, function(){
56+
if (#{@__hyperstack_component_native}.__opalInstanceInitializedState === true) {
57+
#{@__hyperstack_component_native}[method](state_prop_n, function(){
5858
block.$call();
5959
});
6060
} else {
6161
for (var sp in state_prop_n) {
6262
if (state_prop_n.hasOwnProperty(sp)) {
63-
#{@native}.state[sp] = state_prop_n[sp];
63+
#{@__hyperstack_component_native}.state[sp] = state_prop_n[sp];
6464
}
6565
}
6666
}
6767
}
6868
else
6969
%x{
70-
if (#{@native}.__opalInstanceInitializedState === true) {
71-
#{@native}[method](state_prop_n);
70+
if (#{@__hyperstack_component_native}.__opalInstanceInitializedState === true) {
71+
#{@__hyperstack_component_native}[method](state_prop_n);
7272
} else {
7373
for (var sp in state_prop_n) {
7474
if (state_prop_n.hasOwnProperty(sp)) {
75-
#{@native}.state[sp] = state_prop_n[sp];
75+
#{@__hyperstack_component_native}.state[sp] = state_prop_n[sp];
7676
}
7777
}
7878
}

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

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
module Hyperstack
22
module Internal
33
module Component
4-
54
class PropsWrapper
65
attr_reader :component
76

8-
def self.define_param(name, param_type)
7+
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
14+
end
15+
16+
def self.define_param(name, param_type, aka = nil)
17+
param_definitions[name] = [param_type, aka || name]
918
if param_type == Proc
1019
define_method("#{name}") do |*args, &block|
1120
props[name].call(*args, &block) if props[name]
@@ -37,6 +46,26 @@ def self.define_all_others(name)
3746

3847
def initialize(component)
3948
@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
64+
end
65+
end
66+
67+
def reload
68+
initialize(@component)
4069
end
4170

4271
def [](prop)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def call_needs_update(next_params, next_state)
7070
def native_state_changed?(next_state_hash)
7171
# next_state = next_state_hash.to_n
7272
# %x{
73-
# var current_state = #{@native}.state
73+
# var current_state = #{@__hyperstack_component_native}.state
7474
# var normalized_next_state =
7575
# !next_state || Object.keys(next_state).length === 0 ? false : next_state
7676
# var normalized_current_state =
@@ -84,15 +84,15 @@ def native_state_changed?(next_state_hash)
8484
# return (normalized_current_state['***_state_updated_at-***'] !=
8585
# normalized_next_state['***_state_updated_at-***'])
8686
# }
87-
state_hash = Hash.new(`#{@native}.state`)
87+
state_hash = Hash.new(`#{@__hyperstack_component_native}.state`)
8888
next_state_hash != state_hash
8989
end
9090
# rubocop:enable Metrics/MethodLength
9191

9292
# Do a shallow compare on the two hashes. Starting in 0.9 we will do a deep compare. ???
9393

9494
def props_changed?(next_props)
95-
props = Hash.new(`#{@native}.props`)
95+
props = Hash.new(`#{@__hyperstack_component_native}.props`)
9696
next_props != props
9797
end
9898
end

0 commit comments

Comments
 (0)