usar cache para docs
This commit is contained in:
parent
166308aa06
commit
68c6043a10
2 changed files with 8 additions and 6 deletions
|
@ -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 {
|
export function getWorldY(world: WorldIdentifier): WorldY {
|
||||||
|
if (worldYCache[world.room]) return worldYCache[world.room];
|
||||||
const ydoc = new Y.Doc();
|
const ydoc = new Y.Doc();
|
||||||
const provider = new WebrtcProvider(world.room, ydoc, {
|
const provider = new WebrtcProvider(world.room, ydoc, {
|
||||||
password: world.password,
|
password: world.password,
|
||||||
|
@ -29,7 +34,9 @@ export function getWorldY(world: WorldIdentifier): WorldY {
|
||||||
"wss://y-webrtc-signaling-us.herokuapp.com",
|
"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 {
|
export function getWorldPage(ydoc: Y.Doc, pageId: string): Y.XmlFragment {
|
||||||
|
|
|
@ -23,11 +23,6 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
$: docPromise = loadDoc(worldId, pageId);
|
$: docPromise = loadDoc(worldId, pageId);
|
||||||
|
|
||||||
onDestroy(async () => {
|
|
||||||
const doc = await docPromise;
|
|
||||||
doc.worldY.webrtcProvider.destroy();
|
|
||||||
});
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<a class="no-color" href={routes.ChooseWorld}>🠔 Elegir otro mundo</a>
|
<a class="no-color" href={routes.ChooseWorld}>🠔 Elegir otro mundo</a>
|
||||||
|
|
Loading…
Reference in a new issue