Class: Supabase::Realtime::Push

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#eventObject (readonly)

Returns the value of attribute event.



13
14
15
# File 'lib/supabase/realtime/push.rb', line 13

def event
  @event
end

#payloadObject (readonly)

Returns the value of attribute payload.



13
14
15
# File 'lib/supabase/realtime/push.rb', line 13

def payload
  @payload
end

#received_statusObject (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

#refObject (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

#time_outObject

Called by the Channel if no reply arrives within the timeout window.



43
44
45
# File 'lib/supabase/realtime/push.rb', line 43

def time_out
  resolve(status: Types::AckStatus::TIMEOUT, payload: {})
end