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

Commit abeb837

Browse files
fix(observables): avoid circular stringify
related: #22
1 parent 5df2c91 commit abeb837

File tree

4 files changed

+22
-9
lines changed

4 files changed

+22
-9
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"typings": "index.d.ts",
3636
"homepage": "https://github.com/adriancarriger/angularfire2-offline",
3737
"dependencies": {
38+
"json-stringify-safe": "^5.0.1",
3839
"localforage": "1.5.0"
3940
},
4041
"peerDependencies": {

src/afo-list-observable.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { ReplaySubject } from 'rxjs';
33
import { unwrap } from './database';
44
import { OfflineWrite } from './offline-write';
55
import { LocalUpdateService } from './local-update-service';
6+
const stringify = require('json-stringify-safe');
67

78
export class AfoListObservable<T> extends ReplaySubject<T> {
89
/**
@@ -14,6 +15,10 @@ export class AfoListObservable<T> extends ReplaySubject<T> {
1415
* in {@link value} before being applied
1516
*/
1617
que = [];
18+
/**
19+
* Number of times updated
20+
*/
21+
updated: number;
1722
/**
1823
* The current value of the {@link AfoListObservable}
1924
*/
@@ -71,9 +76,10 @@ export class AfoListObservable<T> extends ReplaySubject<T> {
7176
* Only calls next if the new value is unique
7277
*/
7378
uniqueNext(newValue) {
74-
if (JSON.stringify(this.previousValue) !== JSON.stringify(newValue) ) {
79+
if (this.updated > 1 || (stringify(this.previousValue) !== stringify(newValue)) ) {
7580
this.previousValue = newValue;
7681
this.next(newValue);
82+
this.updated++;
7783
}
7884
}
7985
/**

src/afo-object-observable.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { ReplaySubject } from 'rxjs';
33
import { unwrap } from './database';
44
import { OfflineWrite } from './offline-write';
55
import { LocalUpdateService } from './local-update-service';
6+
const stringify = require('json-stringify-safe');
67

78
export class AfoObjectObservable<T> extends ReplaySubject<T> {
89
/**
@@ -14,6 +15,10 @@ export class AfoObjectObservable<T> extends ReplaySubject<T> {
1415
* in {@link value} before being applied
1516
*/
1617
que = [];
18+
/**
19+
* Number of times updated
20+
*/
21+
updated: number;
1722
/**
1823
* The current value of the {@link AfoObjectObservable}
1924
*/
@@ -114,9 +119,10 @@ export class AfoObjectObservable<T> extends ReplaySubject<T> {
114119
* Only calls next if the new value is unique
115120
*/
116121
uniqueNext(newValue) {
117-
if (JSON.stringify(this.previousValue) !== JSON.stringify(newValue) ) {
122+
if (this.updated > 1 || (stringify(this.previousValue) !== stringify(newValue) )) {
118123
this.previousValue = newValue;
119124
this.next(newValue);
125+
this.updated++;
120126
}
121127
}
122128
/**

yarn.lock

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2101,7 +2101,7 @@ json-stable-stringify@^1.0.1:
21012101
dependencies:
21022102
jsonify "~0.0.0"
21032103

2104-
json-stringify-safe@~5.0.1:
2104+
json-stringify-safe@^5.0.1, json-stringify-safe@~5.0.1:
21052105
version "5.0.1"
21062106
resolved "https://registry.yarnpkg.com/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz#1296a2d58fd45f19a0f6ce01d65701e2c735b6eb"
21072107

@@ -3433,15 +3433,15 @@ right-pad@^1.0.1:
34333433
version "1.0.1"
34343434
resolved "https://registry.yarnpkg.com/right-pad/-/right-pad-1.0.1.tgz#8ca08c2cbb5b55e74dafa96bf7fd1a27d568c8d0"
34353435

3436-
rimraf@2, rimraf@^2.3.3, rimraf@^2.4.4, rimraf@~2.5.1, rimraf@~2.5.4:
3437-
version "2.5.4"
3438-
resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-2.5.4.tgz#96800093cbf1a0c86bd95b4625467535c29dfa04"
3436+
rimraf@2, rimraf@^2.3.3, rimraf@^2.4.4, rimraf@^2.6.1:
3437+
version "2.6.1"
3438+
resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-2.6.1.tgz#c2338ec643df7a1b7fe5c54fa86f57428a55f33d"
34393439
dependencies:
34403440
glob "^7.0.5"
34413441

3442-
rimraf@^2.6.1:
3443-
version "2.6.1"
3444-
resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-2.6.1.tgz#c2338ec643df7a1b7fe5c54fa86f57428a55f33d"
3442+
rimraf@~2.5.1, rimraf@~2.5.4:
3443+
version "2.5.4"
3444+
resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-2.5.4.tgz#96800093cbf1a0c86bd95b4625467535c29dfa04"
34453445
dependencies:
34463446
glob "^7.0.5"
34473447

0 commit comments

Comments
 (0)