UNPKG

@deepkit/core

Version:
70 lines 2.47 kB
"use strict"; /* * Deepkit Framework * Copyright (C) 2021 Deepkit UG, Marc J. Schmidt * * This program is free software: you can redistribute it and/or modify * it under the terms of the MIT License. * * You should have received a copy of the MIT License along with this program. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.AsyncEventEmitter = exports.AsyncEmitterEvent = exports.__ΩAsyncEventSubscription = void 0; const array_js_1 = require("./array.js"); const __ΩAsyncSubscriber = ['T', 'event', '', 'b!Pe"!2"P$`$J/#']; const __ΩAsyncEventSubscription = ['', 'unsubscribe', 'PP$/!4"M']; exports.__ΩAsyncEventSubscription = __ΩAsyncEventSubscription; let asyncId = 0; class AsyncEmitterEvent { constructor() { this.id = asyncId++; this.stopped = false; this.propagationStopped = false; } /** * Stop propagating the event to subsequent event listeners. */ stopPropagation() { this.propagationStopped = true; } /** * Signal the emitter that you want to abort. * Subsequent event listeners will still be called. */ stop() { this.stopped = true; } } exports.AsyncEmitterEvent = AsyncEmitterEvent; AsyncEmitterEvent.__type = ['stopPropagation', 'stop', 'P"0!P"0"5']; class AsyncEventEmitter { constructor(parent) { this.parent = parent; this.subscribers = []; } subscribe(callback) { this.subscribers.push(callback); return { unsubscribe: () => { (0, array_js_1.arrayRemoveItem)(this.subscribers, callback); } }; } async emit(event) { if (this.parent) await this.parent.emit(event); if (event.propagationStopped) return; for (const subscriber of this.subscribers) { await subscriber(event); if (event.propagationStopped) return; } } hasSubscriptions() { return this.subscribers.length > 0; } } exports.AsyncEventEmitter = AsyncEventEmitter; AsyncEventEmitter.__type = ['T', () => __ΩAsyncSubscriber, 'subscribers', function () { return []; }, () => AsyncEventEmitter, 'parent', 'constructor', () => __ΩAsyncSubscriber, 'callback', () => __ΩAsyncEventSubscription, 'subscribe', 'event', 'emit', 'hasSubscriptions', 'b!e!!o""F3#<>$PP"7%2&8<"0\'Pe"!o("2)n*0+Pe"!2,$`0-P)0.5']; //# sourceMappingURL=emitter.js.map