Skip to content
This repository has been archived by the owner on Sep 4, 2020. It is now read-only.

Commit

Permalink
refactor(database): prepare for multiple queries
Browse files Browse the repository at this point in the history
  • Loading branch information
adriancarriger committed May 22, 2017
1 parent 64c83fe commit ff2599a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
11 changes: 9 additions & 2 deletions src/database/database.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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(() => {
Expand Down
19 changes: 12 additions & 7 deletions src/database/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export class AngularFireOfflineDatabase {
* [valid queries](https://goo.gl/iHiAuB)
*/
list(key: string, options?: FirebaseListFactoryOpts): AfoListObservable<any[]> {
if (!(key in this.listCache)) { this.setupList(key, options); }
this.setupList(key, options);
return new AfoListObservable(this.listCache[key].sub, options);
}
/**
Expand Down Expand Up @@ -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<any[]> = 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 => {
Expand Down Expand Up @@ -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<any[]> = 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
};
Expand All @@ -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);
Expand Down

0 comments on commit ff2599a

Please sign in to comment.