Files
palemoon27/devtools/shared/async-utils.js
roytam1 d370ac889b import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1224115 - Don't flash rows when they're unhidden. r=mratcliffe (64cb22c4f3)
- Bug 1224115 - Fix keyboard navigation for filtered TableWidget. r=mratcliffe (494a5beda5)
- Bug 1184644 - Remove hardcoded colors from the SideMenu theme, and refresh it. r=vporof (9378d86abf)
- Bug 1253981 - Fix out of view breakpoint checkboxes in debugger. r=bgrins (07bbbf2611)
- Bug 1258197 - Recordings list empty label text has a different background color than its parent container, r=jsantell (90f21f52d7)
- Bug 1257063 - Don't destruct the runnable inside the lock when TaskQueue::Dispatch fails. r=bobbyholley. (1f6b254bb0)
- Bug 1224115 - Tweak storage inspector styling for filtering functionality. r=mikeratcliffe (573cb5dc97)
- Bug 1202148 - Move current in only one location in Intervals.h, r=jya (2c98d86b10)
- Bug 1258673. Part 1 - cache mStreamOffset so we won't read at the wrong position when Other Read() interrupt the current Read(). r=jya. (87ab65cc30)
- Bug 1258673. Part 2 - since mStreamOffset is not updated until the end of MediaCacheStream::Read(), we have to pass stream offset to MediaCache::NoteBlockUsage explicitly to avoid hitting the assertion. r=jya. (f02806ea1c)
- Bug 1235350 - Storage Inspector needs a simplified inline editor r+pbro (615da8f863)
- Bug 1224545 - Remove async-utils/async method and use Task.async instead. r=jryans (37e2c7df8f)
- Bug 1171903 - Test for storage inspector endless scrolling. r=miker (cd07c4da21)
- Bug 1242832 - Fix intermittent browser_storage_overflow.js timeouts. r=pbro (dccdf54962)
- Bug 1224115 - Test for storage inspector search. r=mratcliffe (301c7af8c9)
- Bug 1233497 - Don't yield or resolve CPOWs from Tasks or Promises in devtools tests. r=jryans (ceed1b85cf)
- Bug 1229272 - Stop using CPOW when creating indexed DB in storage tests. r=mratcliffe (c49d24d48a)
- Bug 1231154 - Make cookies table fields editable via double-click in storage inspector r+pbro (572b6090e1)
- Bug 1231437 - Storage Inspector: context menu to remove cookie/storage item r=mratcliffe (d33b236c51)
- Bug 1231179 - Make sessionStorage entry rows editable via double-click in storage inspector r=pbro (c11b3a8c9b)
- Bug 1231155 - Make localstorage entry rows editable via double-click in storage inspector r=pbro (d1d012fc30)
- Bug 1231434 - Add 'Delete All' context menu entry to storage inspector. r=mratcliffe (68985287c8)
- Bug 1258114 - use invisible borders to drag devtools splitters on both sides;r=bgrins (88108f3e48)
- Bug 1248447 - Stop grafting prototypes in storage.js::patchMetadataMapsAndProtos() r+pbro (8b2f5af32d)
- Bug 1237915 - Fix devtools storage interface to use the correct user context id when opening indexdb connections. r=mratcliffe (16bd8ea657)
- Bug 1262766 - Storage Inspector breaks down when Cache Storage throws a DOM security error. r=mratcliffe (b02b2b74c5)
2024-06-25 16:00:17 +08:00

109 lines
3.5 KiB
JavaScript

/* 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. Async functions are generator functions that are
* run by Tasks. 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.
*
* See Task documentation at https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/Task.jsm.
*/
var {Cu} = require("chrome");
var {Task} = require("resource://gre/modules/Task.jsm");
var Promise = require("promise");
/**
* Create an async function that only executes once per instance of an object.
* Once called on a given object, the same promise will be returned for any
* future calls for that object.
*
* @param Function func
* The generator function that to wrap as an async function.
* @return Function
* The async function.
*/
exports.asyncOnce = function asyncOnce(func) {
const promises = new WeakMap();
return function(...args) {
let promise = promises.get(this);
if (!promise) {
promise = Task.spawn(func.apply(this, args));
promises.set(this, promise);
}
return promise;
};
};
/**
* 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 nsIDOMElement 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) {
var onEvent = function(ev) {
element.removeEventListener(event, onEvent, useCapture);
resolve(ev);
}
element.addEventListener(event, onEvent, useCapture);
});
};
/**
* Call a function that expects a callback as the last argument and returns a
* promise for the result. This simplifies using callback APIs from tasks and
* async functions.
*
* @param Any obj
* The |this| value to call the function on.
* @param Function func
* The callback-expecting function to call.
* @param Array args
* Additional arguments to pass to the method.
* @return Promise
* The promise for the result. If the callback is called with only one
* argument, it is used as the resolution value. If there's multiple
* arguments, an array containing the arguments is the resolution value.
* If the method throws, the promise is rejected with the thrown value.
*/
function promisify(obj, func, args) {
return new Promise(resolve => {
args.push((...results) => {
resolve(results.length > 1 ? results : results[0]);
});
func.apply(obj, args);
});
}
/**
* Call a method that expects a callback as the last argument and returns a
* promise for the result.
*
* @see promisify
*/
exports.promiseInvoke = function promiseInvoke(obj, func, ...args) {
return promisify(obj, func, args);
};
/**
* Call a function that expects a callback as the last argument.
*
* @see promisify
*/
exports.promiseCall = function promiseCall(func, ...args) {
return promisify(undefined, func, args);
};