|
3 | 3 | require 'active_support/core_ext/class/attribute' |
4 | 4 | require 'react/callbacks' |
5 | 5 | require 'react/rendering_context' |
6 | | -require 'react/observable' |
7 | | -require 'react/state' |
| 6 | +require 'hyper-store' |
| 7 | +require 'react/state_wrapper' |
8 | 8 | require 'react/component/api' |
9 | 9 | require 'react/component/class_methods' |
10 | 10 | require 'react/component/props_wrapper' |
11 | 11 | require 'native' |
12 | 12 |
|
13 | | -module React |
14 | | - module Component |
15 | | - def self.included(base) |
16 | | - base.include(API) |
17 | | - base.include(Callbacks) |
18 | | - base.include(Tags) |
19 | | - base.include(DslInstanceMethods) |
20 | | - base.include(ShouldComponentUpdate) |
21 | | - base.class_eval do |
22 | | - class_attribute :initial_state |
23 | | - define_callback :before_mount |
24 | | - define_callback :after_mount |
25 | | - define_callback :before_receive_props |
26 | | - define_callback :before_update |
27 | | - define_callback :after_update |
28 | | - define_callback :before_unmount |
29 | | - end |
30 | | - base.extend(ClassMethods) |
31 | | - end |
| 13 | +module Hyperloop |
| 14 | + class Component |
| 15 | + module Mixin |
| 16 | + def self.included(base) |
| 17 | + base.include(Store::Mixin) |
| 18 | + base.include(React::Component::API) |
| 19 | + base.include(React::Component::Callbacks) |
| 20 | + base.include(React::Component::Tags) |
| 21 | + base.include(React::Component::DslInstanceMethods) |
| 22 | + base.include(React::Component::ShouldComponentUpdate) |
| 23 | + base.class_eval do |
| 24 | + class_attribute :initial_state |
| 25 | + define_callback :before_mount |
| 26 | + define_callback :after_mount |
| 27 | + define_callback :before_receive_props |
| 28 | + define_callback :before_update |
| 29 | + define_callback :after_update |
| 30 | + define_callback :before_unmount |
| 31 | + end |
| 32 | + base.extend(React::Component::ClassMethods) |
| 33 | + end |
32 | 34 |
|
33 | | - def self.deprecation_warning(message) |
34 | | - @deprecation_messages ||= [] |
35 | | - message = "Warning: Deprecated feature used in #{name}. #{message}" |
36 | | - unless @deprecation_messages.include? message |
37 | | - @deprecation_messages << message |
38 | | - IsomorphicHelpers.log message, :warning |
| 35 | + def self.deprecation_warning(message) |
| 36 | + React::Component.deprecation_warning(name, message) |
39 | 37 | end |
40 | | - end |
41 | 38 |
|
42 | | - def initialize(native_element) |
43 | | - @native = native_element |
44 | | - end |
| 39 | + def deprecation_warning(message) |
| 40 | + React::Component.deprecation_warning(self.class.name, message) |
| 41 | + end |
45 | 42 |
|
46 | | - def emit(event_name, *args) |
47 | | - params["_on#{event_name.to_s.event_camelize}"].call(*args) |
48 | | - end |
| 43 | + def initialize(native_element) |
| 44 | + @native = native_element |
| 45 | + init_store |
| 46 | + end |
49 | 47 |
|
50 | | - def component_will_mount |
51 | | - IsomorphicHelpers.load_context(true) if IsomorphicHelpers.on_opal_client? |
52 | | - set_state! initial_state if initial_state |
53 | | - State.initialize_states(self, initial_state) |
54 | | - State.set_state_context_to(self) { run_callback(:before_mount) } |
55 | | - rescue Exception => e |
56 | | - self.class.process_exception(e, self) |
57 | | - end |
| 48 | + def emit(event_name, *args) |
| 49 | + params["_on#{event_name.to_s.event_camelize}"].call(*args) |
| 50 | + end |
58 | 51 |
|
59 | | - def component_did_mount |
60 | | - State.set_state_context_to(self) do |
61 | | - run_callback(:after_mount) |
62 | | - State.update_states_to_observe |
| 52 | + def component_will_mount |
| 53 | + React::IsomorphicHelpers.load_context(true) if React::IsomorphicHelpers.on_opal_client? |
| 54 | + # set_state! initial_state if initial_state |
| 55 | + # State.initialize_states(self, initial_state) |
| 56 | + React::State.set_state_context_to(self) { run_callback(:before_mount) } |
| 57 | + rescue Exception => e |
| 58 | + self.class.process_exception(e, self) |
63 | 59 | end |
64 | | - rescue Exception => e |
65 | | - self.class.process_exception(e, self) |
66 | | - end |
67 | 60 |
|
68 | | - def component_will_receive_props(next_props) |
69 | | - # need to rethink how this works in opal-react, or if its actually that useful within the react.rb environment |
70 | | - # for now we are just using it to clear processed_params |
71 | | - State.set_state_context_to(self) { self.run_callback(:before_receive_props, Hash.new(next_props)) } |
72 | | - rescue Exception => e |
73 | | - self.class.process_exception(e, self) |
74 | | - end |
| 61 | + def component_did_mount |
| 62 | + React::State.set_state_context_to(self) do |
| 63 | + run_callback(:after_mount) |
| 64 | + React::State.update_states_to_observe |
| 65 | + end |
| 66 | + rescue Exception => e |
| 67 | + self.class.process_exception(e, self) |
| 68 | + end |
75 | 69 |
|
76 | | - def component_will_update(next_props, next_state) |
77 | | - State.set_state_context_to(self) { self.run_callback(:before_update, Hash.new(next_props), Hash.new(next_state)) } |
78 | | - rescue Exception => e |
79 | | - self.class.process_exception(e, self) |
80 | | - end |
| 70 | + def component_will_receive_props(next_props) |
| 71 | + # need to rethink how this works in opal-react, or if its actually that useful within the react.rb environment |
| 72 | + # for now we are just using it to clear processed_params |
| 73 | + React::State.set_state_context_to(self) { self.run_callback(:before_receive_props, Hash.new(next_props)) } |
| 74 | + rescue Exception => e |
| 75 | + self.class.process_exception(e, self) |
| 76 | + end |
81 | 77 |
|
82 | | - def component_did_update(prev_props, prev_state) |
83 | | - State.set_state_context_to(self) do |
84 | | - self.run_callback(:after_update, Hash.new(prev_props), Hash.new(prev_state)) |
85 | | - State.update_states_to_observe |
| 78 | + def component_will_update(next_props, next_state) |
| 79 | + React::State.set_state_context_to(self) { self.run_callback(:before_update, Hash.new(next_props), Hash.new(next_state)) } |
| 80 | + rescue Exception => e |
| 81 | + self.class.process_exception(e, self) |
86 | 82 | end |
87 | | - rescue Exception => e |
88 | | - self.class.process_exception(e, self) |
89 | | - end |
90 | 83 |
|
91 | | - def component_will_unmount |
92 | | - State.set_state_context_to(self) do |
93 | | - self.run_callback(:before_unmount) |
94 | | - State.remove |
| 84 | + def component_did_update(prev_props, prev_state) |
| 85 | + React::State.set_state_context_to(self) do |
| 86 | + self.run_callback(:after_update, Hash.new(prev_props), Hash.new(prev_state)) |
| 87 | + React::State.update_states_to_observe |
| 88 | + end |
| 89 | + rescue Exception => e |
| 90 | + self.class.process_exception(e, self) |
95 | 91 | end |
96 | | - rescue Exception => e |
97 | | - self.class.process_exception(e, self) |
98 | | - end |
99 | 92 |
|
100 | | - attr_reader :waiting_on_resources |
| 93 | + def component_will_unmount |
| 94 | + React::State.set_state_context_to(self) do |
| 95 | + self.run_callback(:before_unmount) |
| 96 | + React::State.remove |
| 97 | + end |
| 98 | + rescue Exception => e |
| 99 | + self.class.process_exception(e, self) |
| 100 | + end |
101 | 101 |
|
102 | | - def update_react_js_state(object, name, value) |
103 | | - if object |
104 | | - name = "#{object.class}.#{name}" unless object == self |
105 | | - set_state( |
106 | | - '***_state_updated_at-***' => Time.now.to_f, |
107 | | - name => value |
108 | | - ) |
109 | | - else |
110 | | - set_state name => value |
| 102 | + attr_reader :waiting_on_resources |
| 103 | + |
| 104 | + def update_react_js_state(object, name, value) |
| 105 | + if object |
| 106 | + name = "#{object.class}.#{name}" unless object == self |
| 107 | + set_state( |
| 108 | + '***_state_updated_at-***' => Time.now.to_f, |
| 109 | + name => value |
| 110 | + ) |
| 111 | + else |
| 112 | + set_state name => value |
| 113 | + end |
111 | 114 | end |
112 | | - end |
113 | 115 |
|
114 | | - def render |
115 | | - raise 'no render defined' |
116 | | - end unless method_defined?(:render) |
117 | | - |
118 | | - def _render_wrapper |
119 | | - State.set_state_context_to(self, true) do |
120 | | - element = React::RenderingContext.render(nil) { render || '' } |
121 | | - @waiting_on_resources = |
122 | | - element.waiting_on_resources if element.respond_to? :waiting_on_resources |
123 | | - element |
124 | | - end |
125 | | - # rubocop:disable Lint/RescueException # we want to catch all exceptions regardless |
126 | | - rescue Exception => e |
127 | | - # rubocop:enable Lint/RescueException |
128 | | - self.class.process_exception(e, self) |
129 | | - end |
| 116 | + def render |
| 117 | + raise 'no render defined' |
| 118 | + end unless method_defined?(:render) |
| 119 | + |
| 120 | + def _render_wrapper |
| 121 | + React::State.set_state_context_to(self, true) do |
| 122 | + element = React::RenderingContext.render(nil) { render || '' } |
| 123 | + @waiting_on_resources = |
| 124 | + element.waiting_on_resources if element.respond_to? :waiting_on_resources |
| 125 | + element |
| 126 | + end |
| 127 | + # rubocop:disable Lint/RescueException # we want to catch all exceptions regardless |
| 128 | + rescue Exception => e |
| 129 | + # rubocop:enable Lint/RescueException |
| 130 | + self.class.process_exception(e, self) |
| 131 | + end |
130 | 132 |
|
131 | | - def watch(value, &on_change) |
132 | | - Observable.new(value, on_change) |
| 133 | + def watch(value, &on_change) |
| 134 | + Observable.new(value, on_change) |
| 135 | + end |
| 136 | + |
| 137 | + def define_state(*args, &block) |
| 138 | + React::State.initialize_states(self, self.class.define_state(*args, &block)) |
| 139 | + end |
133 | 140 | end |
| 141 | + end |
| 142 | +end |
134 | 143 |
|
135 | | - def define_state(*args, &block) |
136 | | - State.initialize_states(self, self.class.define_state(*args, &block)) |
| 144 | +module React |
| 145 | + module Component |
| 146 | + def self.included(base) |
| 147 | + # note this is turned off during old style testing: See the spec_helper |
| 148 | + deprecation_warning base, "The module name React::Component has been deprecated. Use Hyperloop::Component::Mixin instead." |
| 149 | + base.include Hyperloop::Component::Mixin |
| 150 | + end |
| 151 | + def self.deprecation_warning(name, message) |
| 152 | + @deprecation_messages ||= [] |
| 153 | + message = "Warning: Deprecated feature used in #{name}. #{message}" |
| 154 | + unless @deprecation_messages.include? message |
| 155 | + @deprecation_messages << message |
| 156 | + React::IsomorphicHelpers.log message, :warning |
| 157 | + end |
137 | 158 | end |
138 | 159 | end |
| 160 | + module ComponentNoNotice |
| 161 | + def self.included(base) |
| 162 | + base.include Hyperloop::Component::Mixin |
| 163 | + end |
| 164 | + end |
| 165 | +end |
| 166 | + |
| 167 | +module React |
139 | 168 | end |
0 commit comments