UNPKG

@ocap/util

Version:

utils shared across multiple forge js libs, works in both node.js and browser

44 lines (43 loc) 1.23 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Ready = void 0; const events_1 = __importDefault(require("events")); class Ready extends events_1.default { constructor() { super(); this.ready = false; this.readyCallbacks = []; this.readyMarks = {}; } markReady(mark) { if (this.ready) { return; } if (mark === undefined) { this.ready = true; this.readyCallbacks.forEach((x) => x()); this.emit('ready'); } else { // console.log('Ready.markReady', this.name, mark); this.readyMarks[mark] = true; this.ready = Object.values(this.readyMarks).every((x) => !!x); if (this.ready) { this.readyCallbacks.forEach((cb) => cb()); this.emit('ready'); } } } onReady(cb) { if (this.ready) { cb(); } else { this.readyCallbacks.push(cb); } } } exports.Ready = Ready;