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.



102
103
104
# File 'lib/react/component.rb', line 102

def waiting_on_resources
  @waiting_on_resources
end

Class Method Details

.deprecation_warning(message) ⇒ Object



35
36
37
# File 'lib/react/component.rb', line 35

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

.included(base) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/react/component.rb', line 16

def self.included(base)
  base.include(Store::Mixin)
  base.include(React::Component::API)
  base.include(React::Component::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
  end
  base.extend(React::Component::ClassMethods)
end

Instance Method Details

#_render_wrapperObject



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

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
# rubocop:disable Lint/RescueException # we want to catch all exceptions regardless
rescue Exception => e
  # rubocop:enable Lint/RescueException
  self.class.process_exception(e, self)
end

#component_did_mountObject



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

def component_did_mount
  React::State.set_state_context_to(self) do
    run_callback(:after_mount)
    React::State.update_states_to_observe
  end
rescue Exception => e
  self.class.process_exception(e, self)
end

#component_did_update(prev_props, prev_state) ⇒ Object



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

def component_did_update(prev_props, prev_state)
  React::State.set_state_context_to(self) do
    self.run_callback(:after_update, Hash.new(prev_props), Hash.new(prev_state))
    React::State.update_states_to_observe
  end
rescue Exception => e
  self.class.process_exception(e, self)
end

#component_will_mountObject



52
53
54
55
56
57
58
59
# File 'lib/react/component.rb', line 52

def component_will_mount
  React::IsomorphicHelpers.load_context(true) if React::IsomorphicHelpers.on_opal_client?
  # set_state! initial_state if initial_state
  # State.initialize_states(self, initial_state)
  React::State.set_state_context_to(self) { run_callback(:before_mount) }
rescue Exception => e
  self.class.process_exception(e, self)
end

#component_will_receive_props(next_props) ⇒ Object



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

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) { self.run_callback(:before_receive_props, Hash.new(next_props)) }
rescue Exception => e
  self.class.process_exception(e, self)
end

#component_will_unmountObject



93
94
95
96
97
98
99
100
# File 'lib/react/component.rb', line 93

def component_will_unmount
  React::State.set_state_context_to(self) do
    self.run_callback(:before_unmount)
    React::State.remove
  end
rescue Exception => e
  self.class.process_exception(e, self)
end

#component_will_update(next_props, next_state) ⇒ Object



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

def component_will_update(next_props, next_state)
  React::State.set_state_context_to(self) { self.run_callback(:before_update, Hash.new(next_props), Hash.new(next_state)) }
rescue Exception => e
  self.class.process_exception(e, self)
end

#define_state(*args, &block) ⇒ Object



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

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

#deprecation_warning(message) ⇒ Object



39
40
41
# File 'lib/react/component.rb', line 39

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

#emit(event_name, *args) ⇒ Object



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

def emit(event_name, *args)
  params["_on#{event_name.to_s.event_camelize}"].call(*args)
end

#initialize(native_element) ⇒ Object



43
44
45
46
# File 'lib/react/component.rb', line 43

def initialize(native_element)
  @native = native_element
  init_store
end

#renderObject



116
117
118
# File 'lib/react/component.rb', line 116

def render
  raise 'no render defined'
end

#update_react_js_state(object, name, value) ⇒ Object



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

def update_react_js_state(object, name, value)
  if object
    name = "#{object.class}.#{name}" unless object == self
    set_state(
      '***_state_updated_at-***' => Time.now.to_f,
      name => value
    )
  else
    set_state name => value
  end
end

#watch(value, &on_change) ⇒ Object



133
134
135
# File 'lib/react/component.rb', line 133

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