UNPKG

@simplito/privmx-webendpoint

Version:

PrivMX Web Endpoint library

46 lines (45 loc) 1.35 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MockEventQueue = void 0; class MockEventQueue { queue = []; resolveCommand = null; registeredChannels = new Set(); constructor() { } dispatchEvent(event) { if (this.registeredChannels.has(event.channel)) { this.queue.push(event); if (this.resolveCommand) { this.resolveCommand.resolver(); this.resolveCommand = null; } } } async waitEvent() { const lockPromise = new Promise((resolve) => { const event = this.queue.shift(); //if there is event in queue resolve immediately if (event) { resolve(event); } else { //if not, lock promise and unlock it on new event this.resolveCommand = { resolver: () => { const event = this.queue.shift(); if (event) { resolve(event); } } }; } }); return lockPromise; } async emitBreakEvent() { return new Promise((resolve) => { resolve; }); } } exports.MockEventQueue = MockEventQueue;