mirror of
https://github.com/vector-im/hydrogen-web.git
synced 2024-12-24 12:04:57 +01:00
69 lines
1.4 KiB
JavaScript
69 lines
1.4 KiB
JavaScript
|
export default class EventEmitter {
|
||
|
constructor() {
|
||
|
this._handlersByName = {};
|
||
|
}
|
||
|
|
||
|
emit(name, value) {
|
||
|
const handlers = this._handlersByName[name];
|
||
|
if (handlers) {
|
||
|
for(const h of handlers) {
|
||
|
h(value);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
on(name, callback) {
|
||
|
let handlers = this._handlersByName[name];
|
||
|
if (!handlers) {
|
||
|
this._handlersByName[name] = handlers = new Set();
|
||
|
}
|
||
|
handlers.add(callback);
|
||
|
}
|
||
|
|
||
|
off(name, callback) {
|
||
|
const handlers = this._handlersByName[name];
|
||
|
if (handlers) {
|
||
|
handlers.delete(callback);
|
||
|
if (handlers.length === 0) {
|
||
|
delete this._handlersByName[name];
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
//#ifdef TESTS
|
||
|
export function tests() {
|
||
|
return {
|
||
|
test_on_off(assert) {
|
||
|
let counter = 0;
|
||
|
const e = new EventEmitter();
|
||
|
const callback = () => counter += 1;
|
||
|
e.on("change", callback);
|
||
|
e.emit("change");
|
||
|
e.off("change", callback);
|
||
|
e.emit("change");
|
||
|
assert.equal(counter, 1);
|
||
|
},
|
||
|
|
||
|
test_emit_value(assert) {
|
||
|
let value = 0;
|
||
|
const e = new EventEmitter();
|
||
|
const callback = (v) => value = v;
|
||
|
e.on("change", callback);
|
||
|
e.emit("change", 5);
|
||
|
e.off("change", callback);
|
||
|
assert.equal(value, 5);
|
||
|
},
|
||
|
|
||
|
test_double_on(assert) {
|
||
|
let counter = 0;
|
||
|
const e = new EventEmitter();
|
||
|
const callback = () => counter += 1;
|
||
|
e.on("change", callback);
|
||
|
e.on("change", callback);
|
||
|
e.emit("change");
|
||
|
e.off("change", callback);
|
||
|
assert.equal(counter, 1);
|
||
|
}
|
||
|
};
|
||
|
}
|
||
|
//#endif
|