import util from 'util'; export const YOUTUBE_BASE = 'https://www.youtube.com/%s'; export const WATCH_BASE = util.format(YOUTUBE_BASE, 'watch?v=%s'); /** * Hold a queue of promises from which results can be extracted. */ export class PromiseQueue { constructor() { this.pending = new Set(); } /** * Add a new promise to the queue. * * @param Promise promise Promise to be added. */ add(promise) { const wrapped = promise.then(res => { this.pending.delete(wrapped); return res; }); this.pending.add(wrapped); } /** * Check whether there is no promise pending in the queue. * * @return boolean */ empty() { return this.pending.size === 0; } /** * Extract the next available result from the promise queue. * * @return any Next result. */ next() { return Promise.race(this.pending); } } /** * Escape double quotes in a string. */ export const escapeQuotes = str => str.replace(/"/g, '\\"'); /** * Make a promise that is resolved in a point in the future. * * @param number time Time to wait before resolving the promise (ms). * @return null */ export const sleep = time => new Promise(resolve => setTimeout(resolve, time));