Skip to content

Commit 6191694

Browse files
authored
charmmanager: sync the cells, not the docs (#1053)
1 parent 46585bf commit 6191694

File tree

1 file changed

+25
-33
lines changed

1 file changed

+25
-33
lines changed

charm/src/charm.ts

+25-33
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
DocImpl,
1616
EntityId,
1717
followAliases,
18+
getCell,
1819
getCellFromEntityId,
1920
getDoc,
2021
getEntityId,
@@ -110,9 +111,6 @@ function filterOutEntity(
110111

111112
export class CharmManager {
112113
private space: string;
113-
private charmsDoc: DocImpl<CellLink[]>;
114-
private pinned: DocImpl<CellLink[]>;
115-
private trash: DocImpl<CellLink[]>;
116114

117115
private charms: Cell<Cell<Charm>[]>;
118116
private pinnedCharms: Cell<Cell<Charm>[]>;
@@ -121,32 +119,24 @@ export class CharmManager {
121119
/**
122120
* Promise resolved when the charm manager gets the charm list.
123121
*/
124-
ready: Promise<void>;
122+
ready: Promise<any>;
125123

126124
constructor(
127125
private session: Session,
128126
) {
129127
this.space = this.session.space;
130-
this.charmsDoc = getDoc<CellLink[]>([], "charms", this.space);
131-
this.pinned = getDoc<CellLink[]>([], "pinned-charms", this.space);
132-
this.trash = getDoc<CellLink[]>([], "trash", this.space);
133-
this.charms = this.charmsDoc.asCell([], undefined, charmListSchema);
134128

135129
storage.setSigner(session.as);
136-
this.pinnedCharms = this.pinned.asCell([], undefined, charmListSchema);
137-
this.trashedCharms = this.trash.asCell([], undefined, charmListSchema);
138130

139-
this.ready = Promise.all(
140-
this.primaryDocs.map((doc) => storage.syncCell(doc)),
141-
).then();
142-
}
131+
this.charms = getCell(this.space, "charms", charmListSchema);
132+
this.pinnedCharms = getCell(this.space, "pinned-charms", charmListSchema);
133+
this.trashedCharms = getCell(this.space, "trash", charmListSchema);
143134

144-
get primaryDocs() {
145-
return [
146-
this.charmsDoc,
147-
this.pinned,
148-
this.trash,
149-
];
135+
this.ready = Promise.all([
136+
storage.syncCell(this.charms),
137+
storage.syncCell(this.pinnedCharms),
138+
storage.syncCell(this.trashedCharms),
139+
]);
150140
}
151141

152142
getSpace(): string {
@@ -159,7 +149,7 @@ export class CharmManager {
159149
}
160150

161151
async pin(charm: Cell<Charm>) {
162-
await storage.syncCell(this.pinned);
152+
await storage.syncCell(this.pinnedCharms);
163153
// Check if already pinned
164154
if (
165155
!filterOutEntity(this.pinnedCharms, charm).some((c) =>
@@ -172,7 +162,7 @@ export class CharmManager {
172162
}
173163

174164
async unpinById(charmId: EntityId) {
175-
await storage.syncCell(this.pinned);
165+
await storage.syncCell(this.pinnedCharms);
176166
const newPinnedCharms = filterOutEntity(this.pinnedCharms, charmId);
177167

178168
if (newPinnedCharms.length !== this.pinnedCharms.get().length) {
@@ -192,18 +182,18 @@ export class CharmManager {
192182
}
193183

194184
getPinned(): Cell<Cell<Charm>[]> {
195-
storage.syncCell(this.pinned);
185+
storage.syncCell(this.pinnedCharms);
196186
return this.pinnedCharms;
197187
}
198188

199189
getTrash(): Cell<Cell<Charm>[]> {
200-
storage.syncCell(this.trash);
190+
storage.syncCell(this.trashedCharms);
201191
return this.trashedCharms;
202192
}
203193

204194
async restoreFromTrash(idOrCharm: string | EntityId | Cell<Charm>) {
205-
await storage.syncCell(this.trash);
206-
await storage.syncCell(this.charmsDoc);
195+
await storage.syncCell(this.trashedCharms);
196+
await storage.syncCell(this.charms);
207197

208198
const id = getEntityId(idOrCharm);
209199
if (!id) return false;
@@ -227,7 +217,7 @@ export class CharmManager {
227217
}
228218

229219
async emptyTrash() {
230-
await storage.syncCell(this.trash);
220+
await storage.syncCell(this.trashedCharms);
231221
this.trashedCharms.set([]);
232222
await idle();
233223
return true;
@@ -239,12 +229,12 @@ export class CharmManager {
239229
getCharms(): Cell<Cell<Charm>[]> {
240230
// Start syncing if not already syncing. Will trigger a change to the list
241231
// once loaded.
242-
storage.syncCell(this.charmsDoc);
232+
storage.syncCell(this.charms);
243233
return this.charms;
244234
}
245235

246236
private async add(newCharms: Cell<Charm>[]) {
247-
await storage.syncCell(this.charmsDoc);
237+
await storage.syncCell(this.charms);
248238
await idle();
249239

250240
newCharms.forEach((charm) => {
@@ -1152,9 +1142,11 @@ export class CharmManager {
11521142
// note: removing a charm doesn't clean up the charm's cells
11531143
// Now moves the charm to trash instead of just removing it
11541144
async remove(idOrCharm: string | EntityId | Cell<Charm>) {
1155-
await storage.syncCell(this.charmsDoc);
1156-
await storage.syncCell(this.pinned);
1157-
await storage.syncCell(this.trash);
1145+
await Promise.all([
1146+
storage.syncCell(this.charms),
1147+
storage.syncCell(this.pinnedCharms),
1148+
storage.syncCell(this.trashedCharms),
1149+
]);
11581150

11591151
const id = getEntityId(idOrCharm);
11601152
if (!id) return false;
@@ -1183,7 +1175,7 @@ export class CharmManager {
11831175

11841176
// Permanently delete a charm (from trash or directly)
11851177
async permanentlyDelete(idOrCharm: string | EntityId | Cell<Charm>) {
1186-
await storage.syncCell(this.trash);
1178+
await storage.syncCell(this.trashedCharms);
11871179

11881180
const id = getEntityId(idOrCharm);
11891181
if (!id) return false;

0 commit comments

Comments
 (0)