Skip to content
This repository was archived by the owner on Oct 19, 2018. It is now read-only.

Commit b2e04a5

Browse files
committed
no need to repeat react module when inside it
1 parent 4717858 commit b2e04a5

File tree

5 files changed

+40
-45
lines changed

5 files changed

+40
-45
lines changed

lib/react/component.rb

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module React
1313
module Component
1414
def self.included(base)
1515
base.include(API)
16-
base.include(React::Callbacks)
16+
base.include(Callbacks)
1717
base.class_eval do
1818
class_attribute :initial_state
1919
define_callback :before_mount
@@ -86,7 +86,7 @@ def method_missing(n, *args, &block)
8686
unless name && name.method_defined?(:render)
8787
return super
8888
end
89-
React::RenderingContext.build_or_render(node_only, name, *args, &block)
89+
RenderingContext.build_or_render(node_only, name, *args, &block)
9090
end
9191

9292
end
@@ -131,16 +131,16 @@ def component_will_mount
131131
IsomorphicHelpers.load_context(true) if IsomorphicHelpers.on_opal_client?
132132
@props_wrapper = self.class.props_wrapper.new(Hash.new(`#{@native}.props`))
133133
set_state! initial_state if initial_state
134-
React::State.initialize_states(self, initial_state)
135-
React::State.set_state_context_to(self) { self.run_callback(:before_mount) }
134+
State.initialize_states(self, initial_state)
135+
State.set_state_context_to(self) { self.run_callback(:before_mount) }
136136
rescue Exception => e
137137
self.class.process_exception(e, self)
138138
end
139139

140140
def component_did_mount
141-
React::State.set_state_context_to(self) do
141+
State.set_state_context_to(self) do
142142
self.run_callback(:after_mount)
143-
React::State.update_states_to_observe
143+
State.update_states_to_observe
144144
end
145145
rescue Exception => e
146146
self.class.process_exception(e, self)
@@ -149,7 +149,7 @@ def component_did_mount
149149
def component_will_receive_props(next_props)
150150
# need to rethink how this works in opal-react, or if its actually that useful within the react.rb environment
151151
# for now we are just using it to clear processed_params
152-
React::State.set_state_context_to(self) { self.run_callback(:before_receive_props, Hash.new(next_props)) }
152+
State.set_state_context_to(self) { self.run_callback(:before_receive_props, Hash.new(next_props)) }
153153
rescue Exception => e
154154
self.class.process_exception(e, self)
155155
end
@@ -160,7 +160,7 @@ def props_changed?(next_props)
160160
end
161161

162162
def should_component_update?(next_props, next_state)
163-
React::State.set_state_context_to(self) do
163+
State.set_state_context_to(self) do
164164
next_props = Hash.new(next_props)
165165
if self.respond_to?(:needs_update?)
166166
!!self.needs_update?(next_props, Hash.new(next_state))
@@ -181,25 +181,25 @@ def should_component_update?(next_props, next_state)
181181
end
182182

183183
def component_will_update(next_props, next_state)
184-
React::State.set_state_context_to(self) { self.run_callback(:before_update, Hash.new(next_props), Hash.new(next_state)) }
184+
State.set_state_context_to(self) { self.run_callback(:before_update, Hash.new(next_props), Hash.new(next_state)) }
185185
@props_wrapper = self.class.props_wrapper.new(Hash.new(next_props))
186186
rescue Exception => e
187187
self.class.process_exception(e, self)
188188
end
189189

190190
def component_did_update(prev_props, prev_state)
191-
React::State.set_state_context_to(self) do
191+
State.set_state_context_to(self) do
192192
self.run_callback(:after_update, Hash.new(prev_props), Hash.new(prev_state))
193-
React::State.update_states_to_observe
193+
State.update_states_to_observe
194194
end
195195
rescue Exception => e
196196
self.class.process_exception(e, self)
197197
end
198198

199199
def component_will_unmount
200-
React::State.set_state_context_to(self) do
200+
State.set_state_context_to(self) do
201201
self.run_callback(:before_unmount)
202-
React::State.remove
202+
State.remove
203203
end
204204
rescue Exception => e
205205
self.class.process_exception(e, self)
@@ -232,7 +232,7 @@ def method_missing(n, *args, &block)
232232
node_only = true
233233
name = name.gsub(/_as_node$/, "")
234234
end
235-
unless (React::HTML_TAGS.include?(name) || name == 'present' || name == '_p_tag' || (name = component?(name, self)))
235+
unless (HTML_TAGS.include?(name) || name == 'present' || name == '_p_tag' || (name = component?(name, self)))
236236
return super
237237
end
238238

@@ -244,21 +244,21 @@ def method_missing(n, *args, &block)
244244
name = "p"
245245
end
246246

247-
React::RenderingContext.build_or_render(node_only, name, *args, &block)
247+
RenderingContext.build_or_render(node_only, name, *args, &block)
248248
end
249249

250250
def watch(value, &on_change)
251-
React::Observable.new(value, on_change)
251+
Observable.new(value, on_change)
252252
end
253253

254254
def define_state(*args, &block)
255-
React::State.initialize_states(self, self.class.define_state(*args, &block))
255+
State.initialize_states(self, self.class.define_state(*args, &block))
256256
end
257257

