2 /* --------------------------------------------------------------------------------------------
3 * Copyright (c) Microsoft Corporation. All rights reserved.
4 * Licensed under the MIT License. See License.txt in the project root for license information.
5 * ------------------------------------------------------------------------------------------ */
6 Object.defineProperty(exports, "__esModule", { value: true });
7 exports.Emitter = exports.Event = void 0;
8 const ral_1 = require("./ral");
11 const _disposable = { dispose() { } };
12 Event.None = function () { return _disposable; };
13 })(Event = exports.Event || (exports.Event = {}));
15 add(callback, context = null, bucket) {
16 if (!this._callbacks) {
20 this._callbacks.push(callback);
21 this._contexts.push(context);
22 if (Array.isArray(bucket)) {
23 bucket.push({ dispose: () => this.remove(callback, context) });
26 remove(callback, context = null) {
27 if (!this._callbacks) {
30 let foundCallbackWithDifferentContext = false;
31 for (let i = 0, len = this._callbacks.length; i < len; i++) {
32 if (this._callbacks[i] === callback) {
33 if (this._contexts[i] === context) {
34 // callback & context match => remove it
35 this._callbacks.splice(i, 1);
36 this._contexts.splice(i, 1);
40 foundCallbackWithDifferentContext = true;
44 if (foundCallbackWithDifferentContext) {
45 throw new Error('When adding a listener with a context, you should remove it with the same context');
49 if (!this._callbacks) {
52 const ret = [], callbacks = this._callbacks.slice(0), contexts = this._contexts.slice(0);
53 for (let i = 0, len = callbacks.length; i < len; i++) {
55 ret.push(callbacks[i].apply(contexts[i], args));
58 // eslint-disable-next-line no-console
59 ral_1.default().console.error(e);
65 return !this._callbacks || this._callbacks.length === 0;
68 this._callbacks = undefined;
69 this._contexts = undefined;
73 constructor(_options) {
74 this._options = _options;
77 * For the public to allow to subscribe
78 * to events from this Emitter
82 this._event = (listener, thisArgs, disposables) => {
83 if (!this._callbacks) {
84 this._callbacks = new CallbackList();
86 if (this._options && this._options.onFirstListenerAdd && this._callbacks.isEmpty()) {
87 this._options.onFirstListenerAdd(this);
89 this._callbacks.add(listener, thisArgs);
92 if (!this._callbacks) {
93 // disposable is disposed after emitter is disposed.
96 this._callbacks.remove(listener, thisArgs);
97 result.dispose = Emitter._noop;
98 if (this._options && this._options.onLastListenerRemove && this._callbacks.isEmpty()) {
99 this._options.onLastListenerRemove(this);
103 if (Array.isArray(disposables)) {
104 disposables.push(result);
112 * To be kept private to fire an event to
116 if (this._callbacks) {
117 this._callbacks.invoke.call(this._callbacks, event);
121 if (this._callbacks) {
122 this._callbacks.dispose();
123 this._callbacks = undefined;
127 exports.Emitter = Emitter;
128 Emitter._noop = function () { };
129 //# sourceMappingURL=events.js.map