@@ -27,6 +27,7 @@ interface BuildLogsState {
2727
2828 _trpcClient : TRPCClient | null
2929 _params : BuildLogsParams | null
30+ _initVersion : number
3031}
3132
3233interface BuildLogsMutations {
@@ -71,6 +72,7 @@ const initialState: BuildLogsState = {
7172 isInitialized : false ,
7273 _trpcClient : null ,
7374 _params : null ,
75+ _initVersion : 0 ,
7476}
7577
7678export const createBuildLogsStore = ( ) =>
@@ -103,11 +105,15 @@ export const createBuildLogsStore = () =>
103105 get ( ) . reset ( )
104106 }
105107
108+ // Increment version to invalidate any in-flight requests
109+ const requestVersion = state . _initVersion + 1
110+
106111 set ( ( s ) => {
107112 s . _trpcClient = trpcClient
108113 s . _params = params
109114 s . level = level
110115 s . isLoadingBackwards = true
116+ s . _initVersion = requestVersion
111117 } )
112118
113119 try {
@@ -119,18 +125,27 @@ export const createBuildLogsStore = () =>
119125 cursor : Date . now ( ) ,
120126 } )
121127
128+ // Ignore stale response if a newer init was called
129+ if ( get ( ) . _initVersion !== requestVersion ) {
130+ return
131+ }
132+
122133 set ( ( s ) => {
123134 s . logs = result . logs
124135 s . hasMoreBackwards = result . nextCursor !== null
125136 s . backwardsCursor = result . nextCursor
126137 s . isLoadingBackwards = false
127138 s . isInitialized = true
128139 } )
129- } catch ( error ) {
140+ } catch {
141+ // Ignore errors from stale requests
142+ if ( get ( ) . _initVersion !== requestVersion ) {
143+ return
144+ }
145+
130146 set ( ( s ) => {
131147 s . isLoadingBackwards = false
132148 } )
133- throw error
134149 }
135150 } ,
136151
@@ -146,6 +161,8 @@ export const createBuildLogsStore = () =>
146161 return
147162 }
148163
164+ const requestVersion = state . _initVersion
165+
149166 set ( ( s ) => {
150167 s . isLoadingBackwards = true
151168 } )
@@ -164,28 +181,42 @@ export const createBuildLogsStore = () =>
164181 }
165182 )
166183
184+ // Ignore stale response if init was called during fetch
185+ if ( get ( ) . _initVersion !== requestVersion ) {
186+ return
187+ }
188+
167189 set ( ( s ) => {
168190 const uniqueNewLogs = deduplicateLogs ( s . logs , result . logs )
169191 s . logs = [ ...s . logs , ...uniqueNewLogs ]
170192 s . hasMoreBackwards = result . nextCursor !== null
171193 s . backwardsCursor = result . nextCursor
172194 s . isLoadingBackwards = false
173195 } )
174- } catch ( error ) {
196+ } catch {
197+ if ( get ( ) . _initVersion !== requestVersion ) {
198+ return
199+ }
200+
175201 set ( ( s ) => {
176202 s . isLoadingBackwards = false
177203 } )
178- throw error
179204 }
180205 } ,
181206
182207 fetchMoreForwards : async ( ) => {
183208 const state = get ( )
184209
185- if ( ! state . _trpcClient || ! state . _params ) {
210+ if (
211+ ! state . _trpcClient ||
212+ ! state . _params ||
213+ state . isLoadingForwards
214+ ) {
186215 return { logsCount : 0 }
187216 }
188217
218+ const requestVersion = state . _initVersion
219+
189220 set ( ( s ) => {
190221 s . isLoadingForwards = true
191222 } )
@@ -204,6 +235,11 @@ export const createBuildLogsStore = () =>
204235 cursor,
205236 } )
206237
238+ // Ignore stale response if init was called during fetch
239+ if ( get ( ) . _initVersion !== requestVersion ) {
240+ return { logsCount : 0 }
241+ }
242+
207243 set ( ( s ) => {
208244 const uniqueNewLogs = deduplicateLogs ( s . logs , result . logs )
209245 if ( uniqueNewLogs . length > 0 ) {
@@ -213,11 +249,16 @@ export const createBuildLogsStore = () =>
213249 } )
214250
215251 return { logsCount : result . logs . length }
216- } catch ( error ) {
252+ } catch {
253+ if ( get ( ) . _initVersion !== requestVersion ) {
254+ return { logsCount : 0 }
255+ }
256+
217257 set ( ( s ) => {
218258 s . isLoadingForwards = false
219259 } )
220- throw error
260+
261+ return { logsCount : 0 }
221262 }
222263 } ,
223264
0 commit comments