| 116 | } |
| 117 | |
| 118 | export class Emitter<T> { |
| 119 | |
| 120 | private static _noop = function () { }; |
| 121 | |
| 122 | private _event: Event<T>; |
| 123 | private _callbacks: CallbackList | undefined; |
| 124 | |
| 125 | constructor(private _options?: EmitterOptions) { |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * For the public to allow to subscribe |
| 130 | * to events from this Emitter |
| 131 | */ |
| 132 | get event(): Event<T> { |
| 133 | if (!this._event) { |
| 134 | this._event = (listener: (e: T) => any, thisArgs?: any, disposables?: Disposable[]) => { |
| 135 | if (!this._callbacks) { |
| 136 | this._callbacks = new CallbackList(); |
| 137 | } |
| 138 | if (this._options && this._options.onFirstListenerAdd && this._callbacks.isEmpty()) { |
| 139 | this._options.onFirstListenerAdd(this); |
| 140 | } |
| 141 | this._callbacks.add(listener, thisArgs); |
| 142 | |
| 143 | let result: Disposable; |
| 144 | result = { |
| 145 | dispose: () => { |
| 146 | this._callbacks!.remove(listener, thisArgs); |
| 147 | result.dispose = Emitter._noop; |
| 148 | if (this._options && this._options.onLastListenerRemove && this._callbacks!.isEmpty()) { |
| 149 | this._options.onLastListenerRemove(this); |
| 150 | } |
| 151 | } |
| 152 | }; |
| 153 | if (Array.isArray(disposables)) { |
| 154 | disposables.push(result); |
| 155 | } |
| 156 | |
| 157 | return result; |
| 158 | }; |
| 159 | } |
| 160 | return this._event; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * To be kept private to fire an event to |
| 165 | * subscribers |
| 166 | */ |
| 167 | fire(event: T): any { |
| 168 | if (this._callbacks) { |
| 169 | this._callbacks.invoke.call(this._callbacks, event); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | dispose() { |
| 174 | if (this._callbacks) { |
| 175 | this._callbacks.dispose(); |
nothing calls this directly
no outgoing calls
no test coverage detected