Skip to content

Commit 6e9723d

Browse files
committed
moved children, react_wrapper, and mounted_components method
1 parent f6eaf9e commit 6e9723d

File tree

14 files changed

+74
-118
lines changed

14 files changed

+74
-118
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
require 'hyperstack/state/observer'
1111
require 'react/validator'
1212
require 'react/element'
13-
require 'react/api'
13+
require 'hyperstack/internal/component/react_wrapper'
1414
require 'react/component'
1515
#require 'react/component/dsl_instance_methods'
1616
require 'hyperstack/internal/component/should_component_update'

ruby/hyper-component/lib/react/children.rb renamed to ruby/hyper-component/lib/hyperstack/component/children.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def each(&block)
2222
%x{
2323
React.Children.forEach(#{@children}, function(context){
2424
#{
25-
element = Hyperstack::Component::Element.new(`context`)
25+
element = Element.new(`context`)
2626
block.call(element)
2727
collection << element
2828
}

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,13 @@ def export_component(opts = {})
149149
first_name = export_name.first
150150
Native(`Opal.global`)[first_name] = add_item_to_tree(
151151
Native(`Opal.global`)[first_name],
152-
[Hyperstack::Component::Internal::ReactWrapper.create_native_react_class(self)] + export_name[1..-1].reverse
152+
[ReactWrapper.create_native_react_class(self)] + export_name[1..-1].reverse
153153
).to_n
154154
end
155155

156156
def imports(component_name)
157-
Hyperstack::Component::Internal::ReactWrapper.import_native_component(
158-
self, Hyperstack::Component::Internal::ReactWrapper.eval_native_react_component(component_name)
157+
ReactWrapper.import_native_component(
158+
self, ReactWrapper.eval_native_react_component(component_name)
159159
)
160160
define_method(:render) {} # define a dummy render method - will never be called...
161161
rescue Exception => e # rubocop:disable Lint/RescueException : we need to catch everything!
@@ -177,7 +177,15 @@ def add_item_to_tree(current_tree, new_item)
177177
end
178178

179179
def to_n
180-
Hyperstack::Component::Internal::ReactWrapper.class_eval('@@component_classes')[self]
180+
ReactWrapper.class_eval('@@component_classes')[self]
181+
end
182+
183+
def force_update!
184+
components = Component.mounted_components.to_a
185+
components.each do |comp|
186+
next unless Component.mounted_components.include? comp
187+
comp.force_update!
188+
end
181189
end
182190
end
183191
end

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require "react/children"
1+
require "hyperstack/component/children"
22

33
module Hyperstack
44
module Internal
@@ -49,7 +49,7 @@ def set_or_replace_state_or_prop(state_or_prop, method, &block)
4949
`var state_prop_n = #{state_or_prop.shallow_to_n}`
5050
# the state object is initalized when the ruby component is instantiated
5151
# this is detected by self.native.__opalInstanceInitializedState
52-
# which is set in the native component constructor in react/api.rb
52+
# 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{

ruby/hyper-component/lib/react/api.rb renamed to ruby/hyper-component/lib/hyperstack/internal/component/react_wrapper.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
require 'react/native_library'
22

33
module Hyperstack
4-
module Component
5-
module Internal
4+
module Internal
5+
module Component
66
# contains the name of all HTML tags, and the mechanism to register a component
77
# Provides the internal mechanisms to interface between reactrb and native components
88
# the code will attempt to create a js component wrapper on any rb class that has a
@@ -167,10 +167,10 @@ def self.create_element(type, *args, &block)
167167
params << ncc
168168
elsif type.is_a?(Class)
169169
params << create_native_react_class(type)
170-
elsif block_given? || Hyperstack::Internal::Component::Tags::HTML_TAGS.include?(type)
170+
elsif block_given? || Tags::HTML_TAGS.include?(type)
171171
params << type
172172
elsif type.is_a?(String)
173-
return Element.new(type)
173+
return Hyperstack::Component::Element.new(type)
174174
else
175175
raise "#{type} not implemented"
176176
end
@@ -188,7 +188,7 @@ def self.create_element(type, *args, &block)
188188
}
189189
}
190190
end
191-
Element.new(`React.createElement.apply(null, #{params})`, type, properties, block)
191+
Hyperstack::Component::Element.new(`React.createElement.apply(null, #{params})`, type, properties, block)
192192
end
193193

194194
def self.clear_component_class_cache
@@ -221,7 +221,7 @@ def self.convert_props(args)
221221
end
222222

223223
properties['style'] = (properties['style'] || {}).merge(value)
224-
elsif ReactAPI::HASH_ATTRIBUTES.include?(key) && value.is_a?(Hash)
224+
elsif Hyperstack::Component::ReactAPI::HASH_ATTRIBUTES.include?(key) && value.is_a?(Hash)
225225
properties[key] = (properties[key] || {}).merge(value)
226226
else
227227
properties[key] = value
@@ -255,10 +255,10 @@ def self.convert_props(args)
255255
}
256256
}
257257
}
258-
elsif ReactAPI::HASH_ATTRIBUTES.include?(key) && value.is_a?(Hash)
258+
elsif Hyperstack::Component::ReactAPI::HASH_ATTRIBUTES.include?(key) && value.is_a?(Hash)
259259
value.each { |k, v| props["#{key}-#{k.gsub(/__|_/, '__' => '_', '_' => '-')}"] = v.to_n }
260260
else
261-
props[ReactAPI.html_attr?(lower_camelize(key)) ? lower_camelize(key) : key] = value
261+
props[Hyperstack::Component::ReactAPI.html_attr?(lower_camelize(key)) ? lower_camelize(key) : key] = value
262262
end
263263
end
264264
props

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ module Tags
5050
def self.html_tag_class_for(tag)
5151
downcased_tag = tag.downcase
5252
if tag =~ /[A-Z]+/ && HTML_TAGS.include?(downcased_tag)
53-
Object.const_set tag, Hyperstack::Component::Internal::ReactWrapper.create_element(downcased_tag)
53+
Object.const_set tag, ReactWrapper.create_element(downcased_tag)
5454
end
5555
end
5656

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

