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

Commit

Permalink
fix(database): only update observers on unique value
Browse files Browse the repository at this point in the history
fixes: #20
  • Loading branch information
adriancarriger committed Apr 30, 2017
1 parent 1b40f9c commit 749214e
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 10 deletions.
15 changes: 14 additions & 1 deletion src/afo-list-observable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ export class AfoListObservable<T> extends ReplaySubject<T> {
* The current value of the {@link AfoListObservable}
*/
value: any;
/**
* The value preceding the current value.
*/
private previousValue: any;
/**
* Creates the {@link AfoListObservable}
* @param ref a reference to the related FirebaseListObservable
Expand Down Expand Up @@ -63,6 +67,15 @@ export class AfoListObservable<T> extends ReplaySubject<T> {
}
});
}
/**
* Only calls next if the new value is unique
*/
uniqueNext(newValue) {
if (JSON.stringify(this.previousValue) !== JSON.stringify(newValue) ) {
this.previousValue = newValue;
this.next(newValue);
}
}
/**
* Wraps the AngularFire2 FirebaseListObservable [push](https://goo.gl/nTe7C0) method
*
Expand Down Expand Up @@ -186,6 +199,6 @@ export class AfoListObservable<T> extends ReplaySubject<T> {
* Sends the the current {@link value} to all subscribers
*/
private updateSubscribers() {
this.next(this.value);
this.uniqueNext(this.value);
}
}
15 changes: 14 additions & 1 deletion src/afo-object-observable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ export class AfoObjectObservable<T> extends ReplaySubject<T> {
* The current value of the {@link AfoObjectObservable}
*/
value: any;
/**
* The value preceding the current value.
*/
private previousValue: any;
/**
* Creates the {@link AfoObjectObservable}
* @param ref a reference to the related FirebaseObjectObservable
Expand Down Expand Up @@ -106,6 +110,15 @@ export class AfoObjectObservable<T> extends ReplaySubject<T> {
this.offlineWrite(promise, 'update', [value]);
return promise;
}
/**
* Only calls next if the new value is unique
*/
uniqueNext(newValue) {
if (JSON.stringify(this.previousValue) !== JSON.stringify(newValue) ) {
this.previousValue = newValue;
this.next(newValue);
}
}
/**
* Convenience method to save an offline write
*
Expand Down Expand Up @@ -142,6 +155,6 @@ export class AfoObjectObservable<T> extends ReplaySubject<T> {
* Sends the the current {@link value} to all subscribers
*/
private updateSubscribers() {
this.next(unwrap(this.ref.$ref.key, this.value, () => this.value !== null));
this.uniqueNext(unwrap(this.ref.$ref.key, this.value, () => this.value !== null));
}
}
10 changes: 5 additions & 5 deletions src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export class AngularFireOfflineDatabase {
if (this.processing.current) {
this.processing.listCache[key] = cacheValue;
} else {
this.listCache[key].sub.next(cacheValue);
this.listCache[key].sub.uniqueNext(cacheValue);
}
});
}
Expand All @@ -162,7 +162,7 @@ export class AngularFireOfflineDatabase {
this.processing.current = false;
['list', 'object'].forEach(type => {
Object.keys(this.processing[`${type}Cache`]).forEach(cacheKey => {
this[`${type}Cache`][cacheKey].sub.next( this.processing[`${type}Cache`][cacheKey] );
this[`${type}Cache`][cacheKey].sub.uniqueNext( this.processing[`${type}Cache`][cacheKey] );
});
});
}
Expand Down Expand Up @@ -193,7 +193,7 @@ export class AngularFireOfflineDatabase {
if (this.processing.current) {
this.processing.objectCache[key] = cacheValue;
} else {
this.objectCache[key].sub.next( cacheValue );
this.objectCache[key].sub.uniqueNext( cacheValue );
}
this.localForage.setItem(`read/object${key}`, snap.val());
});
Expand All @@ -204,7 +204,7 @@ export class AngularFireOfflineDatabase {
if (this.processing.current) {
this.processing.objectCache[key] = cacheValue;
} else {
this.objectCache[key].sub.next( cacheValue );
this.objectCache[key].sub.uniqueNext( cacheValue );
}
}
});
Expand Down Expand Up @@ -276,7 +276,7 @@ export class AngularFireOfflineDatabase {
if (this.processing.current) {
this.processing.listCache[key] = cacheValue;
} else {
this.listCache[key].sub.next( cacheValue );
this.listCache[key].sub.uniqueNext( cacheValue );
}
this.setList(key, value);
});
Expand Down
2 changes: 1 addition & 1 deletion tests/afo-list-observable.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ describe('List Observable', () => {
expect(x[0].$exists()).toBe(true);
done();
});
listObservable.next([]);
listObservable.uniqueNext([]);
});
});

Expand Down
4 changes: 2 additions & 2 deletions tests/afo-object-observable.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ describe('Object Observable', () => {
done();
}
});
objectObservable.next('a value');
objectObservable.uniqueNext('a value');
});

it('should emulate a que for update', done => {
Expand All @@ -107,7 +107,7 @@ describe('Object Observable', () => {
done();
}
});
objectObservable.next({
objectObservable.uniqueNext({
title: 'old title',
key2: 'other value'
});
Expand Down

0 comments on commit 749214e

Please sign in to comment.