File size: 3,484 Bytes
c212805 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | import { Channel } from '@supabase/phoenix'
import { CHANNEL_STATES, MAX_PUSH_BUFFER_SIZE } from '../lib/constants'
import type { RealtimeChannelOptions } from '../RealtimeChannel'
import SocketAdapter from './socketAdapter'
import type {
ChannelBindingCallback,
ChannelOnMessage,
ChannelOnErrorCallback,
ChannelFilterBindings,
Params,
ChannelState,
Push,
Timer,
} from './types'
export default class ChannelAdapter {
private channel: Channel
private socket: SocketAdapter
constructor(socket: SocketAdapter, topic: string, params: RealtimeChannelOptions) {
const phoenixParams = phoenixChannelParams(params)
this.channel = socket.getSocket().channel(topic, phoenixParams)
this.socket = socket
}
get state(): ChannelState {
return this.channel.state
}
set state(state: ChannelState) {
this.channel.state = state
}
get joinedOnce(): boolean {
return this.channel.joinedOnce
}
get joinPush(): Push {
return this.channel.joinPush
}
get rejoinTimer(): Timer {
return this.channel.rejoinTimer
}
on(event: string, callback: ChannelBindingCallback): number {
return this.channel.on(event, callback)
}
off(event: string, refNumber?: number) {
this.channel.off(event, refNumber)
}
subscribe(timeout?: number): Push {
return this.channel.join(timeout)
}
unsubscribe(timeout?: number): Push {
return this.channel.leave(timeout)
}
teardown() {
this.channel.teardown()
}
onClose(callback: ChannelBindingCallback) {
this.channel.onClose(callback)
}
onError(callback: ChannelOnErrorCallback): number {
return this.channel.onError(callback)
}
push(event: string, payload: { [key: string]: any }, timeout?: number): Push {
let push: Push
try {
push = this.channel.push(event, payload, timeout)
} catch (error) {
throw new Error(
`tried to push '${event}' to '${this.channel.topic}' before joining. Use channel.subscribe() before pushing events`
)
}
if (this.channel.pushBuffer.length > MAX_PUSH_BUFFER_SIZE) {
const removedPush = this.channel.pushBuffer.shift()!
removedPush.cancelTimeout()
this.socket.log(
'channel',
`discarded push due to buffer overflow: ${removedPush.event}`,
removedPush.payload()
)
}
return push
}
updateJoinPayload(payload: Record<string, any>) {
const oldPayload = this.channel.joinPush.payload()
this.channel.joinPush.payload = () => ({ ...oldPayload, ...payload })
}
canPush() {
return this.socket.isConnected() && this.state === CHANNEL_STATES.joined
}
isJoined() {
return this.state === CHANNEL_STATES.joined
}
isJoining() {
return this.state === CHANNEL_STATES.joining
}
isClosed() {
return this.state === CHANNEL_STATES.closed
}
isLeaving() {
return this.state === CHANNEL_STATES.leaving
}
updateFilterBindings(filterBindings: ChannelFilterBindings) {
this.channel.filterBindings = filterBindings
}
updatePayloadTransform(callback: ChannelOnMessage) {
this.channel.onMessage = callback
}
/**
* @internal
*/
getChannel() {
return this.channel
}
}
function phoenixChannelParams(options: RealtimeChannelOptions): Params {
return {
config: {
...{
broadcast: { ack: false, self: false },
presence: { key: '', enabled: false },
private: false,
},
...options.config,
},
}
}
|