Lines changed: 4 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,18 @@
44
require 'hyperstack/internal/component/callbacks'
55
require 'react/rendering_context'
66
require 'react/state_wrapper'
7+
require 'hyperstack/internal/component'
78
require 'hyperstack/internal/component/instance_methods'
89
require 'hyperstack/internal/component/class_methods'
910
require 'hyperstack/internal/component/props_wrapper'
1011
module Hyperstack
1112
module Component
1213

13-
# TODO: move to its own file (i.e. component/force_update)
14-
module Internal
15-
class << self
16-
def mounted_components
17-
@mounted_components ||= Set.new
18-
end
19-
end
20-
end
21-
def self.force_update!
22-
components = Internal.mounted_components.to_a
23-
components.each do |comp|
24-
next unless Internal.mounted_components.include? comp
25-
comp.force_update!
26-
end
27-
end
28-
# end of todo
29-
3014
def self.included(base)
31-
#base.include(Hyperstack::State::Observable)
3215
base.include(Hyperstack::State::Observer)
3316
base.include(Hyperstack::Internal::Component::InstanceMethods)
3417
base.include(Hyperstack::Internal::Component::Callbacks)
3518
base.include(Hyperstack::Internal::Component::Tags)
36-
#base.include(React::Component::DslInstanceMethods)
3719
base.include(Hyperstack::Internal::Component::ShouldComponentUpdate)
3820
base.class_eval do
3921
class_attribute :initial_state
@@ -43,7 +25,7 @@ def self.included(base)
4325
define_callback :before_update
4426
define_callback :after_update
4527
define_callback :before_unmount
46-
define_callback(:after_error) { Internal::ReactWrapper.add_after_error_hook(base) }
28+
define_callback(:after_error) { Hyperstack::Internal::Component::ReactWrapper.add_after_error_hook(base) }
4729
end
4830
base.extend(Hyperstack::Internal::Component::ClassMethods)
4931
end
@@ -71,7 +53,7 @@ def emit(event_name, *args)
7153
def component_will_mount
7254
IsomorphicHelpers.load_context(true) if IsomorphicHelpers.on_opal_client?
7355
observing(immediate_update: true) do
74-
Internal.mounted_components << self
56+
Hyperstack::Internal::Component.mounted_components << self
7557
run_callback(:before_mount)
7658
end
7759
end
@@ -101,7 +83,7 @@ def component_will_unmount
10183
observing do
10284
run_callback(:before_unmount)
10385
remove
104-
Internal.mounted_components.delete self
86+
Hyperstack::Internal::Component.mounted_components.delete self
10587
end
10688
end
10789

@@ -132,10 +114,6 @@ def update_react_js_state(object, name, value)
132114
end
133115
end
134116

135-
# def set_state_synchronously?
136-
# @native.JS[:__opalInstanceSyncSetState]
137-
# end
138-
139117
def render
140118
raise 'no render defined'
141119
end unless method_defined?(:render)
@@ -148,35 +126,5 @@ def _render_wrapper
148126
element
149127
end
150128
end
151-
152-
# def watch(value, &on_change)
153-
# Store::Observable.new(value, on_change)
154-
# end
155129
end
156130
end
157-
158-
# module React
159-
# module Component
160-
# def self.included(base)
161-
# # note this is turned off during old style testing: See the spec_helper
162-
# deprecation_warning base, "The module name React::Component has been deprecated. Use Hyperloop::Component instead."
163-
# base.include Hyperloop::Component
164-
# end
165-
# def self.deprecation_warning(name, message)
166-
# @deprecation_messages ||= []
167-
# message = "Warning: Deprecated feature used in #{name}. #{message}"
168-
# unless @deprecation_messages.include? message
169-
# @deprecation_messages << message
170-
# Hyperstack::Component::IsomorphicHelpers.log message, :warning
171-
# end
172-
# end
173-
# end
174-
# module ComponentNoNotice
175-
# def self.included(base)
176-
# base.include Hyperloop::Component
177-
# end
178-
# end
179-
# end
180-
#
181-
# module React
182-
# end

ruby/hyper-component/lib/react/element.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def render(props = {}, &new_block)
4949
if props.empty?
5050
Internal::RenderingContext.render(self)
5151
else
52-
props = ReactWrapper.convert_props(props)
52+
props = Hyperstack::Internal::Component::ReactWrapper.convert_props(props)
5353
Internal::RenderingContext.render(
5454
Element.new(`React.cloneElement(#{@native}, #{props.shallow_to_n})`,
5555
type, @properties.merge(props), block)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def scope_native_name(js_name)
7070
end
7171

7272
def create_component_wrapper(klass, native_name, ruby_name)
73-
if Internal::ReactWrapper.native_react_component?(native_name)
73+
if Hyperstack::Internal::Component::ReactWrapper.native_react_component?(native_name)
7474
new_klass = klass.const_set ruby_name, Class.new
7575
new_klass.class_eval do
7676
include Component

ruby/hyper-component/lib/react/ref_callback.rb

Lines changed: 0 additions & 35 deletions
This file was deleted.

0 commit comments

Comments
 (0)