UNPKG

@deepkit/core

Version:

Deepkit core library

104 lines 4.07 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AsyncEventEmitter = exports.EventEmitter = exports.EmitterEvent = exports.__ΩEventSubscription = exports.__ΩAsyncEventSubscription = void 0; /* * 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. */ /** @group Event */ const array_js_1 = require("./array.js"); const __ΩAsyncSubscriber = ['T', 'event', '', 'AsyncSubscriber', 'b!Pe"!2"P$`$J/#w$y']; const __ΩSubscriber = ['T', 'event', '', 'Subscriber', 'b!Pe"!2"P$`$J/#w$y']; const __ΩAsyncEventSubscription = ['', 'unsubscribe', 'AsyncEventSubscription', 'PP$/!4"Mw#y']; exports.__ΩAsyncEventSubscription = __ΩAsyncEventSubscription; const __ΩEventSubscription = ['', 'unsubscribe', 'EventSubscription', 'PP$/!4"Mw#y']; exports.__ΩEventSubscription = __ΩEventSubscription; let asyncId = 0; class EmitterEvent { 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.EmitterEvent = EmitterEvent; EmitterEvent.__type = ['id', function () { return asyncId++; }, 'stopped', function () { return false; }, 'propagationStopped', function () { return false; }, 'stopPropagation', 'stop', 'EmitterEvent', '!3!9>")3#>$)3%>&P"0\'P"0(5w)']; class EventEmitter { constructor(parent) { this.parent = parent; this.subscribers = []; } subscribe(callback) { this.subscribers.push(callback); return { unsubscribe: () => { (0, array_js_1.arrayRemoveItem)(this.subscribers, callback); } }; } emit(event) { if (this.parent) this.parent.emit(event); if (event.propagationStopped) return; for (const subscriber of this.subscribers) { subscriber(event); if (event.propagationStopped) return; } } hasSubscriptions() { return this.subscribers.length > 0; } } exports.EventEmitter = EventEmitter; EventEmitter.__type = ['T', () => __ΩSubscriber, 'subscribers', function () { return []; }, () => EventEmitter, 'parent', 'constructor', () => __ΩSubscriber, 'callback', () => __ΩEventSubscription, 'subscribe', 'event', 'emit', 'hasSubscriptions', 'EventEmitter', 'b!e!!o""F3#<>$PP"7%2&8<"0\'Pe"!o("2)n*0+Pe"!2,$0-P)0.5w/']; 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', 'AsyncEventEmitter', 'b!e!!o""F3#<>$PP"7%2&8<"0\'Pe"!o("2)n*0+Pe"!2,$`0-P)0.5w/']; //# sourceMappingURL=emitter.js.map