@@ -56,11 +56,17 @@ type MatrixClientWithWritableFetchRoomEvent = MatrixClient & {
5656 fetchRoomEvent : ( roomId : string , eventId : string ) => Promise < FetchRoomEventResult > ;
5757} ;
5858
59- // Replace fetchRoomEvent for first sync so thread roots don't each trigger GET /event
60- // Uses cached timeline events when present otherwise a stub that fetches when user opens thread
61- function installStartupFetchRoomEventPatch ( mx : MatrixClient ) : void {
59+ type StartupFetchRoomEventPatchOptions = {
60+ stubOnCacheMiss : boolean ;
61+ } ;
62+
63+ function installStartupFetchRoomEventPatch (
64+ mx : MatrixClient ,
65+ options : StartupFetchRoomEventPatchOptions
66+ ) : void {
6267 fetchRoomEventStartupCleanupByClient . get ( mx ) ?.( ) ;
6368
69+ const { stubOnCacheMiss } = options ;
6470 const mxWritable = mx as MatrixClientWithWritableFetchRoomEvent ;
6571 const origFetchRoomEvent = mx . fetchRoomEvent . bind ( mx ) ;
6672 let restored = false ;
@@ -84,12 +90,17 @@ function installStartupFetchRoomEventPatch(mx: MatrixClient): void {
8490 mxWritable . fetchRoomEvent = ( roomId : string , eventId : string ) => {
8591 if ( restored ) return origFetchRoomEvent ( roomId , eventId ) ;
8692 const cachedEvent = mx . getRoom ( roomId ) ?. findEventById ( eventId ) ;
87- // Reuse sync payload instead of another GET when we already have the root.
88- const payload : FetchRoomEventResult = cachedEvent ?. event ?? {
89- event_id : eventId ,
90- room_id : roomId ,
91- } ;
92- return Promise . resolve ( payload ) ;
93+ if ( cachedEvent ) {
94+ return Promise . resolve ( cachedEvent . event ) ;
95+ }
96+ if ( stubOnCacheMiss ) {
97+ const payload : FetchRoomEventResult = {
98+ event_id : eventId ,
99+ room_id : roomId ,
100+ } ;
101+ return Promise . resolve ( payload ) ;
102+ }
103+ return origFetchRoomEvent ( roomId , eventId ) ;
93104 } ;
94105
95106 mx . on ( ClientEvent . Sync , onSync ) ;
@@ -518,7 +529,7 @@ export const startClient = async (mx: MatrixClient, config?: StartClientConfig):
518529 ( filterDefinition . room . timeline as { lazy_load_members ?: boolean } ) . lazy_load_members = true ;
519530 }
520531
521- installStartupFetchRoomEventPatch ( mx ) ;
532+ installStartupFetchRoomEventPatch ( mx , { stubOnCacheMiss : true } ) ;
522533
523534 let syncStarted : Promise < void > ;
524535 try {
@@ -693,7 +704,7 @@ export const startClient = async (mx: MatrixClient, config?: StartClientConfig):
693704 } ) ;
694705
695706 try {
696- installStartupFetchRoomEventPatch ( mx ) ;
707+ installStartupFetchRoomEventPatch ( mx , { stubOnCacheMiss : false } ) ;
697708 await mx . startClient ( {
698709 lazyLoadMembers : true ,
699710 slidingSync : manager . slidingSync ,
0 commit comments