@@ -16,10 +16,10 @@ limitations under the License.
1616
1717import { Optional } from "matrix-events-sdk" ;
1818
19- import { MatrixClient } from "../client" ;
19+ import { MatrixClient , PendingEventOrdering } from "../client" ;
2020import { TypedReEmitter } from "../ReEmitter" ;
2121import { RelationType } from "../@types/event" ;
22- import { IThreadBundledRelationship , MatrixEvent , MatrixEventEvent } from "./event" ;
22+ import { EventStatus , IThreadBundledRelationship , MatrixEvent , MatrixEventEvent } from "./event" ;
2323import { EventTimeline } from "./event-timeline" ;
2424import { EventTimelineSet , EventTimelineSetHandlerMap } from './event-timeline-set' ;
2525import { NotificationCountType , Room , RoomEvent } from './room' ;
@@ -51,6 +51,7 @@ export type EventHandlerMap = {
5151interface IThreadOpts {
5252 room : Room ;
5353 client : MatrixClient ;
54+ pendingEventOrdering ?: PendingEventOrdering ;
5455}
5556
5657export enum FeatureSupport {
@@ -88,9 +89,12 @@ export class Thread extends ReadReceipt<EmittedEvents, EventHandlerMap> {
8889
8990 private lastEvent : MatrixEvent | undefined ;
9091 private replyCount = 0 ;
92+ private lastPendingEvent : MatrixEvent | undefined ;
93+ private pendingReplyCount = 0 ;
9194
9295 public readonly room : Room ;
9396 public readonly client : MatrixClient ;
97+ private readonly pendingEventOrdering : PendingEventOrdering ;
9498
9599 public initialEventsFetched = ! Thread . hasServerSideSupport ;
96100
@@ -109,6 +113,7 @@ export class Thread extends ReadReceipt<EmittedEvents, EventHandlerMap> {
109113
110114 this . room = opts . room ;
111115 this . client = opts . client ;
116+ this . pendingEventOrdering = opts . pendingEventOrdering ?? PendingEventOrdering . Chronological ;
112117 this . timelineSet = new EventTimelineSet ( this . room , {
113118 timelineSupport : true ,
114119 pendingEvents : true ,
@@ -300,6 +305,18 @@ export class Thread extends ReadReceipt<EmittedEvents, EventHandlerMap> {
300305 bundledRelationship = this . getRootEventBundledRelationship ( ) ;
301306 }
302307
308+ let pendingEvents : MatrixEvent [ ] = [ ] ;
309+ if ( this . pendingEventOrdering === PendingEventOrdering . Detached ) {
310+ pendingEvents = this . room . getPendingEvents ( ) . filter ( ( ev ) => {
311+ const isNotSent = ev . status === EventStatus . NOT_SENT ;
312+ const belongsToTheThread = this . id === ev . threadRootId ;
313+ return isNotSent && belongsToTheThread ;
314+ } ) ;
315+ await Promise . all ( pendingEvents . map ( ev => this . processEvent ( ev ) ) ) ;
316+ }
317+ this . lastPendingEvent = pendingEvents . length ? pendingEvents [ pendingEvents . length - 1 ] : undefined ;
318+ this . pendingReplyCount = pendingEvents . length ;
319+
303320 if ( Thread . hasServerSideSupport && bundledRelationship ) {
304321 this . replyCount = bundledRelationship . count ;
305322 this . _currentUserParticipated = ! ! bundledRelationship . current_user_participated ;
@@ -393,14 +410,14 @@ export class Thread extends ReadReceipt<EmittedEvents, EventHandlerMap> {
393410 * exclude annotations from that number
394411 */
395412 public get length ( ) : number {
396- return this . replyCount ;
413+ return this . replyCount + this . pendingReplyCount ;
397414 }
398415
399416 /**
400417 * A getter for the last event added to the thread, if known.
401418 */
402419 public get replyToEvent ( ) : Optional < MatrixEvent > {
403- return this . lastEvent ?? this . lastReply ( ) ;
420+ return this . lastPendingEvent ?? this . lastEvent ?? this . lastReply ( ) ;
404421 }
405422
406423 public get events ( ) : MatrixEvent [ ] {
0 commit comments