utils.js (1293B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */ 4 5 const DevToolsUtils = require("resource://devtools/shared/DevToolsUtils.js"); 6 7 /** 8 * Utils for utils, by utils 9 * 10 * @module utils/utils 11 */ 12 13 /** 14 * @memberof utils/utils 15 * @static 16 */ 17 export function handleError(err) { 18 console.log("ERROR: ", err); 19 } 20 21 /** 22 * @memberof utils/utils 23 * @static 24 */ 25 export function promisify(context, method, ...args) { 26 return new Promise((resolve, reject) => { 27 args.push(response => { 28 if (response.error) { 29 reject(response); 30 } else { 31 resolve(response); 32 } 33 }); 34 method.apply(context, args); 35 }); 36 } 37 38 /** 39 * @memberof utils/utils 40 * @static 41 */ 42 export function endTruncateStr(str, size) { 43 if (str.length > size) { 44 return `…${str.slice(str.length - size)}`; 45 } 46 return str; 47 } 48 49 export function waitForMs(ms) { 50 return new Promise(resolve => setTimeout(resolve, ms)); 51 } 52 53 export async function saveAsLocalFile(content, fileName) { 54 if (content.type !== "text") { 55 return null; 56 } 57 58 const data = new TextEncoder().encode(content.value); 59 return DevToolsUtils.saveAs(window, data, fileName); 60 }