@@ -7,7 +7,7 @@ type QueuedUpdate = {
77 messageTs : string ;
88 text : string ;
99 asMarkdown : boolean ;
10- resolve : ( ) => void ;
10+ resolve : ( messageTs ?: string ) => void ;
1111} ;
1212
1313function isRateLimitError ( error : unknown ) : boolean {
@@ -23,6 +23,7 @@ export function createRateLimitedImAdapter(
2323 const queue : QueuedUpdate [ ] = [ ] ;
2424 let processing = false ;
2525 const rateLimitedMessages = new Set < string > ( ) ;
26+ const rateLimitErrors = new Map < string , string > ( ) ;
2627
2728 function key ( channelId : string , messageTs : string ) : string {
2829 return `${ channelId } :${ messageTs } ` ;
@@ -43,10 +44,13 @@ export function createRateLimitedImAdapter(
4344
4445 globalLastUpdateAt = Date . now ( ) ;
4546 try {
46- await im . updateMessage ( item . channelId , item . messageTs , item . text , item . asMarkdown ) ;
47+ const maybeUpdatedTs = await im . updateMessage ( item . channelId , item . messageTs , item . text , item . asMarkdown ) ;
48+ item . resolve ( typeof maybeUpdatedTs === "string" ? maybeUpdatedTs : undefined ) ;
4749 } catch ( error ) {
4850 if ( isRateLimitError ( error ) ) {
49- rateLimitedMessages . add ( key ( item . channelId , item . messageTs ) ) ;
51+ const rateLimitKey = key ( item . channelId , item . messageTs ) ;
52+ rateLimitedMessages . add ( rateLimitKey ) ;
53+ rateLimitErrors . set ( rateLimitKey , String ( error ) ) ;
5054 log . warn ( "IM message update hit rate limit (429)" , {
5155 channelId : item . channelId ,
5256 messageTs : item . messageTs ,
@@ -58,9 +62,8 @@ export function createRateLimitedImAdapter(
5862 messageTs : item . messageTs ,
5963 error : String ( error ) ,
6064 } ) ;
65+ item . resolve ( ) ;
6166 }
62-
63- item . resolve ( ) ;
6467 }
6568
6669 processing = false ;
@@ -74,12 +77,19 @@ export function createRateLimitedImAdapter(
7477 }
7578 return rateLimitedMessages . has ( key ( channelId , messageTs ) ) ;
7679 } ,
80+ getRateLimitError : ( channelId : string , messageTs : string ) : string | undefined => {
81+ if ( typeof im . getRateLimitError === "function" ) {
82+ const upstream = im . getRateLimitError ( channelId , messageTs ) ;
83+ if ( upstream ) return upstream ;
84+ }
85+ return rateLimitErrors . get ( key ( channelId , messageTs ) ) ;
86+ } ,
7787 updateMessage : async (
7888 channelId : string ,
7989 messageTs : string ,
8090 text : string ,
8191 asMarkdown = true
82- ) : Promise < void > => {
92+ ) : Promise < string | undefined > => {
8393 for ( let i = queue . length - 1 ; i >= 0 ; i -- ) {
8494 const queued = queue [ i ] ;
8595 if ( queued && queued . channelId === channelId && queued . messageTs === messageTs ) {
@@ -88,7 +98,7 @@ export function createRateLimitedImAdapter(
8898 }
8999 }
90100
91- return new Promise < void > ( ( resolve ) => {
101+ return new Promise < string | undefined > ( ( resolve ) => {
92102 queue . push ( { channelId, messageTs, text, asMarkdown, resolve } ) ;
93103 void processQueue ( ) ;
94104 } ) ;
0 commit comments