Skip to content

Instantly share code, notes, and snippets.

@blueneogeo
Created August 14, 2023 16:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blueneogeo/40129b3120c5765b343c119f4b2479bd to your computer and use it in GitHub Desktop.
Save blueneogeo/40129b3120c5765b343c119f4b2479bd to your computer and use it in GitHub Desktop.
import { deferred } from "https://deno.land/std@0.168.0/async/deferred.ts";
import { assertEquals } from "https://deno.land/std@0.192.0/testing/asserts.ts";
import { createClient } from "https://esm.sh/v129/@supabase/supabase-js@2.23.0/denonext/supabase-js.mjs";
import env from "../_shared/test-env.ts";
async function testChannelComms() {
const anonClient = createClient(env.SUPABASE_URL, env.SUPABASE_ANON_KEY)
const channel = anonClient.channel('test-channel')
const done = deferred<void>()
const input = ['a', 'b', 'c']
const output: string[] = []
// Listen to channel data until end is received
// These handlers never get called
channel
.on('broadcast', { event: 'data' }, ({payload}: { payload: { token: string } }) => {
console.log('data received', payload)
output.push(payload.token)
})
.on('broadcast', { event: 'end' }, () => {
console.log('end received')
done.resolve()
})
.subscribe()
// Send data to channel
for await (const token of input) {
await channel.send({ type: 'broadcast', event: 'data', payload: { token } })
console.log('sent', token)
await sleep(50)
}
// End with the end event
await channel.send({ type: 'broadcast', event: 'end' })
console.log('sent end-data')
// Wait for the end event to have been received
console.log('awaiting data')
await done // never finishes
console.log('got data', output)
// Check that the data is the same
assertEquals(input, output)
// Try to clean up (but test still generates errors?)
await anonClient.removeAllChannels()
anonClient.realtime.disconnect()
}
function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms))
}
Deno.test('Channels test', testChannelComms)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment