From ef1de4647f3e32ce21941c9c331d00ad2c5fab28 Mon Sep 17 00:00:00 2001 From: Jonathan Raphaelson Date: Wed, 29 Oct 2025 19:40:49 +0000 Subject: [PATCH] fix announce, sync quickly --- src/client/page-app.tsx | 3 ++- src/realm/client/service-connection-peer.ts | 2 +- src/realm/client/service-connection-sync.ts | 12 ++++++++++-- src/realm/client/service-connection.ts | 12 +++++++++++- vite.config.js | 1 + 5 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/client/page-app.tsx b/src/client/page-app.tsx index 6ffc9f1..ed8248a 100644 --- a/src/client/page-app.tsx +++ b/src/client/page-app.tsx @@ -18,8 +18,9 @@ export const App: preact.FunctionComponent = () => { p.error ?

Connection Error! {p.error.message}

:

Connection Loading

const wshost = window.location.host - const wsproto = window.location.protocol === 'https' ? 'wss' : 'ws' + const wsproto = window.location.protocol === 'https:' ? 'wss' : 'ws' const wsurl = `${wsproto}://${wshost}/stream` + console.log('connectiong at:', wsurl) return ( diff --git a/src/realm/client/service-connection-peer.ts b/src/realm/client/service-connection-peer.ts index 780fb25..76f8717 100644 --- a/src/realm/client/service-connection-peer.ts +++ b/src/realm/client/service-connection-peer.ts @@ -108,7 +108,7 @@ export class RealmPeer extends SimplePeer { #closed = () => { console.debug(`disconnected from ${this.identid}`) - this.#abort.abort() + this.#abort.abort('disconnected') this.#dispatch('peerclose', {identid: this.identid}) } diff --git a/src/realm/client/service-connection-sync.ts b/src/realm/client/service-connection-sync.ts index 89c3b60..467270f 100644 --- a/src/realm/client/service-connection-sync.ts +++ b/src/realm/client/service-connection-sync.ts @@ -51,16 +51,24 @@ export class RealmSyncManager { async buildSyncState(): Promise { const states: PeerClocks = {} - states[this.#identity.identid] = this.#identity.latest await this.#db.clocks.each((row) => (states[row.actor] = row.clock)) + states[this.#identity.identid] = this.#identity.latest return states } async buildSyncDelta(clocks: PeerClocks): Promise { + console.log('building sync delta:', clocks) + + // in the initial case (they have no peers), just send everything + // we have nothing to compare to; they can dedupe if this wrong + if (!Object.keys(clocks).length) return await this.#db.actions.toCollection().sortBy('clock') + + // in the initial case (we have no other peers), just send everything + // we have nothing to compare to; they can dedupe if this wrong const states = await this.buildSyncState() const [first, ...known] = Object.keys(states) as IdentID[] - if (!first) return [] + if (!first) return await this.#db.actions.toCollection().sortBy('clock') const initial = this.#db.actions.where('[actor+clock]').between( [first, clocks[first] ?? Dexie.minKey], diff --git a/src/realm/client/service-connection.ts b/src/realm/client/service-connection.ts index c5fe85a..323e288 100644 --- a/src/realm/client/service-connection.ts +++ b/src/realm/client/service-connection.ts @@ -24,6 +24,7 @@ const realmRtcMessagesSchema = z.union([ protocol.realmRtcPeerJoinedEventSchema, protocol.realmRtcPeerLeftEventSchema, protocol.realmRtcPongResponseSchema, + protocol.realmRtcAnnounceResponseSchema ]) export interface ConnectionOptions { @@ -137,7 +138,7 @@ export class RealmConnection extends EventTarget { this.#reconnecting = false // shutdown the loops - this.#abort.abort() + this.#abort.abort('shutting down') // close ports this.#closeWebSocket() @@ -389,6 +390,15 @@ export class RealmConnection extends EventTarget { return } + + case 'realm.rtc.announce': { + const actions = await this.#sync.buildSyncDelta(parse.data.dat.peerClocks) + if (actions.length) { + this.#socket.send(JSON.stringify(actions.map((a) => a.action))) + } + + return + } } } diff --git a/vite.config.js b/vite.config.js index 06e92a1..7311a1f 100644 --- a/vite.config.js +++ b/vite.config.js @@ -30,6 +30,7 @@ export default defineConfig({ clearScreen: false, server: { host: '127.0.0.1', + allowedHosts: ['lightyear.cetacean-skate.ts.net'], port: 4000, proxy: { '/api': 'http://127.0.0.1:4001', -- 2.51.2