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

Commit 749214e

Browse files
fix(database): only update observers on unique value
fixes: #20
1 parent 1b40f9c commit 749214e

File tree

5 files changed

+36
-10
lines changed

5 files changed

+36
-10
lines changed

src/afo-list-observable.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ export class AfoListObservable<T> extends ReplaySubject<T> {
1818
* The current value of the {@link AfoListObservable}
1919
*/
2020
value: any;
21+
/**
22+
* The value preceding the current value.
23+
*/
24+
private previousValue: any;
2125
/**
2226
* Creates the {@link AfoListObservable}
2327
* @param ref a reference to the related FirebaseListObservable
@@ -63,6 +67,15 @@ export class AfoListObservable<T> extends ReplaySubject<T> {
6367
}
6468
});
6569
}
70+
/**
71+
* Only calls next if the new value is unique
72+
*/
73+
uniqueNext(newValue) {
74+
if (JSON.stringify(this.previousValue) !== JSON.stringify(newValue) ) {
75+
this.previousValue = newValue;
76+
this.next(newValue);
77+
}
78+
}
6679
/**
6780
* Wraps the AngularFire2 FirebaseListObservable [push](https://goo.gl/nTe7C0) method
6881
*
@@ -186,6 +199,6 @@ export class AfoListObservable<T> extends ReplaySubject<T> {
186199
* Sends the the current {@link value} to all subscribers
187200
*/
188201
private updateSubscribers() {
189-
this.next(this.value);
202+
this.uniqueNext(this.value);
190203
}
191204
}

src/afo-object-observable.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ export class AfoObjectObservable<T> extends ReplaySubject<T> {
1818
* The current value of the {@link AfoObjectObservable}
1919
*/
2020
value: any;
21+
/**
22+
* The value preceding the current value.
23+
*/
24+
private previousValue: any;
2125
/**
2226
* Creates the {@link AfoObjectObservable}
2327
* @param ref a reference to the related FirebaseObjectObservable
@@ -106,6 +110,15 @@ export class AfoObjectObservable<T> extends ReplaySubject<T> {
106110
this.offlineWrite(promise, 'update', [value]);
107111
return promise;
108112
}
113+
/**
114+
* Only calls next if the new value is unique
115+
*/
116+
uniqueNext(newValue) {
117+
if (JSON.stringify(this.previousValue) !== JSON.stringify(newValue) ) {
118+
this.previousValue = newValue;
119+
this.next(newValue);
120+
}
121+
}
109122
/**
110123
* Convenience method to save an offline write
111124
*
@@ -142,6 +155,6 @@ export class AfoObjectObservable<T> extends ReplaySubject<T> {
142155
* Sends the the current {@link value} to all subscribers
143156
*/
144157
private updateSubscribers() {
145-
this.next(unwrap(this.ref.$ref.key, this.value, () => this.value !== null));
158+
this.uniqueNext(unwrap(this.ref.$ref.key, this.value, () => this.value !== null));
146159
}
147160
}

src/database.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ export class AngularFireOfflineDatabase {
149149
if (this.processing.current) {
150150
this.processing.listCache[key] = cacheValue;
151151
} else {
152-
this.listCache[key].sub.next(cacheValue);
152+
this.listCache[key].sub.uniqueNext(cacheValue);
153153
}
154154
});
155155
}
@@ -162,7 +162,7 @@ export class AngularFireOfflineDatabase {
162162
this.processing.current = false;
163163
['list', 'object'].forEach(type => {
164164
Object.keys(this.processing[`${type}Cache`]).forEach(cacheKey => {
165-
this[`${type}Cache`][cacheKey].sub.next( this.processing[`${type}Cache`][cacheKey] );
165+
this[`${type}Cache`][cacheKey].sub.uniqueNext( this.processing[`${type}Cache`][cacheKey] );
166166
});
167167
});
168168
}
@@ -193,7 +193,7 @@ export class AngularFireOfflineDatabase {
193193
if (this.processing.current) {
194194
this.processing.objectCache[key] = cacheValue;
195195
} else {
196-
this.objectCache[key].sub.next( cacheValue );
196+
this.objectCache[key].sub.uniqueNext( cacheValue );
197197
}
198198
this.localForage.setItem(`read/object${key}`, snap.val());
199199
});
@@ -204,7 +204,7 @@ export class AngularFireOfflineDatabase {
204204
if (this.processing.current) {
205205
this.processing.objectCache[key] = cacheValue;
206206
} else {
207-
this.objectCache[key].sub.next( cacheValue );
207+
this.objectCache[key].sub.uniqueNext( cacheValue );
208208
}
209209
}
210210
});
@@ -276,7 +276,7 @@ export class AngularFireOfflineDatabase {
276276
if (this.processing.current) {
277277
this.processing.listCache[key] = cacheValue;
278278
} else {
279-
this.listCache[key].sub.next( cacheValue );
279+
this.listCache[key].sub.uniqueNext( cacheValue );
280280
}
281281
this.setList(key, value);
282282
});

tests/afo-list-observable.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ describe('List Observable', () => {
200200
expect(x[0].$exists()).toBe(true);
201201
done();
202202
});
203-
listObservable.next([]);
203+
listObservable.uniqueNext([]);
204204
});
205205
});
206206

tests/afo-object-observable.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ describe('Object Observable', () => {
8686
done();
8787
}
8888
});
89-
objectObservable.next('a value');
89+
objectObservable.uniqueNext('a value');
9090
});
9191

9292
it('should emulate a que for update', done => {
@@ -107,7 +107,7 @@ describe('Object Observable', () => {
107107
done();
108108
}
109109
});
110-
objectObservable.next({
110+
objectObservable.uniqueNext({
111111
title: 'old title',
112112
key2: 'other value'
113113
});

0 commit comments

Comments
 (0)