@@ -2,10 +2,8 @@ import { DatabaseQuery, ChildEvent, DatabaseSnapshot } from '../interfaces';
2
2
import { fromRef } from '../observable/fromRef' ;
3
3
import { createListSnapshotChanges } from './snapshot-changes' ;
4
4
import { Observable } from 'rxjs/Observable' ;
5
- import 'rxjs/add/operator/skipUntil' ;
6
5
import 'rxjs/add/operator/skipWhile' ;
7
6
import 'rxjs/add/operator/withLatestFrom' ;
8
- import 'rxjs/add/operator/do' ;
9
7
import 'rxjs/add/operator/filter' ;
10
8
import { database } from 'firebase/app' ;
11
9
@@ -22,7 +20,7 @@ export function createLoadedChanges(query: DatabaseQuery) {
22
20
// Create an observable of loaded values to retrieve the
23
21
// known dataset. This will allow us to know what key to
24
22
// emit the "whole" array at when listening for child events.
25
- const loaded$ = fromRef ( query , 'value' , 'once' )
23
+ const loaded$ = fromRef ( query , 'value' )
26
24
. map ( data => {
27
25
// Store the last key in the data set
28
26
let lastKeyToLoad ;
@@ -35,21 +33,21 @@ export function createLoadedChanges(query: DatabaseQuery) {
35
33
} ) ;
36
34
return function snapshotChanges ( events ?: ChildEvent [ ] ) {
37
35
const snapChanges$ = createListSnapshotChanges ( query ) ( events ) ;
38
- return snapChanges $
39
- . withLatestFrom ( loaded $)
36
+ return loaded $
37
+ . withLatestFrom ( snapChanges $)
40
38
// Get the latest values from the "loaded" and "child" datasets
41
39
// This way
42
- . map ( ( [ snaps , loaded ] ) => {
40
+ . map ( ( [ loaded , snaps ] ) => {
43
41
// Store the last key in the data set
44
42
let lastKeyToLoad = loaded . lastKeyToLoad ;
45
- // Store the lastest key to load as a child event
46
- let latestKey = snaps [ snaps . length - 1 ] . key ;
47
- return { snaps, lastKeyToLoad, latestKey }
43
+ // Store all child keys loaded at this point
44
+ const loadedKeys = snaps . map ( snap => snap . key ) ;
45
+ return { snaps, lastKeyToLoad, loadedKeys }
48
46
} )
49
- // This is the magical part, only emit when the latest key
50
- // in the dataset matches the last known key . At this point
47
+ // This is the magical part, only emit when the last load key
48
+ // in the dataset has been loaded by a child event . At this point
51
49
// we can assume the dataset is "whole".
52
- . skipWhile ( meta => meta . latestKey !== meta . lastKeyToLoad )
50
+ . skipWhile ( meta => meta . loadedKeys . indexOf ( meta . lastKeyToLoad ) === - 1 )
53
51
// Pluck off the meta data because the user only cares
54
52
// to iterate through the snapshots
55
53
. map ( meta => meta . snaps ) ;
0 commit comments