From 6773d6e85afae238cfa031abb50b17517195c865 Mon Sep 17 00:00:00 2001 From: Wen Bo Xie Date: Fri, 12 Aug 2022 01:14:46 -0400 Subject: [PATCH] feat: reduce Realtime footprint to four helper methods --- example/next-storage/README.md | 3 +- .../next-storage/components/ProfileList.tsx | 5 +- example/next-todo/components/TodoList.js | 8 +- package-lock.json | 119 ++++++++++----- package.json | 2 +- src/SupabaseClient.ts | 96 +++--------- src/index.ts | 2 +- src/lib/SupabaseRealtimeChannel.ts | 138 ------------------ src/lib/types.ts | 12 -- 9 files changed, 114 insertions(+), 271 deletions(-) delete mode 100644 src/lib/SupabaseRealtimeChannel.ts diff --git a/example/next-storage/README.md b/example/next-storage/README.md index 679641a2..4b977683 100644 --- a/example/next-storage/README.md +++ b/example/next-storage/README.md @@ -37,9 +37,8 @@ create policy "Users can update own profile." -- Set up Realtime! begin; drop publication if exists supabase_realtime; - create publication supabase_realtime; + create publication supabase_realtime for table profiles; commit; -alter publication supabase_realtime add table profiles; -- Set up Storage! insert into storage.buckets (id, name) diff --git a/example/next-storage/components/ProfileList.tsx b/example/next-storage/components/ProfileList.tsx index a632aa98..0e8ea08e 100644 --- a/example/next-storage/components/ProfileList.tsx +++ b/example/next-storage/components/ProfileList.tsx @@ -1,6 +1,5 @@ import ProfileCard from '../components/ProfileCard' import { Profile } from '../lib/constants' -import { SupabaseRealtimePayload } from '@supabase/supabase-js' import { supabase } from '../lib/api' import { useState, useEffect } from 'react' @@ -14,9 +13,9 @@ export default function ProfileList() { const realtimeProfiles = supabase .channel('profiles-channel') .on( - 'realtime', + 'postgres_changes', { event: '*', schema: 'public', table: 'profiles' }, - (payload: SupabaseRealtimePayload) => profileUpdated(profiles, payload.new) + (payload: { [key: string]: any }) => profileUpdated(profiles, payload.new) ) .subscribe() diff --git a/example/next-todo/components/TodoList.js b/example/next-todo/components/TodoList.js index f53f162b..dadecad2 100644 --- a/example/next-todo/components/TodoList.js +++ b/example/next-todo/components/TodoList.js @@ -14,20 +14,20 @@ export default function Todos({ user }) { subscription1 = supabase .channel('todos-table-channel') - .on('realtime', { event: 'UPDATE', schema: 'public', table: 'todos' }, (v) => + .on('postgres_changes', { event: 'UPDATE', schema: 'public', table: 'todos' }, (v) => console.log('UPDATE on todos', v) ) - .on('realtime', { event: 'INSERT', schema: 'public', table: 'todos' }, (v) => + .on('postgres_changes', { event: 'INSERT', schema: 'public', table: 'todos' }, (v) => console.log('INSERT on todos', v) ) .subscribe((change) => console.log('todos changed', change)) subscription2 = supabase .channel('public-schema-channel') - .on('realtime', { event: 'UPDATE', schema: 'public' }, (v) => + .on('postgres_changes', { event: 'UPDATE', schema: 'public' }, (v) => console.log('UPDATE on schema', v) ) - .on('realtime', { event: 'INSERT', schema: 'public' }, (v) => + .on('postgres_changes', { event: 'INSERT', schema: 'public' }, (v) => console.log('INSERT on schema', v) ) .subscribe((change) => console.log('schema changed', change)) diff --git a/package-lock.json b/package-lock.json index 2601ef5e..40965cda 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,7 +12,7 @@ "@supabase/functions-js": "^1.4.0-next.1", "@supabase/gotrue-js": "^1.23.0-next.15", "@supabase/postgrest-js": "^1.0.0-next.6", - "@supabase/realtime-js": "^1.8.0-next.13", + "@supabase/realtime-js": "^1.8.0-next.15", "@supabase/storage-js": "^1.8.0-next.5", "cross-fetch": "^3.1.5" }, @@ -867,6 +867,20 @@ "node": ">= 10.14.2" } }, + "node_modules/@jridgewell/gen-mapping": { + "version": "0.3.2", + "resolved": "https://registry.npmjs.org/@jridgewell/gen-mapping/-/gen-mapping-0.3.2.tgz", + "integrity": "sha512-mh65xKQAzI6iBcFzwv28KVWSmCkdRBWoOh+bYQGW3+6OZvbbN3TqMGo5hqYxQniRcH9F2VZIoJCm4pa3BPDK/A==", + "dev": true, + "dependencies": { + "@jridgewell/set-array": "^1.0.1", + "@jridgewell/sourcemap-codec": "^1.4.10", + "@jridgewell/trace-mapping": "^0.3.9" + }, + "engines": { + "node": ">=6.0.0" + } + }, "node_modules/@jridgewell/resolve-uri": { "version": "3.0.5", "resolved": "https://registry.npmjs.org/@jridgewell/resolve-uri/-/resolve-uri-3.0.5.tgz", @@ -876,6 +890,25 @@ "node": ">=6.0.0" } }, + "node_modules/@jridgewell/set-array": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/@jridgewell/set-array/-/set-array-1.1.2.tgz", + "integrity": "sha512-xnkseuNADM0gt2bs+BvhO0p78Mk762YnZdsuzFV018NoG1Sj1SCQvpSqa7XUaTam5vAGasABV9qXASMKnFMwMw==", + "dev": true, + "engines": { + "node": ">=6.0.0" + } + }, + "node_modules/@jridgewell/source-map": { + "version": "0.3.2", + "resolved": "https://registry.npmjs.org/@jridgewell/source-map/-/source-map-0.3.2.tgz", + "integrity": "sha512-m7O9o2uR8k2ObDysZYzdfhb08VuEml5oWGiosa1VdaPZ/A6QyPkAJuwN0Q1lhULOf6B7MtQmHENS743hWtCrgw==", + "dev": true, + "dependencies": { + "@jridgewell/gen-mapping": "^0.3.0", + "@jridgewell/trace-mapping": "^0.3.9" + } + }, "node_modules/@jridgewell/sourcemap-codec": { "version": "1.4.11", "resolved": "https://registry.npmjs.org/@jridgewell/sourcemap-codec/-/sourcemap-codec-1.4.11.tgz", @@ -883,9 +916,9 @@ "dev": true }, "node_modules/@jridgewell/trace-mapping": { - "version": "0.3.4", - "resolved": "https://registry.npmjs.org/@jridgewell/trace-mapping/-/trace-mapping-0.3.4.tgz", - "integrity": "sha512-vFv9ttIedivx0ux3QSjhgtCVjPZd5l46ZOMDSCwnH1yUO2e964gO8LZGyv2QkqcgR6TnBU1v+1IFqmeoG+0UJQ==", + "version": "0.3.15", + "resolved": "https://registry.npmjs.org/@jridgewell/trace-mapping/-/trace-mapping-0.3.15.tgz", + "integrity": "sha512-oWZNOULl+UbhsgB51uuZzglikfIKSUBO/M9W2OfEjn7cmqoAiCgmv9lyACTUacZwBz0ITnJ2NqjU8Tx0DHL88g==", "dev": true, "dependencies": { "@jridgewell/resolve-uri": "^3.0.3", @@ -935,9 +968,9 @@ } }, "node_modules/@supabase/realtime-js": { - "version": "1.8.0-next.13", - "resolved": "https://registry.npmjs.org/@supabase/realtime-js/-/realtime-js-1.8.0-next.13.tgz", - "integrity": "sha512-qHMIXTp7AXd4Iy2vtOBqYJ7C1x7DrgTH3BqrcvQQCqp+xEBAqQdKsiZsbk8mfSJqVMQghksgMT3GT3G9QboBDQ==", + "version": "1.8.0-next.15", + "resolved": "https://registry.npmjs.org/@supabase/realtime-js/-/realtime-js-1.8.0-next.15.tgz", + "integrity": "sha512-V/83Mov30NRtxzY/pfIQElQfTEbmvuhuApsuwvf7+jkaLo/M+lcr23fj8uPv+8PDdSW0NEXqhJLGHKwdM47hwA==", "dependencies": { "@types/phoenix": "^1.5.4", "websocket": "^1.0.34" @@ -7312,14 +7345,14 @@ } }, "node_modules/terser": { - "version": "5.12.1", - "resolved": "https://registry.npmjs.org/terser/-/terser-5.12.1.tgz", - "integrity": "sha512-NXbs+7nisos5E+yXwAD+y7zrcTkMqb0dEJxIGtSKPdCBzopf7ni4odPul2aechpV7EXNvOudYOX2bb5tln1jbQ==", + "version": "5.14.2", + "resolved": "https://registry.npmjs.org/terser/-/terser-5.14.2.tgz", + "integrity": "sha512-oL0rGeM/WFQCUd0y2QrWxYnq7tfSuKBiqTjRPWrRgB46WD/kiwHwF8T23z78H6Q6kGCuuHcPB+KULHRdxvVGQA==", "dev": true, "dependencies": { + "@jridgewell/source-map": "^0.3.2", "acorn": "^8.5.0", "commander": "^2.20.0", - "source-map": "~0.7.2", "source-map-support": "~0.5.20" }, "bin": { @@ -7392,15 +7425,6 @@ "url": "https://github.com/chalk/supports-color?sponsor=1" } }, - "node_modules/terser/node_modules/source-map": { - "version": "0.7.3", - "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.7.3.tgz", - "integrity": "sha512-CkCj6giN3S+n9qrYiBTX5gystlENnRW5jZeNLHpe6aue+SrHcG5VYwujhW9s4dY31mEGsxBDrHR6oI69fTXsaQ==", - "dev": true, - "engines": { - "node": ">= 8" - } - }, "node_modules/test-exclude": { "version": "6.0.0", "resolved": "https://registry.npmjs.org/test-exclude/-/test-exclude-6.0.0.tgz", @@ -9048,12 +9072,39 @@ "chalk": "^4.0.0" } }, + "@jridgewell/gen-mapping": { + "version": "0.3.2", + "resolved": "https://registry.npmjs.org/@jridgewell/gen-mapping/-/gen-mapping-0.3.2.tgz", + "integrity": "sha512-mh65xKQAzI6iBcFzwv28KVWSmCkdRBWoOh+bYQGW3+6OZvbbN3TqMGo5hqYxQniRcH9F2VZIoJCm4pa3BPDK/A==", + "dev": true, + "requires": { + "@jridgewell/set-array": "^1.0.1", + "@jridgewell/sourcemap-codec": "^1.4.10", + "@jridgewell/trace-mapping": "^0.3.9" + } + }, "@jridgewell/resolve-uri": { "version": "3.0.5", "resolved": "https://registry.npmjs.org/@jridgewell/resolve-uri/-/resolve-uri-3.0.5.tgz", "integrity": "sha512-VPeQ7+wH0itvQxnG+lIzWgkysKIr3L9sslimFW55rHMdGu/qCQ5z5h9zq4gI8uBtqkpHhsF4Z/OwExufUCThew==", "dev": true }, + "@jridgewell/set-array": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/@jridgewell/set-array/-/set-array-1.1.2.tgz", + "integrity": "sha512-xnkseuNADM0gt2bs+BvhO0p78Mk762YnZdsuzFV018NoG1Sj1SCQvpSqa7XUaTam5vAGasABV9qXASMKnFMwMw==", + "dev": true + }, + "@jridgewell/source-map": { + "version": "0.3.2", + "resolved": "https://registry.npmjs.org/@jridgewell/source-map/-/source-map-0.3.2.tgz", + "integrity": "sha512-m7O9o2uR8k2ObDysZYzdfhb08VuEml5oWGiosa1VdaPZ/A6QyPkAJuwN0Q1lhULOf6B7MtQmHENS743hWtCrgw==", + "dev": true, + "requires": { + "@jridgewell/gen-mapping": "^0.3.0", + "@jridgewell/trace-mapping": "^0.3.9" + } + }, "@jridgewell/sourcemap-codec": { "version": "1.4.11", "resolved": "https://registry.npmjs.org/@jridgewell/sourcemap-codec/-/sourcemap-codec-1.4.11.tgz", @@ -9061,9 +9112,9 @@ "dev": true }, "@jridgewell/trace-mapping": { - "version": "0.3.4", - "resolved": "https://registry.npmjs.org/@jridgewell/trace-mapping/-/trace-mapping-0.3.4.tgz", - "integrity": "sha512-vFv9ttIedivx0ux3QSjhgtCVjPZd5l46ZOMDSCwnH1yUO2e964gO8LZGyv2QkqcgR6TnBU1v+1IFqmeoG+0UJQ==", + "version": "0.3.15", + "resolved": "https://registry.npmjs.org/@jridgewell/trace-mapping/-/trace-mapping-0.3.15.tgz", + "integrity": "sha512-oWZNOULl+UbhsgB51uuZzglikfIKSUBO/M9W2OfEjn7cmqoAiCgmv9lyACTUacZwBz0ITnJ2NqjU8Tx0DHL88g==", "dev": true, "requires": { "@jridgewell/resolve-uri": "^3.0.3", @@ -9113,9 +9164,9 @@ } }, "@supabase/realtime-js": { - "version": "1.8.0-next.13", - "resolved": "https://registry.npmjs.org/@supabase/realtime-js/-/realtime-js-1.8.0-next.13.tgz", - "integrity": "sha512-qHMIXTp7AXd4Iy2vtOBqYJ7C1x7DrgTH3BqrcvQQCqp+xEBAqQdKsiZsbk8mfSJqVMQghksgMT3GT3G9QboBDQ==", + "version": "1.8.0-next.15", + "resolved": "https://registry.npmjs.org/@supabase/realtime-js/-/realtime-js-1.8.0-next.15.tgz", + "integrity": "sha512-V/83Mov30NRtxzY/pfIQElQfTEbmvuhuApsuwvf7+jkaLo/M+lcr23fj8uPv+8PDdSW0NEXqhJLGHKwdM47hwA==", "requires": { "@types/phoenix": "^1.5.4", "websocket": "^1.0.34" @@ -14092,23 +14143,15 @@ } }, "terser": { - "version": "5.12.1", - "resolved": "https://registry.npmjs.org/terser/-/terser-5.12.1.tgz", - "integrity": "sha512-NXbs+7nisos5E+yXwAD+y7zrcTkMqb0dEJxIGtSKPdCBzopf7ni4odPul2aechpV7EXNvOudYOX2bb5tln1jbQ==", + "version": "5.14.2", + "resolved": "https://registry.npmjs.org/terser/-/terser-5.14.2.tgz", + "integrity": "sha512-oL0rGeM/WFQCUd0y2QrWxYnq7tfSuKBiqTjRPWrRgB46WD/kiwHwF8T23z78H6Q6kGCuuHcPB+KULHRdxvVGQA==", "dev": true, "requires": { + "@jridgewell/source-map": "^0.3.2", "acorn": "^8.5.0", "commander": "^2.20.0", - "source-map": "~0.7.2", "source-map-support": "~0.5.20" - }, - "dependencies": { - "source-map": { - "version": "0.7.3", - "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.7.3.tgz", - "integrity": "sha512-CkCj6giN3S+n9qrYiBTX5gystlENnRW5jZeNLHpe6aue+SrHcG5VYwujhW9s4dY31mEGsxBDrHR6oI69fTXsaQ==", - "dev": true - } } }, "terser-webpack-plugin": { diff --git a/package.json b/package.json index e9a55c89..e37269b8 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ "@supabase/functions-js": "^1.4.0-next.1", "@supabase/gotrue-js": "^1.23.0-next.15", "@supabase/postgrest-js": "^1.0.0-next.6", - "@supabase/realtime-js": "^1.8.0-next.13", + "@supabase/realtime-js": "^1.8.0-next.15", "@supabase/storage-js": "^1.8.0-next.5", "cross-fetch": "^3.1.5" }, diff --git a/src/SupabaseClient.ts b/src/SupabaseClient.ts index 0ba81d3b..26dec76c 100644 --- a/src/SupabaseClient.ts +++ b/src/SupabaseClient.ts @@ -11,7 +11,6 @@ import { DEFAULT_HEADERS } from './lib/constants' import { fetchWithAuth } from './lib/fetch' import { stripTrailingSlash, applySettingDefaults } from './lib/helpers' import { SupabaseAuthClient } from './lib/SupabaseAuthClient' -import { SupabaseRealtimeChannel } from './lib/SupabaseRealtimeChannel' import { Fetch, GenericSchema, SupabaseClientOptions, SupabaseAuthClientOptions } from './lib/types' const DEFAULT_GLOBAL_OPTIONS = { @@ -125,11 +124,6 @@ export default class SupabaseClient< }) this._listenForAuthEvents() - - // In the future we might allow the user to pass in a logger to receive these events. - // this.realtime.onOpen(() => console.log('OPEN')) - // this.realtime.onClose(() => console.log('CLOSED')) - // this.realtime.onError((e: Error) => console.log('Socket error', e)) } /** @@ -194,50 +188,38 @@ export default class SupabaseClient< } /** - * Creates a channel with Broadcast and Presence. + * Creates a Realtime channel with Broadcast, Presence, and Postgres Changes. + * + * @param {string} name - The name of the Realtime channel. + * @param {Object} opts - The options to pass to the Realtime channel. + * */ - channel(name: string, opts?: { [key: string]: any }): SupabaseRealtimeChannel { - if (!this.realtime.isConnected()) { - this.realtime.connect() - } - - return new SupabaseRealtimeChannel(name, opts, this.realtime) + channel(name: string, opts: { [key: string]: any } = {}): RealtimeChannel { + return this.realtime.channel(name, opts) } /** - * Closes and removes all channels and returns a list of removed - * channels and their errors. + * Returns all Realtime channels. */ - async removeAllChannels(): Promise< - { data: { channels: RealtimeChannel }; error: Error | null }[] - > { - const allChans: RealtimeChannel[] = this.getChannels().slice() - const allChanPromises = allChans.map((chan) => this.removeChannel(chan)) - const allRemovedChans = await Promise.all(allChanPromises) - - return allRemovedChans.map(({ error }, i) => { - return { - data: { channels: allChans[i] }, - error, - } - }) + getChannels(): RealtimeChannel[] { + return this.realtime.getChannels() } /** - * Closes and removes a channel and returns the number of open channels. + * Unsubscribes and removes Realtime channel from Realtime client. + * + * @param {RealtimeChannel} channel - The name of the Realtime channel. * - * @param channel The channel you want to close and remove. */ - async removeChannel( - channel: RealtimeChannel - ): Promise<{ data: { openChannels: number }; error: Error | null }> { - const { error } = await this._closeChannel(channel) - const allChans: RealtimeChannel[] = this.getChannels() - const openChanCount = allChans.filter((chan) => chan.isJoined()).length - - if (allChans.length === 0) await this.realtime.disconnect() + removeChannel(channel: RealtimeChannel): Promise<'ok' | 'timed out' | 'error'> { + return this.realtime.removeChannel(channel) + } - return { data: { openChannels: openChanCount }, error } + /** + * Unsubscribes and removes all Realtime channels from Realtime client. + */ + removeAllChannels(): Promise<('ok' | 'timed out' | 'error')[]> { + return this.realtime.removeAllChannels() } private async _getAccessToken() { @@ -246,36 +228,6 @@ export default class SupabaseClient< return session?.access_token ?? null } - private async _closeChannel(channel: RealtimeChannel): Promise<{ error: Error | null }> { - let error = null - - if (!channel.isClosed()) { - const { error: unsubError } = await this._unsubscribeChannel(channel) - error = unsubError - } - - this.realtime.remove(channel) - - return { error } - } - - private _unsubscribeChannel(channel: RealtimeChannel): Promise<{ error: Error | null }> { - return new Promise((resolve) => { - channel - .unsubscribe() - .receive('ok', () => resolve({ error: null })) - .receive('error', (error: Error) => resolve({ error })) - .receive('timeout', () => resolve({ error: new Error('timed out') })) - }) - } - - /** - * Returns an array of all your channels. - */ - getChannels(): RealtimeChannel[] { - return this.realtime.channels as RealtimeChannel[] - } - private _initSupabaseAuthClient( { autoRefreshToken, @@ -305,10 +257,10 @@ export default class SupabaseClient< }) } - private _initRealtimeClient(options?: RealtimeClientOptions) { + private _initRealtimeClient(options: RealtimeClientOptions) { return new RealtimeClient(this.realtimeUrl, { ...options, - params: { ...{ apikey: this.supabaseKey, vsndate: '2022' }, ...options?.params }, + params: { ...{ apikey: this.supabaseKey }, ...options?.params }, }) } @@ -329,7 +281,7 @@ export default class SupabaseClient< this.changedAccessToken !== token ) { // Token has changed - this.realtime.setAuth(token!) + this.realtime.setAuth(token ?? null) this.changedAccessToken = token } else if (event === 'SIGNED_OUT' || event === 'USER_DELETED') { diff --git a/src/index.ts b/src/index.ts index 06fb0bff..d7cbf4f4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -11,7 +11,7 @@ export type { } from '@supabase/postgrest-js' export * from '@supabase/realtime-js' export { default as SupabaseClient } from './SupabaseClient' -export type { SupabaseClientOptions, SupabaseRealtimePayload } from './lib/types' +export type { SupabaseClientOptions } from './lib/types' /** * Creates a new Supabase Client. diff --git a/src/lib/SupabaseRealtimeChannel.ts b/src/lib/SupabaseRealtimeChannel.ts deleted file mode 100644 index 6baf0537..00000000 --- a/src/lib/SupabaseRealtimeChannel.ts +++ /dev/null @@ -1,138 +0,0 @@ -import { RealtimeChannel, RealtimeClient, Transformers } from '@supabase/realtime-js' - -export class SupabaseRealtimeChannel { - socket: RealtimeClient - channel: RealtimeChannel - - constructor(name: string, opts: { [key: string]: any } = {}, socket: RealtimeClient) { - this.socket = socket - this.channel = socket.channel(`realtime:${name}`, opts) as RealtimeChannel - } - - /** - * The event you want to listen to. - * - * @param event The event - * @param filter An object that specifies what you want to listen to from the event. - * @param callback A callback function that is called whenever the event occurs. - */ - on(event: string, filter?: Record, callback?: (payload: any) => void) { - this.channel.on(event, filter ?? {}, ({ payload }: { payload: any }) => { - let enrichedPayload = payload - - if (event === 'realtime') { - const { schema, table, commit_timestamp, type, errors } = enrichedPayload - enrichedPayload = { - schema: schema, - table: table, - commit_timestamp: commit_timestamp, - eventType: type, - new: {}, - old: {}, - errors: errors, - } - enrichedPayload = { ...enrichedPayload, ...this.getPayloadRecords(payload) } - } - - callback && callback(enrichedPayload) - }) - - return this - } - - /** - * Enables the channel. - */ - subscribe(callback: Function = () => {}) { - // if the socket already has a good accessToken - // we can just use it straight away - if (this.socket.accessToken) { - this.channel.updateJoinPayload({ - user_token: this.socket.accessToken, - }) - } - - this.channel.onError((e: Error) => callback('CHANNEL_ERROR', e)) - this.channel.onClose(() => callback('CLOSED')) - this.channel - .subscribe() - .receive('ok', () => { - callback('SUBSCRIBED') - - // re-set the accessToken again in case it was set while - // the subscription was isJoining - if (this.socket.accessToken) { - this.socket.setAuth(this.socket.accessToken) - } - }) - .receive('error', (e: Error) => callback('CHANNEL_ERROR', e)) - .receive('timeout', () => callback('RETRYING_AFTER_TIMEOUT')) - - return this.channel - } - - presenceList() { - return this.channel.presence.list() - } - - send( - payload: { type: string; [key: string]: any }, - opts: { [key: string]: any } = {} - ): Promise<['ok' | 'timeout', number]> { - return new Promise((resolve) => { - const now = performance.now() - const timeout = opts.timeout || this.channel.timeout - - this.channel - .push(payload.type, payload, timeout) - .receive('ok', () => { - const diff = performance.now() - now - resolve(['ok', diff]) - }) - .receive('timeout', () => { - resolve(['timeout', timeout]) - }) - }) - } - - async track( - payload: { [key: string]: any }, - opts: { [key: string]: any } = {} - ): Promise<['ok' | 'timeout', number]> { - return await this.send( - { - type: 'presence', - event: 'track', - payload, - }, - opts - ) - } - - async untrack(opts: { [key: string]: any } = {}): Promise<['ok' | 'timeout', number]> { - return await this.send( - { - type: 'presence', - event: 'untrack', - }, - opts - ) - } - - private getPayloadRecords(payload: any) { - const records = { - new: {}, - old: {}, - } - - if (payload.type === 'INSERT' || payload.type === 'UPDATE') { - records.new = Transformers.convertChangeData(payload.columns, payload.record) - } - - if (payload.type === 'UPDATE' || payload.type === 'DELETE') { - records.old = Transformers.convertChangeData(payload.columns, payload.old_record) - } - - return records - } -} diff --git a/src/lib/types.ts b/src/lib/types.ts index 3ccb7d9e..2ef4e7f7 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -57,18 +57,6 @@ export type SupabaseClientOptions = { } } -export type SupabaseRealtimePayload = { - commit_timestamp: string - eventType: 'INSERT' | 'UPDATE' | 'DELETE' - schema: string - table: string - /** The new record. Present for 'INSERT' and 'UPDATE' events. */ - new: T - /** The previous record. Present for 'UPDATE' and 'DELETE' events. */ - old: T - errors: string[] | null -} - export type GenericTable = { Row: Record Insert: Record