vector-im-hydrogen-web/src/utils/EventEmitter.ts

101 lines
3.1 KiB
TypeScript
Raw Normal View History

2020-08-05 18:38:55 +02:00
/*
Copyright 2020 Bruno Windels <bruno@windels.cloud>
Copyright 2021 Daniel Fedorin <danila.fedorin@gmail.com>
2020-08-05 18:38:55 +02:00
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.
*/
type Handler<T> = (value?: T) => void;
export class EventEmitter<T> {
private _handlersByName: { [event in keyof T]?: Set<Handler<T[event]>> }
2019-06-26 22:31:36 +02:00
constructor() {
this._handlersByName = {};
}
2019-02-10 21:25:29 +01:00
emit<K extends keyof T>(name: K, value?: T[K]): void {
2019-06-26 22:31:36 +02:00
const handlers = this._handlersByName[name];
if (handlers) {
handlers.forEach(h => h(value));
2019-06-26 22:31:36 +02:00
}
}
2019-02-10 21:25:29 +01:00
disposableOn<K extends keyof T>(name: K, callback: Handler<T[K]>): () => void {
this.on(name, callback);
return () => {
this.off(name, callback);
}
}
on<K extends keyof T>(name: K, callback: Handler<T[K]>): void {
2019-06-26 22:31:36 +02:00
let handlers = this._handlersByName[name];
if (!handlers) {
this.onFirstSubscriptionAdded(name);
2019-06-26 22:31:36 +02:00
this._handlersByName[name] = handlers = new Set();
}
handlers.add(callback);
}
2019-02-10 21:25:29 +01:00
off<K extends keyof T>(name: K, callback: Handler<T[K]>): void {
2019-06-26 22:31:36 +02:00
const handlers = this._handlersByName[name];
if (handlers) {
handlers.delete(callback);
if (handlers.size === 0) {
2019-06-26 22:31:36 +02:00
delete this._handlersByName[name];
this.onLastSubscriptionRemoved(name);
2019-06-26 22:31:36 +02:00
}
}
}
onFirstSubscriptionAdded<K extends keyof T>(name: K): void {}
onLastSubscriptionRemoved<K extends keyof T>(name: K): void {}
2019-02-10 21:25:29 +01:00
}
2020-04-20 21:31:27 +02:00
2019-02-10 21:25:29 +01:00
export function tests() {
2019-06-26 22:31:36 +02:00
return {
test_on_off(assert) {
let counter = 0;
const e = new EventEmitter<{ change: never }>();
2019-06-26 22:31:36 +02:00
const callback = () => counter += 1;
e.on("change", callback);
e.emit("change");
e.off("change", callback);
e.emit("change");
assert.equal(counter, 1);
},
2019-02-10 21:25:29 +01:00
2019-06-26 22:31:36 +02:00
test_emit_value(assert) {
let value = 0;
const e = new EventEmitter<{ change: number }>();
2019-06-26 22:31:36 +02:00
const callback = (v) => value = v;
e.on("change", callback);
e.emit("change", 5);
e.off("change", callback);
assert.equal(value, 5);
},
2019-02-10 21:25:29 +01:00
2019-06-26 22:31:36 +02:00
test_double_on(assert) {
let counter = 0;
const e = new EventEmitter<{ change: never }>();
2019-06-26 22:31:36 +02:00
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-10 21:25:29 +01:00
}