@@ -15,6 +15,7 @@ import {
15
15
DocImpl ,
16
16
EntityId ,
17
17
followAliases ,
18
+ getCell ,
18
19
getCellFromEntityId ,
19
20
getDoc ,
20
21
getEntityId ,
@@ -110,9 +111,6 @@ function filterOutEntity(
110
111
111
112
export class CharmManager {
112
113
private space : string ;
113
- private charmsDoc : DocImpl < CellLink [ ] > ;
114
- private pinned : DocImpl < CellLink [ ] > ;
115
- private trash : DocImpl < CellLink [ ] > ;
116
114
117
115
private charms : Cell < Cell < Charm > [ ] > ;
118
116
private pinnedCharms : Cell < Cell < Charm > [ ] > ;
@@ -121,32 +119,24 @@ export class CharmManager {
121
119
/**
122
120
* Promise resolved when the charm manager gets the charm list.
123
121
*/
124
- ready : Promise < void > ;
122
+ ready : Promise < any > ;
125
123
126
124
constructor (
127
125
private session : Session ,
128
126
) {
129
127
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 ) ;
134
128
135
129
storage . setSigner ( session . as ) ;
136
- this . pinnedCharms = this . pinned . asCell ( [ ] , undefined , charmListSchema ) ;
137
- this . trashedCharms = this . trash . asCell ( [ ] , undefined , charmListSchema ) ;
138
130
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 ) ;
143
134
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
+ ] ) ;
150
140
}
151
141
152
142
getSpace ( ) : string {
@@ -159,7 +149,7 @@ export class CharmManager {
159
149
}
160
150
161
151
async pin ( charm : Cell < Charm > ) {
162
- await storage . syncCell ( this . pinned ) ;
152
+ await storage . syncCell ( this . pinnedCharms ) ;
163
153
// Check if already pinned
164
154
if (
165
155
! filterOutEntity ( this . pinnedCharms , charm ) . some ( ( c ) =>
@@ -172,7 +162,7 @@ export class CharmManager {
172
162
}
173
163
174
164
async unpinById ( charmId : EntityId ) {
175
- await storage . syncCell ( this . pinned ) ;
165
+ await storage . syncCell ( this . pinnedCharms ) ;
176
166
const newPinnedCharms = filterOutEntity ( this . pinnedCharms , charmId ) ;
177
167
178
168
if ( newPinnedCharms . length !== this . pinnedCharms . get ( ) . length ) {
@@ -192,18 +182,18 @@ export class CharmManager {
192
182
}
193
183
194
184
getPinned ( ) : Cell < Cell < Charm > [ ] > {
195
- storage . syncCell ( this . pinned ) ;
185
+ storage . syncCell ( this . pinnedCharms ) ;
196
186
return this . pinnedCharms ;
197
187
}
198
188
199
189
getTrash ( ) : Cell < Cell < Charm > [ ] > {
200
- storage . syncCell ( this . trash ) ;
190
+ storage . syncCell ( this . trashedCharms ) ;
201
191
return this . trashedCharms ;
202
192
}
203
193
204
194
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 ) ;
207
197
208
198
const id = getEntityId ( idOrCharm ) ;
209
199
if ( ! id ) return false ;
@@ -227,7 +217,7 @@ export class CharmManager {
227
217
}
228
218
229
219
async emptyTrash ( ) {
230
- await storage . syncCell ( this . trash ) ;
220
+ await storage . syncCell ( this . trashedCharms ) ;
231
221
this . trashedCharms . set ( [ ] ) ;
232
222
await idle ( ) ;
233
223
return true ;
@@ -239,12 +229,12 @@ export class CharmManager {
239
229
getCharms ( ) : Cell < Cell < Charm > [ ] > {
240
230
// Start syncing if not already syncing. Will trigger a change to the list
241
231
// once loaded.
242
- storage . syncCell ( this . charmsDoc ) ;
232
+ storage . syncCell ( this . charms ) ;
243
233
return this . charms ;
244
234
}
245
235
246
236
private async add ( newCharms : Cell < Charm > [ ] ) {
247
- await storage . syncCell ( this . charmsDoc ) ;
237
+ await storage . syncCell ( this . charms ) ;
248
238
await idle ( ) ;
249
239
250
240
newCharms . forEach ( ( charm ) => {
@@ -1152,9 +1142,11 @@ export class CharmManager {
1152
1142
// note: removing a charm doesn't clean up the charm's cells
1153
1143
// Now moves the charm to trash instead of just removing it
1154
1144
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
+ ] ) ;
1158
1150
1159
1151
const id = getEntityId ( idOrCharm ) ;
1160
1152
if ( ! id ) return false ;
@@ -1183,7 +1175,7 @@ export class CharmManager {
1183
1175
1184
1176
// Permanently delete a charm (from trash or directly)
1185
1177
async permanentlyDelete ( idOrCharm : string | EntityId | Cell < Charm > ) {
1186
- await storage . syncCell ( this . trash ) ;
1178
+ await storage . syncCell ( this . trashedCharms ) ;
1187
1179
1188
1180
const id = getEntityId ( idOrCharm ) ;
1189
1181
if ( ! id ) return false ;
0 commit comments