Skip to content

Commit 6245f47

Browse files
committed
refactor(db): sync loaded events
1 parent 887220c commit 6245f47

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angularfire2",
3-
"version": "4.0.0-rc.3",
3+
"version": "4.0.0-rc.3-exp.2",
44
"description": "The official library of Firebase and Angular.",
55
"private": true,
66
"scripts": {

src/database/list/loaded.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@ import { DatabaseQuery, ChildEvent, DatabaseSnapshot } from '../interfaces';
22
import { fromRef } from '../observable/fromRef';
33
import { createListSnapshotChanges } from './snapshot-changes';
44
import { Observable } from 'rxjs/Observable';
5-
import 'rxjs/add/operator/skipUntil';
65
import 'rxjs/add/operator/skipWhile';
76
import 'rxjs/add/operator/withLatestFrom';
8-
import 'rxjs/add/operator/do';
97
import 'rxjs/add/operator/filter';
108
import { database } from 'firebase/app';
119

@@ -22,7 +20,7 @@ export function createLoadedChanges(query: DatabaseQuery) {
2220
// Create an observable of loaded values to retrieve the
2321
// known dataset. This will allow us to know what key to
2422
// emit the "whole" array at when listening for child events.
25-
const loaded$ = fromRef(query, 'value', 'once')
23+
const loaded$ = fromRef(query, 'value')
2624
.map(data => {
2725
// Store the last key in the data set
2826
let lastKeyToLoad;
@@ -35,21 +33,21 @@ export function createLoadedChanges(query: DatabaseQuery) {
3533
});
3634
return function snapshotChanges(events?: ChildEvent[]) {
3735
const snapChanges$ = createListSnapshotChanges(query)(events);
38-
return snapChanges$
39-
.withLatestFrom(loaded$)
36+
return loaded$
37+
.withLatestFrom(snapChanges$)
4038
// Get the latest values from the "loaded" and "child" datasets
4139
// This way
42-
.map(([snaps, loaded]) => {
40+
.map(([loaded, snaps]) => {
4341
// Store the last key in the data set
4442
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 }
4846
})
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
5149
// we can assume the dataset is "whole".
52-
.skipWhile(meta => meta.latestKey !== meta.lastKeyToLoad)
50+
.skipWhile(meta => meta.loadedKeys.indexOf(meta.lastKeyToLoad) === -1)
5351
// Pluck off the meta data because the user only cares
5452
// to iterate through the snapshots
5553
.map(meta => meta.snaps);

0 commit comments

Comments
 (0)