From ff2599a4e71363d0555c7cd7cbe3a211fbffaa41 Mon Sep 17 00:00:00 2001 From: adriancarriger Date: Sun, 21 May 2017 19:11:56 -0500 Subject: [PATCH] refactor(database): prepare for multiple queries --- src/database/database.spec.ts | 11 +++++++++-- src/database/database.ts | 19 ++++++++++++------- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/database/database.spec.ts b/src/database/database.spec.ts index ef50644..f62838c 100644 --- a/src/database/database.spec.ts +++ b/src/database/database.spec.ts @@ -42,9 +42,15 @@ describe('Service: AngularFireOfflineDatabase', () => { let newValue = [ { val: () => { return 'xyz'; }, getPriority: () => {} } ]; + const options = { + query: { + orderByPriority: true + } + }; service.processing.current = false; - service.list(key).subscribe(list => { + service.list(key, options).subscribe(list => { expect(list[0].$value).toBe('xyz'); + expect(list[0].$exists()).toBe(true); done(); }); expect(service.listCache[key].loaded).toBe(false); @@ -271,7 +277,8 @@ describe('Service: AngularFireOfflineDatabase', () => { service.listCache[key] = { loaded: false, offlineInit: false, - sub: new MockInternalListObservable() + sub: new MockInternalListObservable(), + options: [] }; service.processing.current = true; setTimeout(() => { diff --git a/src/database/database.ts b/src/database/database.ts index b5d67cc..8ba872b 100644 --- a/src/database/database.ts +++ b/src/database/database.ts @@ -116,7 +116,7 @@ export class AngularFireOfflineDatabase { * [valid queries](https://goo.gl/iHiAuB) */ list(key: string, options?: FirebaseListFactoryOpts): AfoListObservable { - if (!(key in this.listCache)) { this.setupList(key, options); } + this.setupList(key, options); return new AfoListObservable(this.listCache[key].sub, options); } /** @@ -152,8 +152,16 @@ export class AngularFireOfflineDatabase { this.listCache = {}; this.localForage.clear(); }; - private getListFirebase(key: string, ref, options) { + private getListFirebase(key: string) { + const options = this.listCache[key].firebaseOptions; const usePriority = options && options.query && options.query.orderByPriority; + // Get Firebase ref + const ref: FirebaseListObservable = this.af.list(key, options); + // Create cache observable if none exists + if (!this.listCache[key].sub) { + this.listCache[key].sub = new InternalListObservable(ref, this.localUpdateService); + } + // Firebase const subscription = ref.subscribe(value => { this.listCache[key].loaded = true; const cacheValue = value.map(snap => { @@ -311,15 +319,12 @@ export class AngularFireOfflineDatabase { * @param options passed directly from {@link list}'s options param */ private setupList(key: string, options: FirebaseListFactoryOpts = {}) { - options.preserveSnapshot = true; - // Get Firebase ref - const ref: FirebaseListObservable = this.af.list(key, options); // Create cache if none exists if (!(key in this.listCache)) { this.listCache[key] = { loaded: false, offlineInit: false, - sub: new InternalListObservable(ref, this.localUpdateService), + sub: undefined, options: [], firebaseOptions: undefined }; @@ -328,7 +333,7 @@ export class AngularFireOfflineDatabase { this.listCache[key].options.push(options); // Firebase if (this.optionsHaveChanged(key)) { - this.getListFirebase(key, ref, options); + this.getListFirebase(key); } // Local this.getListLocal(key);