Skip to content

Commit 543bc77

Browse files
Add free functions
1 parent d32d192 commit 543bc77

File tree

1 file changed

+97
-104
lines changed

1 file changed

+97
-104
lines changed

packages/firestore/exp/src/api/snapshot.ts

Lines changed: 97 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -369,10 +369,16 @@ export class QuerySnapshot<T = DocumentData> {
369369
this._snapshot.docs.forEach(doc => {
370370
callback.call(
371371
thisArg,
372-
this._convertToDocumentSnapshot(
372+
new QueryDocumentSnapshot<T>(
373+
this._firestore,
374+
this._userDataWriter,
375+
doc.key,
373376
doc,
374-
this._snapshot.fromCache,
375-
this._snapshot.mutatedKeys.has(doc.key)
377+
new SnapshotMetadata(
378+
this._snapshot.mutatedKeys.has(doc.key),
379+
this._snapshot.fromCache
380+
),
381+
this.query._converter
376382
)
377383
);
378384
});
@@ -402,121 +408,108 @@ export class QuerySnapshot<T = DocumentData> {
402408
!this._cachedChanges ||
403409
this._cachedChangesIncludeMetadataChanges !== includeMetadataChanges
404410
) {
405-
this._cachedChanges = this.changesFromSnapshot(
406-
this._snapshot,
407-
includeMetadataChanges
408-
);
411+
this._cachedChanges = changesFromSnapshot(this, includeMetadataChanges);
409412
this._cachedChangesIncludeMetadataChanges = includeMetadataChanges;
410413
}
411414

412415
return this._cachedChanges;
413416
}
417+
}
414418

