From 75ef4782833569f77692d1f4c6ebb3503ce65cb4 Mon Sep 17 00:00:00 2001 From: Bernhard Seefeld Date: Tue, 15 Apr 2025 15:22:18 -0700 Subject: [PATCH] charmmanager: sync the cells, not the docs --- charm/src/charm.ts | 58 ++++++++++++++++++++-------------------------- 1 file changed, 25 insertions(+), 33 deletions(-) diff --git a/charm/src/charm.ts b/charm/src/charm.ts index 1377d870d..113347644 100644 --- a/charm/src/charm.ts +++ b/charm/src/charm.ts @@ -15,6 +15,7 @@ import { DocImpl, EntityId, followAliases, + getCell, getCellFromEntityId, getDoc, getEntityId, @@ -110,9 +111,6 @@ function filterOutEntity( export class CharmManager { private space: string; - private charmsDoc: DocImpl; - private pinned: DocImpl; - private trash: DocImpl; private charms: Cell[]>; private pinnedCharms: Cell[]>; @@ -121,32 +119,24 @@ export class CharmManager { /** * Promise resolved when the charm manager gets the charm list. */ - ready: Promise; + ready: Promise; constructor( private session: Session, ) { this.space = this.session.space; - this.charmsDoc = getDoc([], "charms", this.space); - this.pinned = getDoc([], "pinned-charms", this.space); - this.trash = getDoc([], "trash", this.space); - this.charms = this.charmsDoc.asCell([], undefined, charmListSchema); storage.setSigner(session.as); - this.pinnedCharms = this.pinned.asCell([], undefined, charmListSchema); - this.trashedCharms = this.trash.asCell([], undefined, charmListSchema); - this.ready = Promise.all( - this.primaryDocs.map((doc) => storage.syncCell(doc)), - ).then(); - } + this.charms = getCell(this.space, "charms", charmListSchema); + this.pinnedCharms = getCell(this.space, "pinned-charms", charmListSchema); + this.trashedCharms = getCell(this.space, "trash", charmListSchema); - get primaryDocs() { - return [ - this.charmsDoc, - this.pinned, - this.trash, - ]; + this.ready = Promise.all([ + storage.syncCell(this.charms), + storage.syncCell(this.pinnedCharms), + storage.syncCell(this.trashedCharms), + ]); } getSpace(): string { @@ -159,7 +149,7 @@ export class CharmManager { } async pin(charm: Cell) { - await storage.syncCell(this.pinned); + await storage.syncCell(this.pinnedCharms); // Check if already pinned if ( !filterOutEntity(this.pinnedCharms, charm).some((c) => @@ -172,7 +162,7 @@ export class CharmManager { } async unpinById(charmId: EntityId) { - await storage.syncCell(this.pinned); + await storage.syncCell(this.pinnedCharms); const newPinnedCharms = filterOutEntity(this.pinnedCharms, charmId); if (newPinnedCharms.length !== this.pinnedCharms.get().length) { @@ -192,18 +182,18 @@ export class CharmManager { } getPinned(): Cell[]> { - storage.syncCell(this.pinned); + storage.syncCell(this.pinnedCharms); return this.pinnedCharms; } getTrash(): Cell[]> { - storage.syncCell(this.trash); + storage.syncCell(this.trashedCharms); return this.trashedCharms; } async restoreFromTrash(idOrCharm: string | EntityId | Cell) { - await storage.syncCell(this.trash); - await storage.syncCell(this.charmsDoc); + await storage.syncCell(this.trashedCharms); + await storage.syncCell(this.charms); const id = getEntityId(idOrCharm); if (!id) return false; @@ -227,7 +217,7 @@ export class CharmManager { } async emptyTrash() { - await storage.syncCell(this.trash); + await storage.syncCell(this.trashedCharms); this.trashedCharms.set([]); await idle(); return true; @@ -239,12 +229,12 @@ export class CharmManager { getCharms(): Cell[]> { // Start syncing if not already syncing. Will trigger a change to the list // once loaded. - storage.syncCell(this.charmsDoc); + storage.syncCell(this.charms); return this.charms; } private async add(newCharms: Cell[]) { - await storage.syncCell(this.charmsDoc); + await storage.syncCell(this.charms); await idle(); newCharms.forEach((charm) => { @@ -1152,9 +1142,11 @@ export class CharmManager { // note: removing a charm doesn't clean up the charm's cells // Now moves the charm to trash instead of just removing it async remove(idOrCharm: string | EntityId | Cell) { - await storage.syncCell(this.charmsDoc); - await storage.syncCell(this.pinned); - await storage.syncCell(this.trash); + await Promise.all([ + storage.syncCell(this.charms), + storage.syncCell(this.pinnedCharms), + storage.syncCell(this.trashedCharms), + ]); const id = getEntityId(idOrCharm); if (!id) return false; @@ -1183,7 +1175,7 @@ export class CharmManager { // Permanently delete a charm (from trash or directly) async permanentlyDelete(idOrCharm: string | EntityId | Cell) { - await storage.syncCell(this.trash); + await storage.syncCell(this.trashedCharms); const id = getEntityId(idOrCharm); if (!id) return false;