Skip to content

Commit

Permalink
wait better for collab readiness
Browse files Browse the repository at this point in the history
  • Loading branch information
chee committed Oct 3, 2024
1 parent bb3df45 commit 739c828
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
9 changes: 5 additions & 4 deletions public/automerge/collab.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,10 @@ async function withAutomergeRepo(fn) {
}
}

let alreadyFancy = false

export function fancy() {
return Boolean(repo?.networkSubsystem.isReady())
return alreadyFancy
}

/**
Expand All @@ -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"))
}

Expand Down Expand Up @@ -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<CollaborativePattern>}
Expand Down Expand Up @@ -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
Expand Down
20 changes: 11 additions & 9 deletions public/bento.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion public/service-worker.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// <reference types="@secret-types/serviceworker" />
// 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)
Expand Down

0 comments on commit 739c828

Please sign in to comment.