Module: Ably::Modules::StateEmitter
- Included in:
- Realtime::Channel, Realtime::Connection, Realtime::Connection::WebsocketTransport, Realtime::Models::NilChannel, Realtime::Presence, Realtime::Presence::MembersMap
- Defined in:
- lib/ably/modules/state_emitter.rb
Overview
This module requires that the method #logger is defined.
StateEmitter module adds a set of generic state related methods to a class on the assumption that the instance variable @state is used exclusively, the Enum STATE is defined prior to inclusion of this module, and the class is an EventEmitter. It then emits state changes.
It also ensures the EventEmitter is configured to restrict permitted events to the the available STATEs or EVENTs if defined i.e. if EVENTs includes an additional type such as :update, then it will support all EVENTs being emitted. EVENTs must be a superset of STATEs
Instance Method Summary collapse
-
#once_or_if(target_states, options = {}) { ... } ⇒ void
If the current state matches the target_state argument the block is called immediately.
-
#once_state_changed(options = {}) { ... } ⇒ void
private
Calls the block once when the state changes.
-
#state ⇒ Symbol
Current state Enum.
-
#state=(new_state, *args) ⇒ Symbol
(also: #change_state)
private
Set the current state Enum.
-
#state?(check_state) ⇒ Boolean
Evaluates if check_state matches current state.
-
#unsafe_once_or_if(target_states, options = {}, &block) ⇒ Object
private
Equivalent of #once_or_if but any exception raised in a block will bubble up and cause this client library to fail.
-
#unsafe_once_state_changed(&block) ⇒ Object
private
Equivalent of #once_state_changed but any exception raised in a block will bubble up and cause this client library to fail.
Instance Method Details
#once_or_if(target_states, options = {}) { ... } ⇒ void
This method returns an undefined value.
If the current state matches the target_state argument the block is called immediately. Else the block is called once when the target_state is reached.
If the option block :else is provided then if any state other than target_state is reached, the :else block is called, however only one of the blocks will ever be called
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/ably/modules/state_emitter.rb', line 77 def once_or_if(target_states, = {}, &block) raise ArgumentError, 'Block required' unless block_given? if Array(target_states).any? { |target_state| state == target_state } safe_yield block else failure_block = .fetch(:else, nil) failure_wrapper = nil success_wrapper = lambda do |*args| yield off(&success_wrapper) off(&failure_wrapper) if failure_wrapper end failure_wrapper = lambda do |*args| failure_block.call(*args) off(&success_wrapper) off(&failure_wrapper) end if failure_block Array(target_states).each do |target_state| safe_unsafe_method [:unsafe], :once, target_state, &success_wrapper safe_unsafe_method [:unsafe], :once_state_changed do |*args| failure_wrapper.call(*args) unless state == target_state end if failure_block end end end |
#once_state_changed(options = {}) { ... } ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Calls the block once when the state changes
121 122 123 124 125 126 127 128 129 130 |
# File 'lib/ably/modules/state_emitter.rb', line 121 def once_state_changed( = {}, &block) raise ArgumentError, 'Block required' unless block_given? once_block = lambda do |*args| off(*self.class::STATE.map, &once_block) yield(*args) end safe_unsafe_method [:unsafe], :once, *self.class::STATE.map, &once_block end |
#state ⇒ Symbol
Current state Enum
39 40 41 |
# File 'lib/ably/modules/state_emitter.rb', line 39 def state STATE(@state) end |
#state=(new_state, *args) ⇒ Symbol Also known as: change_state
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Set the current state Enum
55 56 57 58 59 60 61 |
# File 'lib/ably/modules/state_emitter.rb', line 55 def state=(new_state, *args) if state != new_state logger.debug { "#{self.class}: StateEmitter changed from #{state} => #{new_state}" } if respond_to?(:logger, true) @state = STATE(new_state) emit @state, *args end end |
#state?(check_state) ⇒ Boolean
Evaluates if check_state matches current state
47 48 49 |
# File 'lib/ably/modules/state_emitter.rb', line 47 def state?(check_state) state == check_state end |
#unsafe_once_or_if(target_states, options = {}, &block) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Equivalent of #once_or_if but any exception raised in a block will bubble up and cause this client library to fail. This method should only be used internally by the client library.
111 112 113 |
# File 'lib/ably/modules/state_emitter.rb', line 111 def unsafe_once_or_if(target_states, = {}, &block) once_or_if(target_states, .merge(unsafe: true), &block) end |
#unsafe_once_state_changed(&block) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Equivalent of #once_state_changed but any exception raised in a block will bubble up and cause this client library to fail. This method should only be used internally by the client library.
135 136 137 |
# File 'lib/ably/modules/state_emitter.rb', line 135 def unsafe_once_state_changed(&block) once_state_changed(unsafe: true, &block) end |