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

Commit

Permalink
refactor(database): separate getting local and Firebase list data
Browse files Browse the repository at this point in the history
  • Loading branch information
adriancarriger committed May 21, 2017
1 parent d35317e commit 3f78a00
Showing 1 changed file with 23 additions and 21 deletions.
44 changes: 23 additions & 21 deletions src/database/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,32 @@ export class AngularFireOfflineDatabase {
this.listCache = {};
this.localForage.clear();
};
private getListFirebase(key: string, ref, options) {
const usePriority = options && options.query && options.query.orderByPriority;
const subscription = ref.subscribe(value => {
this.listCache[key].loaded = true;
const cacheValue = value.map(snap => {
const priority = usePriority ? snap.getPriority() : null;
return unwrap(snap.key, snap.val(), () => !isNil(snap.val()), priority);
});
if (this.processing.current) {
this.processing.listCache[key] = cacheValue;
} else {
this.listCache[key].sub.uniqueNext( cacheValue );
}
this.setList(key, value);
});

this.listCache[key].sub.subscribe({
complete: () => subscription.unsubscribe()
});
}
/**
* Retrives a list if locally stored on the device
* - Lists are stored as individual objects, to allow for better offline reuse.
* - Each locally stored list uses a map to stitch together the list from individual objects
*/
private getList(key: string) {
private getListLocal(key: string) {
this.localForage.getItem(`read/list${key}`).then(primaryValue => {
if (!this.listCache[key].loaded && primaryValue !== null) {
const promises = primaryValue.map(partialKey => {
Expand Down Expand Up @@ -283,7 +302,7 @@ export class AngularFireOfflineDatabase {
* - Sets up a {@link AngularFireOfflineCache} item that provides Firebase data
* - Subscribes to the list's Firebase reference
* - Gets the most recent locally stored non-null value and sends to all app subscribers
* via {@link getList}
* via {@link getListLocal}
* - When Firebase sends a value this {@link AngularFireOfflineCache} item is set to loaded,
* the new value is sent to all app subscribers, and the value is stored locally via
* {@link setList}
Expand All @@ -293,7 +312,6 @@ export class AngularFireOfflineDatabase {
*/
private setupList(key: string, options: FirebaseListFactoryOpts = {}) {
options.preserveSnapshot = true;
const usePriority = options && options.query && options.query.orderByPriority;
// Get Firebase ref
const ref: FirebaseListObservable<any[]> = this.af.list(key, options);
// Create cache
Expand All @@ -304,26 +322,10 @@ export class AngularFireOfflineDatabase {
};

// Firebase
const subscription = ref.subscribe(value => {
this.listCache[key].loaded = true;
const cacheValue = value.map(snap => {
const priority = usePriority ? snap.getPriority() : null;
return unwrap(snap.key, snap.val(), () => !isNil(snap.val()), priority);
});
if (this.processing.current) {
this.processing.listCache[key] = cacheValue;
} else {
this.listCache[key].sub.uniqueNext( cacheValue );
}
this.setList(key, value);
});

this.listCache[key].sub.subscribe({
complete: () => subscription.unsubscribe()
});
this.getListFirebase(key, ref, options);

// Local
this.getList(key);
this.getListLocal(key);
}
/**
* Processes cache items that require emulation
Expand Down

0 comments on commit 3f78a00

Please sign in to comment.