usar cache para docs

This commit is contained in:
Cat /dev/Nulo 2023-03-05 18:26:13 +00:00
parent 166308aa06
commit 68c6043a10
2 changed files with 8 additions and 6 deletions

View file

@ -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 {

View file

@ -23,11 +23,6 @@
}
$: docPromise = loadDoc(worldId, pageId);
onDestroy(async () => {
const doc = await docPromise;
doc.worldY.webrtcProvider.destroy();
});
</script>
<a class="no-color" href={routes.ChooseWorld}>🠔 Elegir otro mundo</a>