Skip to content

Commit

Permalink
Merge pull request #365 from margelo/@szymon/use_map_for_pending_promise
Browse files Browse the repository at this point in the history
Use map for keeping pending promises
  • Loading branch information
mountiny authored Sep 28, 2023
2 parents e48a33f + ba23226 commit eda21d0
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions lib/OnyxCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ class OnyxCache {
/**
* @private
* Captured pending tasks for already running storage methods
* @type {Record<string, Promise>}
* Using a map yields better performance on operations such a delete
* https://www.zhenghao.io/posts/object-vs-map
* @type {Map<string, Promise>}
*/
this.pendingPromises = {};
this.pendingPromises = new Map();

// bind all public methods to prevent problems with `this`
_.bindAll(
Expand Down Expand Up @@ -133,7 +135,7 @@ class OnyxCache {
* @returns {*}
*/
hasPendingTask(taskName) {
return isDefined(this.pendingPromises[taskName]);
return isDefined(this.pendingPromises.get(taskName));
}

/**
Expand All @@ -145,7 +147,7 @@ class OnyxCache {
* @returns {Promise<T>}
*/
getTaskPromise(taskName) {
return this.pendingPromises[taskName];
return this.pendingPromises.get(taskName);
}

/**
Expand All @@ -157,11 +159,13 @@ class OnyxCache {
* @returns {Promise<T>}
*/
captureTask(taskName, promise) {
this.pendingPromises[taskName] = promise.finally(() => {
delete this.pendingPromises[taskName];
const returnPromise = promise.finally(() => {
this.pendingPromises.delete(taskName);
});

return this.pendingPromises[taskName];
this.pendingPromises.set(taskName, returnPromise);

return returnPromise;
}

/**
Expand Down

0 comments on commit eda21d0

Please sign in to comment.