diff options
Diffstat (limited to 'addons/mail/static/src/utils/utils.js')
| -rw-r--r-- | addons/mail/static/src/utils/utils.js | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/addons/mail/static/src/utils/utils.js b/addons/mail/static/src/utils/utils.js index 8112eeca980..f502fc8f326 100644 --- a/addons/mail/static/src/utils/utils.js +++ b/addons/mail/static/src/utils/utils.js @@ -15,6 +15,25 @@ const classPatchMap = new WeakMap(); const eventHandledWeakMap = new WeakMap(); /** + * Executes the provided functions in order, but with a potential delay between + * them if they take too much time. This is done in order to avoid blocking the + * main thread for too long. + * + * @param {function[]} functions + * @param {integer} [maxTimeFrame=100] time (in ms) until a delay is introduced + */ +async function executeGracefully(functions, maxTimeFrame = 100) { + let startDate = new Date(); + for (const func of functions) { + if (new Date() - startDate > maxTimeFrame) { + await new Promise(resolve => setTimeout(resolve)); + startDate = new Date(); + } + await func(); + } +} + +/** * Returns whether the given event has been handled with the given markName. * * @param {Event} ev @@ -147,6 +166,7 @@ function unpatchInstanceMethods(Class, patchName) { //------------------------------------------------------------------------------ return { + executeGracefully, isEventHandled, markEventHandled, nextTick, |
