SimpleDBUtils.sys.mjs (1418B)
1 /** 2 * Any copyright is dedicated to the Public Domain. 3 * http://creativecommons.org/publicdomain/zero/1.0/ 4 */ 5 6 import { RequestError } from "resource://testing-common/dom/quota/test/modules/RequestError.sys.mjs"; 7 8 export const SimpleDBUtils = { 9 createConnection(principal) { 10 const connection = Cc["@mozilla.org/dom/sdb-connection;1"].createInstance( 11 Ci.nsISDBConnection 12 ); 13 14 connection.init(principal); 15 16 return connection; 17 }, 18 19 /** 20 * Handles the completion of a request, awaiting the callback to be called 21 * before proceeding. 22 * 23 * This function is designed to handle requests of the type `nsISDBRequest` 24 * 25 * These requests are typically returned by the connection. 26 * 27 * @param {object} request 28 * The request object, which must have a callback property and 29 * result-related properties (e.g., resultCode, resultName). 30 * @returns {Promise} 31 * Resolves with the request's result when the operation is successful. 32 * @throws {RequestError} 33 * If the request's resultCode is not `Cr.NS_OK`, indicating an error in 34 * the request. 35 */ 36 async requestFinished(request) { 37 await new Promise(function (resolve) { 38 request.callback = function () { 39 resolve(); 40 }; 41 }); 42 43 if (request.resultCode !== Cr.NS_OK) { 44 throw new RequestError(request.resultCode, request.resultName); 45 } 46 47 return request.result; 48 }, 49 };