Skip to content

Commit 9dbf6b0

Browse files
Adding forEach Helper (#1150)
1 parent 813a174 commit 9dbf6b0

File tree

6 files changed

+51
-46
lines changed

6 files changed

+51
-46
lines changed

packages/firestore/src/core/sync_engine.ts

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -679,20 +679,16 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
679679
queryView.targetId
680680
);
681681
this.limboDocumentRefs.removeReferencesForId(queryView.targetId);
682-
let p = PersistencePromise.resolve();
683-
limboKeys.forEach(limboKey => {
684-
p = p.next(() => {
685-
return this.limboDocumentRefs
686-
.containsKey(null, limboKey)
687-
.next(isReferenced => {
688-
if (!isReferenced) {
689-
// We removed the last reference for this key
690-
this.removeLimboTarget(limboKey);
691-
}
692-
});
693-
});
694-
});
695-
await p.toPromise();
682+
await PersistencePromise.forEach(limboKeys.toArray(), limboKey => {
683+
return this.limboDocumentRefs
684+
.containsKey(null, limboKey)
685+
.next(isReferenced => {
686+
if (!isReferenced) {
687+
// We removed the last reference for this key
688+
this.removeLimboTarget(limboKey);
689+
}
690+
});
691+
}).toPromise();
696692
}
697693
}
698694

packages/firestore/src/local/indexeddb_persistence.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -369,14 +369,12 @@ export class IndexedDbPersistence implements Persistence {
369369
client => activeClients.indexOf(client) === -1
370370
);
371371
})
372-
.next(() => {
372+
.next(() =>
373373
// Delete metadata for clients that are no longer considered active.
374-
let p = PersistencePromise.resolve();
375-
inactiveClients.forEach(inactiveClient => {
376-
p = p.next(() => metadataStore.delete(inactiveClient.clientId));
377-
});
378-
return p;
379-
})
374+
PersistencePromise.forEach(inactiveClients, inactiveClient =>
375+
metadataStore.delete(inactiveClient.clientId)
376+
)
377+
)
380378
.next(() => {
381379
// Retrieve the minimum change ID from the set of active clients.
382380

packages/firestore/src/local/local_store.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -301,13 +301,11 @@ export class LocalStore {
301301
return PersistencePromise.resolve([]);
302302
}
303303
})
304-
.next(ackedBatches => {
305-
let p = PersistencePromise.resolve();
306-
ackedBatches.forEach(batch => {
307-
p = p.next(() => this.mutationQueue.removeMutationBatch(txn, batch));
308-
});
309-
return p;
310-
});
304+
.next(ackedBatches =>
305+
PersistencePromise.forEach(ackedBatches, batch =>
306+
this.mutationQueue.removeMutationBatch(txn, batch)
307+
)
308+
);
311309
}
312310

313311
/* Accept locally generated Mutations and commit them to storage. */
@@ -979,17 +977,15 @@ export class LocalStore {
979977
batches: MutationBatch[]
980978
): PersistencePromise<DocumentKeySet> {
981979
let affectedDocs = documentKeySet();
982-
983-
let p = PersistencePromise.resolve();
984980
for (const batch of batches) {
985981
for (const mutation of batch.mutations) {
986982
const key = mutation.key;
987983
affectedDocs = affectedDocs.add(key);
988984
}
989-
p = p.next(() => this.mutationQueue.removeMutationBatch(txn, batch));
990985
}
991-
992-
return p.next(() => affectedDocs);
986+
return PersistencePromise.forEach(batches, batch =>
987+
this.mutationQueue.removeMutationBatch(txn, batch)
988+
).next(() => affectedDocs);
993989
}
994990

995991
private applyWriteToRemoteDocuments(

packages/firestore/src/local/persistence_promise.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,4 +205,15 @@ export class PersistencePromise<T> {
205205
return results;
206206
});
207207
}
208+
209+
static forEach<T>(
210+
elements: T[],
211+
callback: (T) => PersistencePromise<void>
212+
): PersistencePromise<void> {
213+
let p = PersistencePromise.resolve();
214+
for (const element of elements) {
215+
p = p.next(() => callback(element));
216+
}
217+
return p;
218+
}
208219
}

packages/firestore/test/unit/local/indexeddb_persistence.test.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -284,11 +284,9 @@ describe('IndexedDbSchema: createOrUpgradeDb', () => {
284284
const sdb = new SimpleDb(db);
285285
return sdb.runTransaction('readwrite', [DbMutationBatch.store], txn => {
286286
const store = txn.store(DbMutationBatch.store);
287-
let p = PersistencePromise.resolve();
288-
for (const testMutation of testMutations) {
289-
p = p.next(() => store.put(testMutation));
290-
}
291-
return p;
287+
return PersistencePromise.forEach(testMutations, testMutation =>
288+
store.put(testMutation)
289+
);
292290
});
293291
}).then(() =>
294292
withDb(4, db => {
@@ -300,14 +298,11 @@ describe('IndexedDbSchema: createOrUpgradeDb', () => {
300298
const store = txn.store<DbMutationBatchKey, DbMutationBatch>(
301299
DbMutationBatch.store
302300
);
303-
let p = PersistencePromise.resolve();
304-
for (const testMutation of testMutations) {
305-
p = p.next(() =>
306-
store.get(testMutation.batchId).next(mutationBatch => {
307-
expect(mutationBatch).to.deep.equal(testMutation);
308-
})
309-
);
310-
}
301+
let p = PersistencePromise.forEach(testMutations, testMutation =>
302+
store.get(testMutation.batchId).next(mutationBatch => {
303+
expect(mutationBatch).to.deep.equal(testMutation);
304+
})
305+
);
311306
p = p.next(() => {
312307
store
313308
.add({} as any) // tslint:disable-line:no-any

packages/firestore/test/unit/local/persistence_promise.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,4 +213,13 @@ describe('PersistencePromise', () => {
213213
})
214214
.toPromise();
215215
});
216+
217+
it('executes forEach in order', async () => {
218+
let result = '';
219+
await PersistencePromise.forEach(['a', 'b', 'c'], el => {
220+
result += el;
221+
return PersistencePromise.resolve();
222+
}).toPromise;
223+
expect(result).to.equal('abc');
224+
});
216225
});

0 commit comments

Comments
 (0)