1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-23 17:02:25 +03:00

Typescriptify thread events a bit

This commit is contained in:
Dariusz Niemczyk
2021-09-16 15:59:41 +02:00
parent 53bdab7cc8
commit ed68bd4108
3 changed files with 34 additions and 8 deletions

View File

@@ -33,7 +33,7 @@ import {
import { Crypto } from "../crypto";
import { deepSortedObjectEntries } from "../utils";
import { RoomMember } from "./room-member";
import { Thread } from "./thread";
import { Thread, THREAD_EVENTS } from "./thread";
import { IActionsObject } from '../pushprocessor';
import { ReEmitter } from '../ReEmitter';
@@ -1321,7 +1321,7 @@ export class MatrixEvent extends EventEmitter {
*/
public setThread(thread: Thread): void {
this.thread = thread;
this.reEmitter.reEmit(thread, ["Thread.ready", "Thread.update"]);
this.reEmitter.reEmit(thread, [THREAD_EVENTS.ready, THREAD_EVENTS.update]);
}
/**

View File

@@ -35,7 +35,7 @@ import { IRoomVersionsCapability, MatrixClient, PendingEventOrdering, RoomVersio
import { JoinRule, ResizeMethod } from "../@types/partials";
import { Filter } from "../filter";
import { RoomState } from "./room-state";
import { Thread } from "./thread";
import { Thread, THREAD_EVENTS } from "./thread";
// These constants are used as sane defaults when the homeserver doesn't support
// the m.room_versions capability. In practice, KNOWN_SAFE_ROOM_VERSION should be
@@ -1074,9 +1074,9 @@ export class Room extends EventEmitter {
public addThread(thread: Thread): Set<Thread> {
this.threads.add(thread);
if (!thread.ready) {
thread.once("Thread.ready", this.dedupeThreads);
this.emit("Thread.update", thread);
this.reEmitter.reEmit(thread, ["Thread.update", "Thread.ready"]);
thread.once(THREAD_EVENTS.ready, this.dedupeThreads);
this.emit(THREAD_EVENTS.update, thread);
this.reEmitter.reEmit(thread, [THREAD_EVENTS.update, THREAD_EVENTS.ready]);
}
return this.threads;
}

View File

@@ -20,6 +20,11 @@ import { MatrixEvent } from "./event";
import { EventTimelineSet } from './event-timeline-set';
import { Room } from './room';
export enum THREAD_EVENTS {
ready = "Thread.ready",
update = "Thread.update"
}
/**
* @experimental
*/
@@ -73,7 +78,7 @@ export class Thread extends EventEmitter {
if (this.ready) {
this.client.decryptEventIfNeeded(event, {});
}
this.emit("Thread.update", this);
this.emit(THREAD_EVENTS.ready, this);
}
/**
@@ -96,7 +101,7 @@ export class Thread extends EventEmitter {
await this.fetchReplyChain();
} else {
await this.decryptEvents();
this.emit("Thread.ready", this);
this.emit(THREAD_EVENTS.ready, this);
}
}
}
@@ -193,4 +198,25 @@ export class Thread extends EventEmitter {
public has(eventId: string): boolean {
return this.timelineSet.findEventById(eventId) instanceof MatrixEvent;
}
public on(event: THREAD_EVENTS, listener: (...args: any[]) => void): this {
super.on(event, listener);
return this;
}
public once(event: THREAD_EVENTS, listener: (...args: any[]) => void): this {
super.once(event, listener);
return this;
}
public off(event: THREAD_EVENTS, listener: (...args: any[]) => void): this {
super.off(event, listener);
return this;
}
public addListener(event: THREAD_EVENTS, listener: (...args: any[]) => void): this {
super.addListener(event, listener);
return this;
}
public removeListener(event: THREAD_EVENTS, listener: (...args: any[]) => void): this {
super.removeListener(event, listener);
return this;
}
}