Skip to content

Commit 999ab87

Browse files
✨ delay new view update
1 parent d115c91 commit 999ab87

3 files changed

Lines changed: 20 additions & 1 deletion

File tree

packages/core/src/tools/utils/functionUtils.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { monitorError } from '../monitor'
12
import type { TimeoutId } from '../timer'
23
import { setTimeout, clearTimeout } from '../timer'
34

@@ -43,3 +44,7 @@ export function throttle<T extends (...args: any[]) => void>(
4344

4445
// eslint-disable-next-line @typescript-eslint/no-empty-function
4546
export function noop() {}
47+
48+
export function enqueueMicroTask(callback: () => void) {
49+
Promise.resolve().then(callback).catch(monitorError)
50+
}

packages/rum-core/src/domain/view/trackViews.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,15 @@ describe('view lifecycle', () => {
107107
changeLocation = viewTest.changeLocation
108108
})
109109

110+
it('notifies a View update asynchronously', () => {
111+
const { getViewUpdateCount } = viewTest
112+
113+
expect(getViewUpdateCount()).toBe(0)
114+
clock.tick(0)
115+
116+
expect(getViewUpdateCount()).toBe(1)
117+
})
118+
110119
describe('expire session', () => {
111120
it('should end the view when the session expires', () => {
112121
const { lifeCycle, getViewEndCount } = viewTest

packages/rum-core/src/domain/view/trackViews.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
setTimeout,
2727
Observable,
2828
createContextManager,
29+
enqueueMicroTask,
2930
} from '@datadog/browser-core'
3031
import type { ViewCustomTimings } from '../../rawRumEvent.types'
3132
import { ViewLoadingType } from '../../rawRumEvent.types'
@@ -287,7 +288,11 @@ function newView(
287288
})
288289

289290
// Initial view update
290-
triggerViewUpdate()
291+
enqueueMicroTask(() => {
292+
if (documentVersion === 0) {
293+
triggerViewUpdate()
294+
}
295+
})
291296

292297
// View context update should always be throttled
293298
contextManager.changeObservable.subscribe(scheduleViewUpdate)

0 commit comments

Comments
 (0)