Skip to content

Commit

Permalink
wait to watch for disconnections until we have a list of connections
Browse files Browse the repository at this point in the history
  • Loading branch information
dankoster committed Oct 25, 2024
1 parent 4ea9019 commit f592f89
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions server/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ async function watchForRoomUpdates() {

//server is starting up... get connections list
const connectionByUUID = await InitConnections()
connectionByUUID.forEach((con, uuid) => watchForRemoteConnectionDisconnect(KV_KEYS.connection(uuid), uuid))
watchForNewConnections()

async function InitConnections() {
Expand All @@ -195,8 +196,6 @@ async function InitConnections() {
if (connection.status === 'online' && !updateFunctionByUUID.has(uuid)) {
createRemoteUpdateFunction(uuid)
}

watchForRemoteConnectionDisconnect(kv_connection.key, uuid)
}

console.log(serverID, "INIT Got connections from KV:", connections)
Expand Down Expand Up @@ -246,8 +245,11 @@ function createRemoteUpdateFunction(uuid: string) {
}

async function watchForRemoteConnectionDisconnect(key: Deno.KvKey, uuid: string) {
if(!connectionByUUID)
throw new Error(`${serverID} cannot watch for disconnect before connectionByUUID exists!`)

//watch for changes to this connection
console.log(serverID, 'KV watching for updates', uuid)
console.log(serverID, 'KV watching for updates', key)
const stream = kv.watch([key]);
for await (const update of stream) {
// console.log(serverID, "KV connection updated", update[0])
Expand Down

0 comments on commit f592f89

Please sign in to comment.