summaryrefslogtreecommitdiffstats
path: root/devtools/shared/async-utils.js
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
commit26a029d407be480d791972afb5975cf62c9360a6 (patch)
treef435a8308119effd964b339f76abb83a57c29483 /devtools/shared/async-utils.js
parentInitial commit. (diff)
downloadfirefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz
firefox-26a029d407be480d791972afb5975cf62c9360a6.zip
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'devtools/shared/async-utils.js')
-rw-r--r--devtools/shared/async-utils.js71
1 files changed, 71 insertions, 0 deletions
diff --git a/devtools/shared/async-utils.js b/devtools/shared/async-utils.js
new file mode 100644
index 0000000000..cb1fee1a4f
--- /dev/null
+++ b/devtools/shared/async-utils.js
@@ -0,0 +1,71 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+/**
+ * Helpers for async functions. An async function returns a Promise for the
+ * resolution of the function. When the function returns, the promise is
+ * resolved with the returned value. If it throws the promise rejects with
+ * the thrown error.
+ */
+
+/**
+ * Adds an event listener to the given element, and then removes its event
+ * listener once the event is called, returning the event object as a promise.
+ * @param Element element
+ * The DOM element to listen on
+ * @param String event
+ * The name of the event type to listen for
+ * @param Boolean useCapture
+ * Should we initiate the capture phase?
+ * @return Promise
+ * The promise resolved with the event object when the event first
+ * happens
+ */
+exports.listenOnce = function listenOnce(element, event, useCapture) {
+ return new Promise(function (resolve, reject) {
+ const onEvent = function (ev) {
+ element.removeEventListener(event, onEvent, useCapture);
+ resolve(ev);
+ };
+ element.addEventListener(event, onEvent, useCapture);
+ });
+};
+
+// Return value when `safeAsyncMethod` catches an error.
+const SWALLOWED_RET = Symbol("swallowed");
+
+/**
+ * Wraps the provided async method in a try/catch block.
+ * If an error is caught while running the method, check the provided condition
+ * to decide whether the error should bubble or not.
+ *
+ * @param {Function} asyncFn
+ * The async method to wrap.
+ * @param {Function} shouldSwallow
+ * Function that will run when an error is caught. If it returns true,
+ * the error will be swallowed. Otherwise, it will bubble up.
+ * @param {Mixed} retValue
+ * Optional value to return when an error is caught and is swallowed.
+ * @return {Function} The wrapped method.
+ */
+exports.safeAsyncMethod = function (
+ asyncFn,
+ shouldSwallow,
+ retValue = SWALLOWED_RET
+) {
+ return async function (...args) {
+ try {
+ const ret = await asyncFn(...args);
+ return ret;
+ } catch (e) {
+ if (shouldSwallow()) {
+ console.warn("Async method failed in safeAsyncMethod", e);
+ return retValue;
+ }
+ throw e;
+ }
+ };
+};