Class: Motion::ComponentConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/motion/component_connection.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(component, log_helper: LogHelper.for_component(component)) ⇒ ComponentConnection

Returns a new instance of ComponentConnection.



20
21
22
23
24
25
26
27
28
29
# File 'lib/motion/component_connection.rb', line 20

def initialize(component, log_helper: LogHelper.for_component(component))
  @component = component
  @log_helper = log_helper

  timing("Connected") do
    @render_hash = component.render_hash

    component.process_connect
  end
end

Instance Attribute Details

#componentObject (readonly)

Returns the value of attribute component.



18
19
20
# File 'lib/motion/component_connection.rb', line 18

def component
  @component
end

Class Method Details

.from_state(state, serializer: Motion.serializer, log_helper: LogHelper.new, **kargs) ⇒ Object



7
8
9
10
11
12
13
14
15
16
# File 'lib/motion/component_connection.rb', line 7

def self.from_state(
  state,
  serializer: Motion.serializer,
  log_helper: LogHelper.new,
  **kargs
)
  component = serializer.deserialize(state)

  new(component, log_helper: log_helper.for_component(component), **kargs)
end

Instance Method Details

#broadcastsObject



94
95
96
# File 'lib/motion/component_connection.rb', line 94

def broadcasts
  component.broadcasts
end

#closeObject



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/motion/component_connection.rb', line 31

def close
  timing("Disconnected") do
    component.process_disconnect
  end

  true
rescue => error
  handle_error(error, "disconnecting the component")

  false
end

#if_render_required(&block) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/motion/component_connection.rb', line 79

def if_render_required(&block)
  timing("Rendered") do
    next_render_hash = component.render_hash

    return if @render_hash == next_render_hash &&
      !component.awaiting_forced_rerender?

    yield(component)

    @render_hash = next_render_hash
  end
rescue => error
  handle_error(error, "rendering the component")
end

#periodic_timersObject



98
99
100
# File 'lib/motion/component_connection.rb', line 98

def periodic_timers
  component.periodic_timers
end

#process_broadcast(broadcast, message) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/motion/component_connection.rb', line 55

def process_broadcast(broadcast, message)
  timing("Processed broadcast to #{broadcast}") do
    component.process_broadcast broadcast, message
  end

  true
rescue => error
  handle_error(error, "processing a broadcast to #{broadcast}")

  false
end

#process_motion(motion, event = nil) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/motion/component_connection.rb', line 43

def process_motion(motion, event = nil)
  timing("Processed #{motion}") do
    component.process_motion(motion, event)
  end

  true
rescue => error
  handle_error(error, "processing #{motion}")

  false
end

#process_periodic_timer(timer) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/motion/component_connection.rb', line 67

def process_periodic_timer(timer)
  timing("Processed periodic timer #{timer}") do
    component.process_periodic_timer timer
  end

  true
rescue => error
  handle_error(error, "processing periodic timer #{timer}")

  false
end