1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-25 05:23:13 +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 { Crypto } from "../crypto";
import { deepSortedObjectEntries } from "../utils"; import { deepSortedObjectEntries } from "../utils";
import { RoomMember } from "./room-member"; import { RoomMember } from "./room-member";
import { Thread } from "./thread"; import { Thread, THREAD_EVENTS } from "./thread";
import { IActionsObject } from '../pushprocessor'; import { IActionsObject } from '../pushprocessor';
import { ReEmitter } from '../ReEmitter'; import { ReEmitter } from '../ReEmitter';
@@ -1321,7 +1321,7 @@ export class MatrixEvent extends EventEmitter {
*/ */
public setThread(thread: Thread): void { public setThread(thread: Thread): void {
this.thread = thread; 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 { JoinRule, ResizeMethod } from "../@types/partials";
import { Filter } from "../filter"; import { Filter } from "../filter";
import { RoomState } from "./room-state"; 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 // 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 // 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> { public addThread(thread: Thread): Set<Thread> {
this.threads.add(thread); this.threads.add(thread);
if (!thread.ready) { if (!thread.ready) {
thread.once("Thread.ready", this.dedupeThreads); thread.once(THREAD_EVENTS.ready, this.dedupeThreads);
this.emit("Thread.update", thread); this.emit(THREAD_EVENTS.update, thread);
this.reEmitter.reEmit(thread, ["Thread.update", "Thread.ready"]); this.reEmitter.reEmit(thread, [THREAD_EVENTS.update, THREAD_EVENTS.ready]);
} }
return this.threads; return this.threads;
} }

View File

@@ -20,6 +20,11 @@ import { MatrixEvent } from "./event";
import { EventTimelineSet } from './event-timeline-set'; import { EventTimelineSet } from './event-timeline-set';
import { Room } from './room'; import { Room } from './room';
export enum THREAD_EVENTS {
ready = "Thread.ready",
update = "Thread.update"
}
/** /**
* @experimental * @experimental
*/ */
@@ -73,7 +78,7 @@ export class Thread extends EventEmitter {
if (this.ready) { if (this.ready) {
this.client.decryptEventIfNeeded(event, {}); 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(); await this.fetchReplyChain();
} else { } else {
await this.decryptEvents(); 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 { public has(eventId: string): boolean {
return this.timelineSet.findEventById(eventId) instanceof MatrixEvent; 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;
}
} }