File size: 3,394 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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | import { Socket } from '@supabase/phoenix'
import type {
Message,
SocketOnClose,
SocketOnMessage,
SocketOnOpen,
SocketOnError,
SocketOptions,
SocketStateChangeCallbacks,
Vsn,
Encode,
Decode,
HeartbeatCallback,
Timer,
} from './types'
import { CONNECTION_STATE, ConnectionState } from '../lib/constants'
import type { HeartbeatTimer, WebSocketLikeConstructor } from '../RealtimeClient'
export default class SocketAdapter {
private socket: Socket
constructor(endPoint: string, options: SocketOptions) {
this.socket = new Socket(endPoint, options)
}
get timeout(): number {
return this.socket.timeout
}
get endPoint(): string {
return this.socket.endPoint
}
get transport(): WebSocketLikeConstructor {
return this.socket.transport as WebSocketLikeConstructor
}
get heartbeatIntervalMs(): number {
return this.socket.heartbeatIntervalMs
}
get heartbeatCallback(): HeartbeatCallback {
return this.socket.heartbeatCallback
}
set heartbeatCallback(callback: HeartbeatCallback) {
this.socket.heartbeatCallback = callback
}
get heartbeatTimer(): HeartbeatTimer {
return this.socket.heartbeatTimer
}
get pendingHeartbeatRef(): string | null {
return this.socket.pendingHeartbeatRef
}
get reconnectTimer(): Timer {
return this.socket.reconnectTimer
}
get vsn(): Vsn {
return this.socket.vsn
}
get encode(): Encode<void> {
return this.socket.encode
}
get decode(): Decode<void> {
return this.socket.decode
}
get reconnectAfterMs(): (tries: number) => number {
return this.socket.reconnectAfterMs
}
get sendBuffer(): (() => void)[] {
return this.socket.sendBuffer
}
get stateChangeCallbacks(): SocketStateChangeCallbacks {
return this.socket.stateChangeCallbacks
}
connect() {
this.socket.connect()
}
disconnect(
callback: () => void,
code?: number,
reason?: string,
timeout: number = 10000
): Promise<'ok' | 'timeout'> {
return new Promise((resolve) => {
setTimeout(() => resolve('timeout'), timeout)
this.socket.disconnect(
() => {
callback()
resolve('ok')
},
code,
reason
)
})
}
push(data: Message<Record<string, unknown>>) {
this.socket.push(data)
}
log(kind: string, msg: string, data?: any) {
this.socket.log(kind, msg, data)
}
makeRef(): string {
return this.socket.makeRef()
}
onOpen(callback: SocketOnOpen) {
this.socket.onOpen(callback)
}
onClose(callback: SocketOnClose) {
this.socket.onClose(callback)
}
onError(callback: SocketOnError) {
this.socket.onError(callback)
}
onMessage(callback: SocketOnMessage) {
this.socket.onMessage(callback)
}
isConnected() {
return this.socket.isConnected()
}
isConnecting() {
return this.socket.connectionState() == CONNECTION_STATE.connecting
}
isDisconnecting() {
return this.socket.connectionState() == CONNECTION_STATE.closing
}
connectionState(): ConnectionState {
// @ts-ignore - requires better typing and exposing type in phoenix
return this.socket.connectionState()
}
endPointURL(): string {
return this.socket.endPointURL()
}
sendHeartbeat() {
this.socket.sendHeartbeat()
}
/**
* @internal
*/
getSocket() {
return this.socket
}
}
|