react-native-firebase/lib/modules/firestore/QuerySnapshot.js

79 lines
1.5 KiB
JavaScript
Raw Normal View History

/**
* @flow
* QuerySnapshot representation wrapper
*/
2017-11-17 14:22:46 +00:00
import DocumentChange from './DocumentChange';
import DocumentSnapshot from './DocumentSnapshot';
import type Firestore from './';
2018-01-25 18:25:39 +00:00
import type {
NativeDocumentChange,
NativeDocumentSnapshot,
SnapshotMetadata,
} from './types';
import type Query from './Query';
type NativeQuerySnapshot = {
changes: NativeDocumentChange[],
documents: NativeDocumentSnapshot[],
metadata: SnapshotMetadata,
2018-01-25 18:25:39 +00:00
};
/**
* @class QuerySnapshot
*/
export default class QuerySnapshot {
_changes: DocumentChange[];
_docs: DocumentSnapshot[];
_metadata: SnapshotMetadata;
_query: Query;
2018-01-25 18:25:39 +00:00
constructor(
firestore: Firestore,
query: Query,
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)
);
this._metadata = nativeData.metadata;
this._query = query;
}
get docChanges(): DocumentChange[] {
return this._changes;
}
get docs(): DocumentSnapshot[] {
return this._docs;
}
get empty(): boolean {
return this._docs.length === 0;
}
get metadata(): SnapshotMetadata {
return this._metadata;
}
get query(): Query {
return this._query;
}
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 => {
callback(doc);
});
}
}