From 68c6043a10c55198731b509689a456f55d1f77a7 Mon Sep 17 00:00:00 2001 From: Nulo Date: Sun, 5 Mar 2023 18:26:13 +0000 Subject: [PATCH] usar cache para docs --- src/lib/doc.ts | 9 ++++++++- src/views/Page.svelte | 5 ----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/lib/doc.ts b/src/lib/doc.ts index 8c73fa1..b949680 100644 --- a/src/lib/doc.ts +++ b/src/lib/doc.ts @@ -19,7 +19,12 @@ export function generateNewWorld(): WorldIdentifier { }; } +// when creating a webrtc provider for a second time in the same room, it freaks out. +// cache the previous doc and return that instead +let worldYCache: { [key: string]: WorldY } = {}; + export function getWorldY(world: WorldIdentifier): WorldY { + if (worldYCache[world.room]) return worldYCache[world.room]; const ydoc = new Y.Doc(); const provider = new WebrtcProvider(world.room, ydoc, { password: world.password, @@ -29,7 +34,9 @@ export function getWorldY(world: WorldIdentifier): WorldY { "wss://y-webrtc-signaling-us.herokuapp.com", ], }); - return { ydoc, webrtcProvider: provider }; + const worldY = { ydoc, webrtcProvider: provider }; + worldYCache[world.room] = worldY; + return worldY; } export function getWorldPage(ydoc: Y.Doc, pageId: string): Y.XmlFragment { diff --git a/src/views/Page.svelte b/src/views/Page.svelte index e574049..dcc2f89 100644 --- a/src/views/Page.svelte +++ b/src/views/Page.svelte @@ -23,11 +23,6 @@ } $: docPromise = loadDoc(worldId, pageId); - - onDestroy(async () => { - const doc = await docPromise; - doc.worldY.webrtcProvider.destroy(); - }); 🠔 Elegir otro mundo