diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-02-25 00:15:17 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-02-25 00:15:17 +0300 |
commit | 0afa6fb9d922cb7e201e81b2b668258d3dcf9d46 (patch) | |
tree | 6cc932813286d30095068905cf30b45cbf609558 /app/assets/javascripts/lib | |
parent | 94ca43181461c52ca0381d414387b02e579dbc61 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/assets/javascripts/lib')
-rw-r--r-- | app/assets/javascripts/lib/utils/ignore_while_pending.js | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/app/assets/javascripts/lib/utils/ignore_while_pending.js b/app/assets/javascripts/lib/utils/ignore_while_pending.js new file mode 100644 index 00000000000..e85a573c8f2 --- /dev/null +++ b/app/assets/javascripts/lib/utils/ignore_while_pending.js @@ -0,0 +1,26 @@ +/** + * This will wrap the given function to make sure that it is only triggered once + * while executing asynchronously + * + * @param {Function} fn some function that returns a promise + * @returns A function that will only be triggered *once* while the promise is executing + */ +export const ignoreWhilePending = (fn) => { + const isPendingMap = new WeakMap(); + const defaultContext = {}; + + // We need this to be a function so we get the `this` + return function ignoreWhilePendingInner(...args) { + const context = this || defaultContext; + + if (isPendingMap.get(context)) { + return Promise.resolve(); + } + + isPendingMap.set(context, true); + + return fn.apply(this, args).finally(() => { + isPendingMap.delete(context); + }); + }; +}; |