415-
/**
416-
* Calculates the array of DocumentChanges for a given ViewSnapshot.
417-
*
418-
* Exported for testing.
419-
*
420-
* @param snapshot The ViewSnapshot that represents the expected state.
421-
* @param includeMetadataChanges Whether to include metadata changes.
422-
* @param converter A factory function that returns a QueryDocumentSnapshot.
423-
* @return An object that matches the DocumentChange API.
424-
*/
425-
changesFromSnapshot(
426-
snapshot: ViewSnapshot,
427-
includeMetadataChanges: boolean
428-
): Array<DocumentChange<T>> {
429-
if (snapshot.oldDocs.isEmpty()) {
430-
// Special case the first snapshot because index calculation is easy and
431-
// fast
432-
let lastDoc: Document;
433-
let index = 0;
434-
return snapshot.docChanges.map(change => {
435-
const doc = this._convertToDocumentSnapshot(
419+
/** Calculates the array of DocumentChanges for a given ViewSnapshot. */
420+
export function changesFromSnapshot<T>(
421+
querySnapshot: QuerySnapshot<T>,
422+
includeMetadataChanges: boolean
423+
): Array<DocumentChange<T>> {
424+
if (querySnapshot._snapshot.oldDocs.isEmpty()) {
425+
// Special case the first snapshot because index calculation is easy and
426+
// fast
427+
let lastDoc: Document;
428+
let index = 0;
429+
return querySnapshot._snapshot.docChanges.map(change => {
430+
debugAssert(
431+
change.type === ChangeType.Added,
432+
'Invalid event type for first snapshot'
433+
);
434+
debugAssert(
435+
!lastDoc ||
436+
newQueryComparator(querySnapshot._snapshot.query)(
437+
lastDoc,
438+
change.doc
439+
) < 0,
440+
'Got added events in wrong order'
441+
);
442+
const doc = new QueryDocumentSnapshot<T>(
443+
querySnapshot._firestore,
444+
querySnapshot._userDataWriter,
445+
change.doc.key,
446+
change.doc,
447+
new SnapshotMetadata(
448+
querySnapshot._snapshot.mutatedKeys.has(change.doc.key),
449+
querySnapshot._snapshot.fromCache
450+
),
451+
querySnapshot.query._converter
452+
);
453+
lastDoc = change.doc;
454+
return {
455+
type: 'added' as DocumentChangeType,
456+
doc,
457+
oldIndex: -1,
458+
newIndex: index++
459+
};
460+
});
461+
} else {
462+
// A DocumentSet that is updated incrementally as changes are applied to use
463+
// to lookup the index of a document.
464+
let indexTracker = querySnapshot._snapshot.oldDocs;
465+
return querySnapshot._snapshot.docChanges
466+
.filter(
467+
change => includeMetadataChanges || change.type !== ChangeType.Metadata
468+
)
469+
.map(change => {
470+
const doc = new QueryDocumentSnapshot<T>(
471+
querySnapshot._firestore,
472+
querySnapshot._userDataWriter,
473+
change.doc.key,
436474
change.doc,
437-
snapshot.fromCache,
438-
snapshot.mutatedKeys.has(change.doc.key)
475+
new SnapshotMetadata(
476+
querySnapshot._snapshot.mutatedKeys.has(change.doc.key),
477+
querySnapshot._snapshot.fromCache
478+
),
479+
querySnapshot.query._converter
439480
);
440-
debugAssert(
441-
change.type === ChangeType.Added,
442-
'Invalid event type for first snapshot'
443-
);
444-
debugAssert(
445-
!lastDoc ||
446-
newQueryComparator(snapshot.query)(lastDoc, change.doc) < 0,
447-
'Got added events in wrong order'
448-
);
449-
lastDoc = change.doc;
481+
let oldIndex = -1;
482+
let newIndex = -1;
483+
if (change.type !== ChangeType.Added) {
484+
oldIndex = indexTracker.indexOf(change.doc.key);
485+
debugAssert(oldIndex >= 0, 'Index for document not found');
486+
indexTracker = indexTracker.delete(change.doc.key);
487+
}
488+
if (change.type !== ChangeType.Removed) {
489+
indexTracker = indexTracker.add(change.doc);
490+
newIndex = indexTracker.indexOf(change.doc.key);
491+
}
450492
return {
451-
type: 'added' as DocumentChangeType,
493+
type: resultChangeType(change.type),
452494
doc,
453-
oldIndex: -1,
454-
newIndex: index++
495+
oldIndex,
496+
newIndex
455497
};
456498
});
457-
} else {
458-
// A DocumentSet that is updated incrementally as changes are applied to use
459-
// to lookup the index of a document.
460-
let indexTracker = snapshot.oldDocs;
461-
return snapshot.docChanges
462-
.filter(
463-
change =>
464-
includeMetadataChanges || change.type !== ChangeType.Metadata
465-
)
466-
.map(change => {
467-
const doc = this._convertToDocumentSnapshot(
468-
change.doc,
469-
snapshot.fromCache,
470-
snapshot.mutatedKeys.has(change.doc.key)
471-
);
472-
let oldIndex = -1;
473-
let newIndex = -1;
474-
if (change.type !== ChangeType.Added) {
475-
oldIndex = indexTracker.indexOf(change.doc.key);
476-
debugAssert(oldIndex >= 0, 'Index for document not found');
477-
indexTracker = indexTracker.delete(change.doc.key);
478-
}
479-
if (change.type !== ChangeType.Removed) {
480-
indexTracker = indexTracker.add(change.doc);
481-
newIndex = indexTracker.indexOf(change.doc.key);
482-
}
483-
return {
484-
type: this.resultChangeType(change.type),
485-
doc,
486-
oldIndex,
487-
newIndex
488-
};
489-
});
490-
}
491-
}
492-
493-
resultChangeType(type: ChangeType): DocumentChangeType {
494-
switch (type) {
495-
case ChangeType.Added:
496-
return 'added';
497-
case ChangeType.Modified:
498-
case ChangeType.Metadata:
499-
return 'modified';
500-
case ChangeType.Removed:
501-
return 'removed';
502-
default:
503-
return fail('Unknown change type: ' + type);
504-
}
505499
}
500+
}
506501

507-
private _convertToDocumentSnapshot(
508-
doc: Document,
509-
fromCache: boolean,
510-
hasPendingWrites: boolean
511-
): QueryDocumentSnapshot<T> {
512-
return new QueryDocumentSnapshot<T>(
513-
this._firestore,
514-
this._userDataWriter,
515-
doc.key,
516-
doc,
517-
new SnapshotMetadata(hasPendingWrites, fromCache),
518-
this.query._converter
519-
);
502+
export function resultChangeType(type: ChangeType): DocumentChangeType {
503+
switch (type) {
504+
case ChangeType.Added:
505+
return 'added';
506+
case ChangeType.Modified:
507+
case ChangeType.Metadata:
508+
return 'modified';
509+
case ChangeType.Removed:
510+
return 'removed';
511+
default:
512+
return fail('Unknown change type: ' + type);
520513
}
521514
}
522515

0 commit comments

Comments
 (0)