|
1 | 1 | import { RealtimeChannel, RealtimeClient, Transformers } from '@supabase/realtime-js'
|
2 |
| -import { SupabaseRealtimePayload } from './types' |
3 | 2 |
|
4 |
| -export class SupabaseRealtimeClient { |
| 3 | +export class SupabaseRealtimeChannel { |
5 | 4 | socket: RealtimeClient
|
6 | 5 | channel: RealtimeChannel
|
7 | 6 |
|
8 |
| - constructor(socket: RealtimeClient, name: string, opts: { [key: string]: any } = {}) { |
| 7 | + constructor(name: string, opts: { [key: string]: any } = {}, socket: RealtimeClient) { |
9 | 8 | this.socket = socket
|
10 | 9 | this.channel = socket.channel(`realtime:${name}`, opts) as RealtimeChannel
|
11 | 10 | }
|
12 | 11 |
|
13 |
| - private getPayloadRecords(payload: any) { |
14 |
| - const records = { |
15 |
| - new: {}, |
16 |
| - old: {}, |
17 |
| - } |
18 |
| - |
19 |
| - if (payload.type === 'INSERT' || payload.type === 'UPDATE') { |
20 |
| - records.new = Transformers.convertChangeData(payload.columns, payload.record) |
21 |
| - } |
22 |
| - |
23 |
| - if (payload.type === 'UPDATE' || payload.type === 'DELETE') { |
24 |
| - records.old = Transformers.convertChangeData(payload.columns, payload.old_record) |
25 |
| - } |
26 |
| - |
27 |
| - return records |
28 |
| - } |
29 |
| - |
30 | 12 | /**
|
31 | 13 | * The event you want to listen to.
|
32 | 14 | *
|
@@ -88,4 +70,69 @@ export class SupabaseRealtimeClient {
|
88 | 70 |
|
89 | 71 | return this.channel
|
90 | 72 | }
|
| 73 | + |
| 74 | + presenceList() { |
| 75 | + return this.channel.presence.list() |
| 76 | + } |
| 77 | + |
| 78 | + send( |
| 79 | + payload: { type: string; [key: string]: any }, |
| 80 | + opts: { [key: string]: any } = {} |
| 81 | + ): Promise<['ok' | 'timeout', number]> { |
| 82 | + return new Promise((resolve) => { |
| 83 | + const now = performance.now() |
| 84 | + const timeout = opts.timeout || this.channel.timeout |
| 85 | + |
| 86 | + this.channel |
| 87 | + .push(payload.type, payload, timeout) |
| 88 | + .receive('ok', () => { |
| 89 | + const diff = performance.now() - now |
| 90 | + resolve(['ok', diff]) |
| 91 | + }) |
| 92 | + .receive('timeout', () => { |
| 93 | + resolve(['timeout', timeout]) |
| 94 | + }) |
| 95 | + }) |
| 96 | + } |
| 97 | + |
| 98 | + async track( |
| 99 | + payload: { [key: string]: any }, |
| 100 | + opts: { [key: string]: any } = {} |
| 101 | + ): Promise<['ok' | 'timeout', number]> { |
| 102 | + return await this.send( |
| 103 | + { |
| 104 | + type: 'presence', |
| 105 | + event: 'track', |
| 106 | + payload, |
| 107 | + }, |
| 108 | + opts |
| 109 | + ) |
| 110 | + } |
| 111 | + |
| 112 | + async untrack(opts: { [key: string]: any } = {}): Promise<['ok' | 'timeout', number]> { |
| 113 | + return await this.send( |
| 114 | + { |
| 115 | + type: 'presence', |
| 116 | + event: 'untrack', |
| 117 | + }, |
| 118 | + opts |
| 119 | + ) |
| 120 | + } |
| 121 | + |
| 122 | + private getPayloadRecords(payload: any) { |
| 123 | + const records = { |
| 124 | + new: {}, |
| 125 | + old: {}, |
| 126 | + } |
| 127 | + |
| 128 | + if (payload.type === 'INSERT' || payload.type === 'UPDATE') { |
| 129 | + records.new = Transformers.convertChangeData(payload.columns, payload.record) |
| 130 | + } |
| 131 | + |
| 132 | + if (payload.type === 'UPDATE' || payload.type === 'DELETE') { |
| 133 | + records.old = Transformers.convertChangeData(payload.columns, payload.old_record) |
| 134 | + } |
| 135 | + |
| 136 | + return records |
| 137 | + } |
91 | 138 | }
|
0 commit comments