all files / firebase/modules/firestore/ QuerySnapshot.js

100% Statements 14/14
100% Branches 0/0
100% Functions 11/11
100% Lines 14/14
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79                                                                      953× 1141×   953× 1074×   953× 953×             72×                         82×             857× 802×        
/**
 * @flow
 * QuerySnapshot representation wrapper
 */
import DocumentChange from './DocumentChange';
import DocumentSnapshot from './DocumentSnapshot';
 
import type Firestore from './';
import type {
  NativeDocumentChange,
  NativeDocumentSnapshot,
  SnapshotMetadata,
} from './types';
import type Query from './Query';
 
type NativeQuerySnapshot = {
  changes: NativeDocumentChange[],
  documents: NativeDocumentSnapshot[],
  metadata: SnapshotMetadata,
};
 
/**
 * @class QuerySnapshot
 */
export default class QuerySnapshot {
  _changes: DocumentChange[];
  _docs: DocumentSnapshot[];
  _metadata: SnapshotMetadata;
  _query: Query;
 
  constructor(
    firestore: Firestore,
    query: Query,
    nativeData: NativeQuerySnapshot
  ) {
    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);
 
    this._docs.forEach(doc => {
      callback(doc);
    });
  }
}