From 739c8287f979cb5d9a00645b864eb34f482b921c Mon Sep 17 00:00:00 2001 From: chee Date: Thu, 3 Oct 2024 18:19:34 +0100 Subject: [PATCH] wait better for collab readiness --- public/automerge/collab.js | 9 +++++---- public/bento.js | 20 +++++++++++--------- public/service-worker.js | 2 +- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/public/automerge/collab.js b/public/automerge/collab.js index 4084c64..12f2fab 100644 --- a/public/automerge/collab.js +++ b/public/automerge/collab.js @@ -66,8 +66,10 @@ async function withAutomergeRepo(fn) { } } +let alreadyFancy = false + export function fancy() { - return Boolean(repo?.networkSubsystem.isReady()) + return alreadyFancy } /** @@ -92,9 +94,6 @@ export async function init(sab) { } }) window.repo = repo - if (!repo.networkSubsystem.isReady()) { - await repo.networkSubsystem.whenReady() - } document.dispatchEvent(new CustomEvent("repo")) } @@ -129,6 +128,7 @@ let currentId */ export async function start(url, memtree) { if (url == currentId) return + alreadyFancy = false currentId = url /** * @type {import("@automerge/automerge-repo").DocHandle} @@ -206,6 +206,7 @@ export async function start(url, memtree) { ) firstLoadComplete = true }) + alreadyFancy = true let preventPatchApplications = false let dispose = memtree.listen((kind, index, source) => { if (source == "automerge") return diff --git a/public/bento.js b/public/bento.js index 34a4a04..1b119cb 100644 --- a/public/bento.js +++ b/public/bento.js @@ -60,31 +60,31 @@ async function getFancy() { try { if (!db.fancy() && !isAutomerge) { await db.load() - if (sounds.empty()) { await sounds.loadDefaultKit() } + party.setAttribute("db", "fancy") } if (isAutomerge && !collab.fancy()) { - await collab.init(sharedarraybuffer) await collab.start( /** @type {import("@automerge/automerge-repo").AutomergeUrl}*/ (slug), memtree ) + party.setAttribute("collab", "fancy") + return } + if (!sounds.fancy()) { await sounds.start() + party.setAttribute("sfx", "fancy") } if (sounds.fancy() && !graphics.fancy()) { graphics.start(sharedarraybuffer) + party.setAttribute("gfx", "fancy") } - if ( - sounds.fancy() && - graphics.fancy() && - (db.fancy() || (isAutomerge && collab.fancy())) - ) { + if (sounds.fancy() && graphics.fancy() && (db.fancy() || collab.fancy())) { party.fancy = true } } catch {} @@ -185,11 +185,15 @@ async function loadFromFile(file) { async function init() { if (isValidAutomergeUrl(db.getSlugFromLocation())) { await collab.init(sharedarraybuffer) + party.setAttribute("collab", "init") } else { await db.init(sharedarraybuffer) + party.setAttribute("db", "init") } await graphics.init() + party.setAttribute("gfx", "init") await sounds.init(sharedarraybuffer, memtree) + party.setAttribute("sfx", "init") } await init() @@ -646,7 +650,6 @@ party.settings.when("load", async () => { }) party.settings.when("jam", async () => { - await collab.init() pushSlug(collab.create(memtree)) let {slug} = history.state collab.stop() @@ -659,7 +662,6 @@ addEventListener("popstate", async () => { let slug = history.state?.slug || "bento" collab.stop() if (isValidAutomergeUrl(slug)) { - await collab.init() await collab.start(slug, memtree) } else { await db.load(slug) diff --git a/public/service-worker.js b/public/service-worker.js index c7bff3c..ac20c30 100644 --- a/public/service-worker.js +++ b/public/service-worker.js @@ -1,6 +1,6 @@ /// // update this when changing the service worker -const SERVICE_WORKER_VERSION = "v17" +const SERVICE_WORKER_VERSION = "v18" const addResourcesToCache = async resources => { const cache = await caches.open(SERVICE_WORKER_VERSION)