2017-09-26 13:57:25 +00:00
|
|
|
/**
|
|
|
|
* @flow
|
|
|
|
* QuerySnapshot representation wrapper
|
|
|
|
*/
|
2017-11-17 14:22:46 +00:00
|
|
|
import DocumentChange from './DocumentChange';
|
|
|
|
import DocumentSnapshot from './DocumentSnapshot';
|
2017-09-26 13:57:25 +00:00
|
|
|
|
2017-11-17 11:07:52 +00:00
|
|
|
import type Firestore from './';
|
2018-01-25 18:25:39 +00:00
|
|
|
import type {
|
2018-02-14 13:00:19 +00:00
|
|
|
NativeDocumentChange,
|
|
|
|
NativeDocumentSnapshot,
|
|
|
|
SnapshotMetadata,
|
|
|
|
} from './types';
|
2017-11-17 11:07:52 +00:00
|
|
|
import type Query from './Query';
|
2017-09-26 13:57:25 +00:00
|
|
|
|
2018-02-14 13:00:19 +00:00
|
|
|
type NativeQuerySnapshot = {
|
|
|
|
changes: NativeDocumentChange[],
|
|
|
|
documents: NativeDocumentSnapshot[],
|
|
|
|
metadata: SnapshotMetadata,
|
2018-01-25 18:25:39 +00:00
|
|
|
};
|
2017-09-26 13:57:25 +00:00
|
|
|
|
2017-11-17 11:07:52 +00:00
|
|
|
/**
|
2017-09-26 13:57:25 +00:00
|
|
|
* @class QuerySnapshot
|
|
|
|
*/
|
|
|
|
export default class QuerySnapshot {
|
|
|
|
_changes: DocumentChange[];
|
|
|
|
_docs: DocumentSnapshot[];
|
2018-02-14 13:00:19 +00:00
|
|
|
_metadata: SnapshotMetadata;
|
2017-09-26 13:57:25 +00:00
|
|
|
_query: Query;
|
|
|
|
|
2018-01-25 18:25:39 +00:00
|
|
|
constructor(
|
|
|
|
firestore: Firestore,
|
|
|
|
query: Query,
|
2018-02-14 13:00:19 +00:00
|
|
|
nativeData: NativeQuerySnapshot
|
2018-01-25 18:25:39 +00:00
|
|
|
) {
|
|
|
|
this._changes = nativeData.changes.map(
|
|
|
|
change => new DocumentChange(firestore, change)
|
|
|
|
);
|
|
|
|
this._docs = nativeData.documents.map(
|
|
|
|
doc => new DocumentSnapshot(firestore, doc)
|
|
|
|
);
|
2017-10-05 09:18:24 +00:00
|
|
|
this._metadata = nativeData.metadata;
|
2017-09-26 13:57:25 +00:00
|
|
|
this._query = query;
|
|
|
|
}
|
|
|
|
|
|
|
|
get docChanges(): DocumentChange[] {
|
|
|
|
return this._changes;
|
|
|
|
}
|
|
|
|
|
|
|
|
get docs(): DocumentSnapshot[] {
|
|
|
|
return this._docs;
|
|
|
|
}
|
|
|
|
|
|
|
|
get empty(): boolean {
|
|
|
|
return this._docs.length === 0;
|
|
|
|
}
|
|
|
|
|
2018-02-14 13:00:19 +00:00
|
|
|
get metadata(): SnapshotMetadata {
|
2017-10-05 09:18:24 +00:00
|
|
|
return this._metadata;
|
2017-09-26 13:57:25 +00:00
|
|
|
}
|
|
|
|
|
2017-11-17 11:07:52 +00:00
|
|
|
get query(): Query {
|
|
|
|
return this._query;
|
|
|
|
}
|
|
|
|
|
2017-09-26 13:57:25 +00:00
|
|
|
get size(): number {
|
|
|
|
return this._docs.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
forEach(callback: DocumentSnapshot => any) {
|
|
|
|
// TODO: Validation
|
|
|
|
// validate.isFunction('callback', callback);
|
|
|
|
|
2018-01-25 18:25:39 +00:00
|
|
|
this._docs.forEach(doc => {
|
2017-09-26 13:57:25 +00:00
|
|
|
callback(doc);
|
2017-11-17 11:07:52 +00:00
|
|
|
});
|
2017-09-26 13:57:25 +00:00
|
|
|
}
|
|
|
|
}
|