Skip to content
This repository was archived by the owner on Feb 18, 2021. It is now read-only.

Commit 2ebe8f9

Browse files
author
stefanwullems
committed
cleaned up subscribe method of CacheObserver
1 parent 41d9635 commit 2ebe8f9

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

src/CacheObserver/CacheManager.ts

+16-4
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ export class CacheManager extends Observable<Data> {
1919
subscribe (id: string, listener: Listener<Data>) {
2020

2121
// Should stay above the super.subscribe call since it depends on the length of subscriptions
22-
if (Object.keys(this._subscriptions).length === 0) {
23-
this._store.on('transform', this._compare)
22+
if (this._hasNoSubscribers()) {
23+
this._subscribeToCacheUpdates()
2424
}
2525

2626
const unsubscribe = super.subscribe(id, listener)
@@ -29,12 +29,24 @@ export class CacheManager extends Observable<Data> {
2929
unsubscribe()
3030

3131
// Should stay below the super.subscribe call since it depends on the length of subscriptions
32-
if (Object.keys(this._subscriptions).length === 0) {
33-
this._store.off('transform', this._compare)
32+
if (this._hasNoSubscribers()) {
33+
this._unsubscribeFromCacheUpdates()
3434
}
3535
}
3636
}
3737

38+
_hasNoSubscribers () {
39+
return Object.keys(this._subscriptions).length === 0
40+
}
41+
42+
_subscribeToCacheUpdates () {
43+
this._store.on('transform', this._compare)
44+
}
45+
46+
_unsubscribeFromCacheUpdates () {
47+
this._store.off('transform', this._compare)
48+
}
49+
3850
query (queryOrQueries: Query | Queries): Data {
3951
const termsOrExpression = getTermsOrExpression(queryOrQueries)
4052

0 commit comments

Comments
 (0)