From c3a266b3e7115ed155f24e292e46c496e53dfaab Mon Sep 17 00:00:00 2001 From: Dariusz Niemczyk <3636685+Palid@users.noreply.github.com> Date: Mon, 15 Nov 2021 15:47:21 +0100 Subject: [PATCH] Implement TypedEventEmitter for better TS support (#2018) We're using stringly typed events everywhere, this is the first step for better typescript support with our event emitters before we replace it with something much better for React support. --- src/models/base-model.ts | 51 ++++++++++++++++++++++++++++++++++++++++ src/models/thread.ts | 29 ++--------------------- 2 files changed, 53 insertions(+), 27 deletions(-) create mode 100644 src/models/base-model.ts diff --git a/src/models/base-model.ts b/src/models/base-model.ts new file mode 100644 index 000000000..e18d85e9c --- /dev/null +++ b/src/models/base-model.ts @@ -0,0 +1,51 @@ +/* +Copyright 2021 The Matrix.org Foundation C.I.C. + +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. +*/ + +import { EventEmitter } from "events"; + +/** + * Typed Event Emitter class which can act as a Base Model for all our model + * and communication events. + * This makes it much easier for us to distinguish between events, as we now need + * to properly type this, so that our events are not stringly-based and prone + * to silly typos. + */ +export abstract class BaseModel extends EventEmitter { + public on(event: Events, listener: (...args: any[]) => void): this { + super.on(event, listener); + return this; + } + + public once(event: Events, listener: (...args: any[]) => void): this { + super.once(event, listener); + return this; + } + + public off(event: Events, listener: (...args: any[]) => void): this { + super.off(event, listener); + return this; + } + + public addListener(event: Events, listener: (...args: any[]) => void): this { + super.addListener(event, listener); + return this; + } + + public removeListener(event: Events, listener: (...args: any[]) => void): this { + super.removeListener(event, listener); + return this; + } +} diff --git a/src/models/thread.ts b/src/models/thread.ts index 5fae43042..5e0c0ef47 100644 --- a/src/models/thread.ts +++ b/src/models/thread.ts @@ -14,12 +14,12 @@ See the License for the specific language governing permissions and limitations under the License. */ -import { EventEmitter } from "events"; import { MatrixClient } from "../matrix"; import { MatrixEvent } from "./event"; import { EventTimeline } from "./event-timeline"; import { EventTimelineSet } from './event-timeline-set'; import { Room } from './room'; +import { BaseModel } from "./base-model"; export enum ThreadEvent { New = "Thread.new", @@ -30,7 +30,7 @@ export enum ThreadEvent { /** * @experimental */ -export class Thread extends EventEmitter { +export class Thread extends BaseModel { /** * A reference to the event ID at the top of the thread */ @@ -183,29 +183,4 @@ export class Thread extends EventEmitter { public has(eventId: string): boolean { return this.timelineSet.findEventById(eventId) instanceof MatrixEvent; } - - public on(event: ThreadEvent, listener: (...args: any[]) => void): this { - super.on(event, listener); - return this; - } - - public once(event: ThreadEvent, listener: (...args: any[]) => void): this { - super.once(event, listener); - return this; - } - - public off(event: ThreadEvent, listener: (...args: any[]) => void): this { - super.off(event, listener); - return this; - } - - public addListener(event: ThreadEvent, listener: (...args: any[]) => void): this { - super.addListener(event, listener); - return this; - } - - public removeListener(event: ThreadEvent, listener: (...args: any[]) => void): this { - super.removeListener(event, listener); - return this; - } }