Class: Supabase::Realtime::Push
- Inherits:
-
Object
- Object
- Supabase::Realtime::Push
- Defined in:
- lib/supabase/realtime/push.rb
Overview
One outbound Phoenix push, awaiting a reply. The channel matches incoming phx_reply messages to pushes by ‘ref` and fires the appropriate handler.
‘receive(:ok / :error / :timeout) { |payload| … }` registers handlers before the push is sent, mirroring phoenix.js’s Push API.
Instance Attribute Summary collapse
-
#event ⇒ Object
readonly
Returns the value of attribute event.
-
#payload ⇒ Object
readonly
Returns the value of attribute payload.
-
#received_status ⇒ Object
readonly
Returns the value of attribute received_status.
-
#ref ⇒ Object
readonly
Returns the value of attribute ref.
Instance Method Summary collapse
-
#initialize(channel, event, payload = {}, ref: nil) ⇒ Push
constructor
A new instance of Push.
- #receive(status, &block) ⇒ Object
-
#resolve(status:, payload:) ⇒ Object
Called by the Channel when a phx_reply with matching ref arrives.
-
#time_out ⇒ Object
Called by the Channel if no reply arrives within the timeout window.
Constructor Details
#initialize(channel, event, payload = {}, ref: nil) ⇒ Push
Returns a new instance of Push.
15 16 17 18 19 20 21 22 23 |
# File 'lib/supabase/realtime/push.rb', line 15 def initialize(channel, event, payload = {}, ref: nil) @channel = channel @event = event @payload = payload @ref = ref @handlers = Hash.new { |h, k| h[k] = [] } @received_status = nil @received_payload = nil end |
Instance Attribute Details
#event ⇒ Object (readonly)
Returns the value of attribute event.
13 14 15 |
# File 'lib/supabase/realtime/push.rb', line 13 def event @event end |
#payload ⇒ Object (readonly)
Returns the value of attribute payload.
13 14 15 |
# File 'lib/supabase/realtime/push.rb', line 13 def payload @payload end |
#received_status ⇒ Object (readonly)
Returns the value of attribute received_status.
13 14 15 |
# File 'lib/supabase/realtime/push.rb', line 13 def received_status @received_status end |
#ref ⇒ Object (readonly)
Returns the value of attribute ref.
13 14 15 |
# File 'lib/supabase/realtime/push.rb', line 13 def ref @ref end |
Instance Method Details
#receive(status, &block) ⇒ Object
25 26 27 28 29 30 31 32 33 |
# File 'lib/supabase/realtime/push.rb', line 25 def receive(status, &block) if @received_status == status # Reply already arrived before this handler was attached — fire immediately. block.call(@received_payload) else @handlers[status] << block end self end |
#resolve(status:, payload:) ⇒ Object
Called by the Channel when a phx_reply with matching ref arrives.
36 37 38 39 40 |
# File 'lib/supabase/realtime/push.rb', line 36 def resolve(status:, payload:) @received_status = status @received_payload = payload @handlers[status].each { |h| h.call(payload) } end |