File tree Expand file tree Collapse file tree 1 file changed +11
-3
lines changed
packages/browser-utils/src/metrics Expand file tree Collapse file tree 1 file changed +11
-3
lines changed Original file line number Diff line number Diff line change @@ -226,13 +226,21 @@ export function listenForWebVitalReportEvents(
226
226
// we only want to collect LCP if we actually navigate. Redirects should be ignored.
227
227
if ( ! options ?. isRedirect ) {
228
228
_runCollectorCallbackOnce ( 'navigation' ) ;
229
- unsubscribeStartNavigation ?.( ) ;
230
- unsubscribeAfterStartPageLoadSpan ?.( ) ;
229
+ safeUnsubscribe ( unsubscribeStartNavigation , unsubscribeAfterStartPageLoadSpan ) ;
231
230
}
232
231
} ) ;
233
232
234
233
const unsubscribeAfterStartPageLoadSpan = client . on ( 'afterStartPageLoadSpan' , span => {
235
234
pageloadSpanId = span . spanContext ( ) . spanId ;
236
- unsubscribeAfterStartPageLoadSpan ?. ( ) ;
235
+ safeUnsubscribe ( unsubscribeAfterStartPageLoadSpan ) ;
237
236
} ) ;
238
237
}
238
+
239
+ /**
240
+ * Invoke a list of unsubscribers in a safe way, by deferring the invocation to the next tick.
241
+ * This is necessary because unsubscribing in sync can lead to other callbacks no longer being invoked
242
+ * due to in-place array mutation of the subscribers array on the client.
243
+ */
244
+ function safeUnsubscribe ( ...unsubscribers : ( ( ) => void | undefined ) [ ] ) : void {
245
+ unsubscribers . forEach ( u => u && setTimeout ( u , 0 ) ) ;
246
+ }
You can’t perform that action at this time.
0 commit comments