2020-08-05 22:08:55 +05:30
|
|
|
/*
|
|
|
|
Copyright 2020 Bruno Windels <bruno@windels.cloud>
|
2021-09-16 13:53:03 +05:30
|
|
|
Copyright 2021 Daniel Fedorin <danila.fedorin@gmail.com>
|
2020-08-05 22:08:55 +05:30
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2021-09-16 13:53:03 +05:30
|
|
|
type Handler<T> = (value?: T) => void;
|
|
|
|
|
|
|
|
export class EventEmitter<T> {
|
|
|
|
private _handlersByName: { [event in keyof T]?: Set<Handler<T[event]>> }
|
|
|
|
|
2019-06-27 02:01:36 +05:30
|
|
|
constructor() {
|
|
|
|
this._handlersByName = {};
|
|
|
|
}
|
2019-02-11 01:55:29 +05:30
|
|
|
|
2021-09-16 13:53:03 +05:30
|
|
|
emit<K extends keyof T>(name: K, value?: T[K]): void {
|
2019-06-27 02:01:36 +05:30
|
|
|
const handlers = this._handlersByName[name];
|
|
|
|
if (handlers) {
|
2021-09-16 13:53:03 +05:30
|
|
|
handlers.forEach(h => h(value));
|
2019-06-27 02:01:36 +05:30
|
|
|
}
|
|
|
|
}
|
2019-02-11 01:55:29 +05:30
|
|
|
|
2021-09-16 13:53:03 +05:30
|
|
|
disposableOn<K extends keyof T>(name: K, callback: Handler<T[K]>): () => void {
|
2020-05-07 03:14:52 +05:30
|
|
|
this.on(name, callback);
|
|
|
|
return () => {
|
|
|
|
this.off(name, callback);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-16 13:53:03 +05:30
|
|
|
on<K extends keyof T>(name: K, callback: Handler<T[K]>): void {
|
2019-06-27 02:01:36 +05:30
|
|
|
let handlers = this._handlersByName[name];
|
|
|
|
if (!handlers) {
|
2019-06-16 14:24:16 +05:30
|
|
|
this.onFirstSubscriptionAdded(name);
|
2019-06-27 02:01:36 +05:30
|
|
|
this._handlersByName[name] = handlers = new Set();
|
|
|
|
}
|
|
|
|
handlers.add(callback);
|
|
|
|
}
|
2019-02-11 01:55:29 +05:30
|
|
|
|
2021-09-16 13:53:03 +05:30
|
|
|
off<K extends keyof T>(name: K, callback: Handler<T[K]>): void {
|
2019-06-27 02:01:36 +05:30
|
|
|
const handlers = this._handlersByName[name];
|
|
|
|
if (handlers) {
|
|
|
|
handlers.delete(callback);
|
2021-09-16 13:53:03 +05:30
|
|
|
if (handlers.size === 0) {
|
2019-06-27 02:01:36 +05:30
|
|
|
delete this._handlersByName[name];
|
2019-06-16 14:24:16 +05:30
|
|
|
this.onLastSubscriptionRemoved(name);
|
2019-06-27 02:01:36 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-16 14:24:16 +05:30
|
|
|
|
2021-09-16 13:53:03 +05:30
|
|
|
onFirstSubscriptionAdded<K extends keyof T>(name: K): void {}
|
2019-06-16 14:24:16 +05:30
|
|
|
|
2021-09-16 13:53:03 +05:30
|
|
|
onLastSubscriptionRemoved<K extends keyof T>(name: K): void {}
|
2019-02-11 01:55:29 +05:30
|
|
|
}
|
2020-04-21 01:01:27 +05:30
|
|
|
|
2019-02-11 01:55:29 +05:30
|
|
|
export function tests() {
|
2019-06-27 02:01:36 +05:30
|
|
|
return {
|
|
|
|
test_on_off(assert) {
|
|
|
|
let counter = 0;
|
2021-09-16 13:53:03 +05:30
|
|
|
const e = new EventEmitter<{ change: never }>();
|
2019-06-27 02:01:36 +05:30
|
|
|
const callback = () => counter += 1;
|
|
|
|
e.on("change", callback);
|
|
|
|
e.emit("change");
|
|
|
|
e.off("change", callback);
|
|
|
|
e.emit("change");
|
|
|
|
assert.equal(counter, 1);
|
|
|
|
},
|
2019-02-11 01:55:29 +05:30
|
|
|
|
2019-06-27 02:01:36 +05:30
|
|
|
test_emit_value(assert) {
|
|
|
|
let value = 0;
|
2021-09-16 13:53:03 +05:30
|
|
|
const e = new EventEmitter<{ change: number }>();
|
2019-06-27 02:01:36 +05:30
|
|
|
const callback = (v) => value = v;
|
|
|
|
e.on("change", callback);
|
|
|
|
e.emit("change", 5);
|
|
|
|
e.off("change", callback);
|
|
|
|
assert.equal(value, 5);
|
|
|
|
},
|
2019-02-11 01:55:29 +05:30
|
|
|
|
2019-06-27 02:01:36 +05:30
|
|
|
test_double_on(assert) {
|
|
|
|
let counter = 0;
|
2021-09-16 13:53:03 +05:30
|
|
|
const e = new EventEmitter<{ change: never }>();
|
2019-06-27 02:01:36 +05:30
|
|
|
const callback = () => counter += 1;
|
|
|
|
e.on("change", callback);
|
|
|
|
e.on("change", callback);
|
|
|
|
e.emit("change");
|
|
|
|
e.off("change", callback);
|
|
|
|
assert.equal(counter, 1);
|
|
|
|
}
|
|
|
|
};
|
2019-02-11 01:55:29 +05:30
|
|
|
}
|