Module: Hyperloop::Component::Mixin

Included in:
React::TopLevelRailsComponent
Defined in:
lib/react/component.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#waiting_on_resourcesObject (readonly)

Returns the value of attribute waiting_on_resources.



119
120
121
# File 'lib/react/component.rb', line 119

def waiting_on_resources
  @waiting_on_resources
end

Class Method Details

.deprecation_warning(message) ⇒ Object



49
50
51
# File 'lib/react/component.rb', line 49

def self.deprecation_warning(message)
  React::Component.deprecation_warning(name, message)
end

.included(base) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/react/component.rb', line 29

def self.included(base)
  base.include(Hyperloop::Store::Mixin)
  base.include(React::Component::API)
  base.include(React::Callbacks)
  base.include(React::Component::Tags)
  base.include(React::Component::DslInstanceMethods)
  base.include(React::Component::ShouldComponentUpdate)
  base.class_eval do
    class_attribute :initial_state
    define_callback :before_mount
    define_callback :after_mount
    define_callback :before_receive_props
    define_callback :before_update
    define_callback :after_update
    define_callback :before_unmount
    define_callback(:after_error) { React::API.add_after_error_hook(base) }
  end
  base.extend(React::Component::ClassMethods)
end

Instance Method Details

#_render_wrapperObject



145
146
147
148
149
150
151
152
# File 'lib/react/component.rb', line 145

def _render_wrapper
  React::State.set_state_context_to(self, true) do
    element = React::RenderingContext.render(nil) { render || '' }
    @waiting_on_resources =
      element.waiting_on_resources if element.respond_to? :waiting_on_resources
    element
  end
end

#component_did_catch(error, info) ⇒ Object



113
114
115
116
117
# File 'lib/react/component.rb', line 113

def component_did_catch(error, info)
  React::State.set_state_context_to(self) do
    run_callback(:after_error, error, info)
  end
end

#component_did_mountObject



78
79
80
81
82
83
# File 'lib/react/component.rb', line 78

def component_did_mount
  React::State.set_state_context_to(self) do
    run_callback(:after_mount)
    React::State.update_states_to_observe
  end
end

#component_did_update(prev_props, prev_state) ⇒ Object



98
99
100
101
102
103
# File 'lib/react/component.rb', line 98

def component_did_update(prev_props, prev_state)
  React::State.set_state_context_to(self) do
    run_callback(:after_update, prev_props, prev_state)
    React::State.update_states_to_observe
  end
end

#component_will_mountObject



70
71
72
73
74
75
76
# File 'lib/react/component.rb', line 70

def component_will_mount
  React::IsomorphicHelpers.load_context(true) if React::IsomorphicHelpers.on_opal_client?
  React::State.set_state_context_to(self) do
    Hyperloop::Component.mounted_components << self
    run_callback(:before_mount)
  end
end

#component_will_receive_props(next_props) ⇒ Object



85
86
87
88
89
90
# File 'lib/react/component.rb', line 85

def component_will_receive_props(next_props)
  # need to rethink how this works in opal-react, or if its actually that useful within the react.rb environment
  # for now we are just using it to clear processed_params
  React::State.set_state_context_to(self) { run_callback(:before_receive_props, next_props) }
  @_receiving_props = true
end

#component_will_unmountObject



105
106
107
108
109
110
111
# File 'lib/react/component.rb', line 105

def component_will_unmount
  React::State.set_state_context_to(self) do
    run_callback(:before_unmount)
    React::State.remove
    Hyperloop::Component.mounted_components.delete self
  end
end

#component_will_update(next_props, next_state) ⇒ Object



92
93
94
95
96
# File 'lib/react/component.rb', line 92

def component_will_update(next_props, next_state)
  React::State.set_state_context_to(self) { run_callback(:before_update, next_props, next_state) }
  params._reset_all_others_cache if @_receiving_props
  @_receiving_props = false
end

#define_state(*args, &block) ⇒ Object



158
159
160
# File 'lib/react/component.rb', line 158

def define_state(*args, &block)
  React::State.initialize_states(self, self.class.define_state(*args, &block))
end

#deprecation_warning(message) ⇒ Object



53
54
55
# File 'lib/react/component.rb', line 53

def deprecation_warning(message)
  React::Component.deprecation_warning(self.class.name, message)
end

#emit(event_name, *args) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/react/component.rb', line 62

def emit(event_name, *args)
  if React::Event::BUILT_IN_EVENTS.include?(built_in_event_name = "on#{event_name.to_s.event_camelize}")
    params[built_in_event_name].call(*args)
  else
    params["on_#{event_name}"].call(*args)
  end
end

#initialize(native_element) ⇒ Object



57
58
59
60
# File 'lib/react/component.rb', line 57

def initialize(native_element)
  @native = native_element
  init_store
end

#renderObject



141
142
143
# File 'lib/react/component.rb', line 141

def render
  raise 'no render defined'
end

#set_state_synchronously?Boolean

Returns:



137
138
139
# File 'lib/react/component.rb', line 137

def set_state_synchronously?
  @native.JS[:__opalInstanceSyncSetState]
end

#update_react_js_state(object, name, value) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/react/component.rb', line 121

def update_react_js_state(object, name, value)
  if object
    name = "#{object.class}.#{name}" unless object == self
    # Date.now() has only millisecond precision, if several notifications of
    # observer happen within a millisecond, updates may get lost.
    # to mitigate this the Math.random() appends some random number
    # this way notifactions will happen as expected by the rest of hyperloop
    set_state(
      '***_state_updated_at-***' => `Date.now() + Math.random()`,
      name => value
    )
  else
    set_state name => value
  end
end

#watch(value, &on_change) ⇒ Object



154
155
156
# File 'lib/react/component.rb', line 154

def watch(value, &on_change)
  Observable.new(value, on_change)
end