Skip to content

Commit 059e9cb

Browse files
committed
Add onCacheUpdatedWrapped arg to ReplicaCache constructor
1 parent 21a6055 commit 059e9cb

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

src/replica/replica-cache.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,24 @@ export class ReplicaCache {
110110

111111
_isClosed = false;
112112

113+
_onFireCacheUpdatedsWrapper = (cb: () => void) => cb();
114+
113115
/**
114116
* Create a new ReplicaCache.
115117
* @param timeToLive - The number of milliseconds a cached document is considered valid for.
118+
* @param onCacheUpdatedWrapper - A function which wraps the firing of all callbacks. Useful for libraries with batching abstractions.
116119
*/
117-
constructor(replica: IReplica, timeToLive?: number) {
120+
constructor(
121+
replica: IReplica,
122+
timeToLive?: number,
123+
onCacheUpdatedWrapper?: (cb: () => void) => void,
124+
) {
118125
this._replica = replica;
119126
this._timeToLive = timeToLive || 1000;
127+
128+
if (onCacheUpdatedWrapper) {
129+
this._onFireCacheUpdatedsWrapper = onCacheUpdatedWrapper;
130+
}
120131
}
121132

122133
async close() {
@@ -442,8 +453,10 @@ export class ReplicaCache {
442453
_fireOnCacheUpdateds(entry: string) {
443454
this.version++;
444455

445-
this._onCacheUpdatedCallbacks.forEach((cb) => {
446-
cb(entry);
456+
this._onFireCacheUpdatedsWrapper(() => {
457+
this._onCacheUpdatedCallbacks.forEach((cb) => {
458+
cb(entry);
459+
});
447460
});
448461
}
449462

0 commit comments

Comments
 (0)