258258
attr_reader :waiting_on_resources
259259

260260
def _render_wrapper
261-
React::State.set_state_context_to(self) do
261+
State.set_state_context_to(self) do
262262
RenderingContext.render(nil) {render || ""}.tap { |element| @waiting_on_resources = element.waiting_on_resources if element.respond_to? :waiting_on_resources }
263263
end
264264
rescue Exception => e

lib/react/component/base.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
require 'react/component'
2-
31
module React
42
module Component
53
class Base
64
def self.inherited(child)
7-
child.send(:include, React::Component)
5+
child.include(Component)
86
end
97
end
108
end

lib/react/component/class_methods.rb

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def export_state(*states, &block)
114114
default_initial_value = (block && block.arity == 0) ? yield : nil
115115
states_hash = (states.last.is_a?(Hash)) ? states.pop : {}
116116
states.each { |name| states_hash[name] = default_initial_value }
117-
React::State.initialize_states(self, states_hash)
117+
State.initialize_states(self, states_hash)
118118
states_hash.each do |name, initial_value|
119119
define_state_methods(self, name, self, &block)
120120
define_state_methods(singleton_class, name, self, &block)
@@ -124,27 +124,27 @@ def export_state(*states, &block)
124124
def define_state_methods(this, name, from = nil, &block)
125125
this.define_method("#{name}") do
126126
self.class.deprecation_warning "Direct access to state `#{name}`. Use `state.#{name}` instead." if from.nil? || from == this
127-
React::State.get_state(from || self, name)
127+
State.get_state(from || self, name)
128128
end
129129
this.define_method("#{name}=") do |new_state|
130130
self.class.deprecation_warning "Direct assignment to state `#{name}`. Use `#{(from && from != this) ? from : 'state'}.#{name}!` instead."
131-
yield name, React::State.get_state(from || self, name), new_state if block && block.arity > 0
132-
React::State.set_state(from || self, name, new_state)
131+
yield name, State.get_state(from || self, name), new_state if block && block.arity > 0
132+
State.set_state(from || self, name, new_state)
133133
end
134134
this.define_method("#{name}!") do |*args|
135135
self.class.deprecation_warning "Direct access to state `#{name}`. Use `state.#{name}` instead." if from.nil? or from == this
136136
if args.count > 0
137-
yield name, React::State.get_state(from || self, name), args[0] if block && block.arity > 0
138-
current_value = React::State.get_state(from || self, name)
139-
React::State.set_state(from || self, name, args[0])
137+
yield name, State.get_state(from || self, name), args[0] if block && block.arity > 0
138+
current_value = State.get_state(from || self, name)
139+
State.set_state(from || self, name, args[0])
140140
current_value
141141
else
142-
current_state = React::State.get_state(from || self, name)
143-
yield name, React::State.get_state(from || self, name), current_state if block && block.arity > 0
144-
React::State.set_state(from || self, name, current_state)
145-
React::Observable.new(current_state) do |update|
146-
yield name, React::State.get_state(from || self, name), update if block && block.arity > 0
147-
React::State.set_state(from || self, name, update)
142+
current_state = State.get_state(from || self, name)
143+
yield name, State.get_state(from || self, name), current_state if block && block.arity > 0
144+
State.set_state(from || self, name, current_state)
145+
Observable.new(current_state) do |update|
146+
yield name, State.get_state(from || self, name), update if block && block.arity > 0
147+
State.set_state(from || self, name, update)
148148
end
149149
end
150150
end

lib/react/component/props_wrapper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def self.define_param(name, param_type, owner)
77
owner.define_method("#{name}") do
88
deprecated_params_method("#{name}", *args, &block)
99
end
10-
if param_type == React::Observable
10+
if param_type == Observable
1111
owner.define_method("#{name}!") do |*args|
1212
deprecated_params_method("#{name}!", *args)
1313
end

lib/react/state.rb

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
module React
2-
32
class StateWrapper < BasicObject
4-
53
def initialize(native, from)
64
@state_hash = Hash.new(`#{native}.state`)
75
@from = from
@@ -18,23 +16,22 @@ def []=(state, new_value)
1816
def method_missing(method, *args)
1917
if match = method.match(/^(.+)\!$/)
2018
if args.count > 0
21-
current_value = React::State.get_state(@from, match[1])
22-
React::State.set_state(@from, $1, args[0])
19+
current_value = State.get_state(@from, match[1])
20+
State.set_state(@from, $1, args[0])
2321
current_value
2422
else
25-
current_state = React::State.get_state(@from, match[1])
26-
React::State.set_state(@from, $1, current_state)
27-
React::Observable.new(current_state) do |update|
28-
React::State.set_state(@from, $1, update)
23+
current_state = State.get_state(@from, match[1])
24+
State.set_state(@from, $1, current_state)
25+
Observable.new(current_state) do |update|
26+
State.set_state(@from, $1, update)
2927
end
3028
end
3129
else
32-
React::State.get_state(@from, method)
30+
State.get_state(@from, method)
3331
end
3432
end
3533
end
3634

37-
3835
class State
3936
class << self
4037
attr_reader :current_observer

0 commit comments

Comments
 (0)