From cce82078703ec2ffc104ad7bb9a9b82001b8cce9 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Thu, 16 Sep 2021 10:23:03 +0200 Subject: [PATCH] copy Daniel's conversion of EventEmitter to TypeScript from microui --- src/domain/ViewModel.js | 2 +- src/matrix/room/BaseRoom.js | 2 +- src/matrix/room/Invite.js | 2 +- .../{EventEmitter.js => EventEmitter.ts} | 31 ++++++++++--------- 4 files changed, 20 insertions(+), 17 deletions(-) rename src/utils/{EventEmitter.js => EventEmitter.ts} (71%) diff --git a/src/domain/ViewModel.js b/src/domain/ViewModel.js index 97a91700..f0e109f8 100644 --- a/src/domain/ViewModel.js +++ b/src/domain/ViewModel.js @@ -18,7 +18,7 @@ limitations under the License. // as in some cases it would really be more convenient to have multiple events (like telling the timeline to scroll down) // we do need to return a disposable from EventEmitter.on, or at least have a method here to easily track a subscription to an EventEmitter -import {EventEmitter} from "../utils/EventEmitter.js"; +import {EventEmitter} from "../utils/EventEmitter"; import {Disposables} from "../utils/Disposables.js"; export class ViewModel extends EventEmitter { diff --git a/src/matrix/room/BaseRoom.js b/src/matrix/room/BaseRoom.js index 4a8f50b4..aac962ac 100644 --- a/src/matrix/room/BaseRoom.js +++ b/src/matrix/room/BaseRoom.js @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -import {EventEmitter} from "../../utils/EventEmitter.js"; +import {EventEmitter} from "../../utils/EventEmitter"; import {RoomSummary} from "./RoomSummary.js"; import {GapWriter} from "./timeline/persistence/GapWriter.js"; import {RelationWriter} from "./timeline/persistence/RelationWriter.js"; diff --git a/src/matrix/room/Invite.js b/src/matrix/room/Invite.js index 9bf818ae..b8190322 100644 --- a/src/matrix/room/Invite.js +++ b/src/matrix/room/Invite.js @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -import {EventEmitter} from "../../utils/EventEmitter.js"; +import {EventEmitter} from "../../utils/EventEmitter"; import {SummaryData, processStateEvent} from "./RoomSummary.js"; import {Heroes} from "./members/Heroes.js"; import {MemberChange, RoomMember, EVENT_TYPE as MEMBER_EVENT_TYPE} from "./members/RoomMember.js"; diff --git a/src/utils/EventEmitter.js b/src/utils/EventEmitter.ts similarity index 71% rename from src/utils/EventEmitter.js rename to src/utils/EventEmitter.ts index 5dd56ac3..ea065d85 100644 --- a/src/utils/EventEmitter.js +++ b/src/utils/EventEmitter.ts @@ -1,5 +1,6 @@ /* Copyright 2020 Bruno Windels +Copyright 2021 Daniel Fedorin Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -14,28 +15,30 @@ See the License for the specific language governing permissions and limitations under the License. */ -export class EventEmitter { +type Handler = (value?: T) => void; + +export class EventEmitter { + private _handlersByName: { [event in keyof T]?: Set> } + constructor() { this._handlersByName = {}; } - emit(name, ...values) { + emit(name: K, value?: T[K]): void { const handlers = this._handlersByName[name]; if (handlers) { - for(const h of handlers) { - h(...values); - } + handlers.forEach(h => h(value)); } } - disposableOn(name, callback) { + disposableOn(name: K, callback: Handler): () => void { this.on(name, callback); return () => { this.off(name, callback); } } - on(name, callback) { + on(name: K, callback: Handler): void { let handlers = this._handlersByName[name]; if (!handlers) { this.onFirstSubscriptionAdded(name); @@ -44,27 +47,27 @@ export class EventEmitter { handlers.add(callback); } - off(name, callback) { + off(name: K, callback: Handler): void { const handlers = this._handlersByName[name]; if (handlers) { handlers.delete(callback); - if (handlers.length === 0) { + if (handlers.size === 0) { delete this._handlersByName[name]; this.onLastSubscriptionRemoved(name); } } } - onFirstSubscriptionAdded(/* name */) {} + onFirstSubscriptionAdded(name: K): void {} - onLastSubscriptionRemoved(/* name */) {} + onLastSubscriptionRemoved(name: K): void {} } export function tests() { return { test_on_off(assert) { let counter = 0; - const e = new EventEmitter(); + const e = new EventEmitter<{ change: never }>(); const callback = () => counter += 1; e.on("change", callback); e.emit("change"); @@ -75,7 +78,7 @@ export function tests() { test_emit_value(assert) { let value = 0; - const e = new EventEmitter(); + const e = new EventEmitter<{ change: number }>(); const callback = (v) => value = v; e.on("change", callback); e.emit("change", 5); @@ -85,7 +88,7 @@ export function tests() { test_double_on(assert) { let counter = 0; - const e = new EventEmitter(); + const e = new EventEmitter<{ change: never }>(); const callback = () => counter += 1; e.on("change", callback); e.on("change", callback);