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

Commit

Permalink
refactor(database): break setup into smaller functions
Browse files Browse the repository at this point in the history
  • Loading branch information
adriancarriger committed May 22, 2017
1 parent 3f78a00 commit 64c83fe
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
40 changes: 31 additions & 9 deletions src/database/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,16 +314,22 @@ export class AngularFireOfflineDatabase {
options.preserveSnapshot = true;
// Get Firebase ref
const ref: FirebaseListObservable<any[]> = this.af.list(key, options);
// Create cache
this.listCache[key] = {
loaded: false,
offlineInit: false,
sub: new InternalListObservable(ref, this.localUpdateService)
};

// Create cache if none exists
if (!(key in this.listCache)) {
this.listCache[key] = {
loaded: false,
offlineInit: false,
sub: new InternalListObservable(ref, this.localUpdateService),
options: [],
firebaseOptions: undefined
};
}
// Store options
this.listCache[key].options.push(options);
// Firebase
this.getListFirebase(key, ref, options);

if (this.optionsHaveChanged(key)) {
this.getListFirebase(key, ref, options);
}
// Local
this.getListLocal(key);
}
Expand All @@ -343,6 +349,22 @@ export class AngularFireOfflineDatabase {
}
});
}
private optionsHaveChanged(key) {
const initialOptions = this.listCache[key].firebaseOptions;
// Base options
const newOptions = {
preserveSnapshot: true,
query: { }
};

// Get latest set of options
const optionsLength = this.listCache[key].options.length;
const latestOptions = this.listCache[key].options[optionsLength - 1];
if (latestOptions.query) { newOptions.query = latestOptions.query; }

this.listCache[key].firebaseOptions = newOptions;
return JSON.stringify(initialOptions) !== JSON.stringify(newOptions);
}
}
/**
* Utility function used to check if an value exists.
Expand Down
3 changes: 3 additions & 0 deletions src/database/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ReplaySubject, Observable } from 'rxjs';
import { FirebaseListFactoryOpts } from 'angularfire2/interfaces';

/**
* Each cacheItem is related to a Firebase reference.
Expand All @@ -10,6 +11,8 @@ export interface AngularFireOfflineCache {
offlineInit: boolean;
loaded: boolean;
sub: any;
options?: FirebaseListFactoryOpts[];
firebaseOptions?: FirebaseListFactoryOpts;
};
}

Expand Down

0 comments on commit 64c83fe

Please sign in to comment.