-
Notifications
You must be signed in to change notification settings - Fork 136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
⚡ [RUM-116] On view change, take the full snapshot asynchronously #2887
Draft
RomanGaignault
wants to merge
4
commits into
main
Choose a base branch
from
romanG/take-full-snapshot-asynchronously
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+219
−41
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
eebddff
On view change, take the full snapshot asynchronously
RomanGaignault 479736c
startfullsnapshot unit test
RomanGaignault 0e188c0
set a counter for id mockrequestIdleCallBack add CancelIdleCallback t…
RomanGaignault fd4b76c
update requestIdleCallBack unit test
RomanGaignault File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { registerCleanupTask } from '../registerCleanupTask' | ||
|
||
let requestIdleCallbackSpy: jasmine.Spy | ||
let cancelIdleCallbackSpy: jasmine.Spy | ||
|
||
export function mockRequestIdleCallback() { | ||
const callbacks = new Map<number, () => void>() | ||
|
||
let idCounter = 0 | ||
|
||
function addCallback(callback: (...params: any[]) => any) { | ||
const id = ++idCounter | ||
callbacks.set(id, callback) | ||
return id | ||
} | ||
|
||
function removeCallback(id: number) { | ||
callbacks.delete(id) | ||
} | ||
|
||
if (!window.requestIdleCallback || !window.cancelIdleCallback) { | ||
requestIdleCallbackSpy = spyOn(window, 'requestAnimationFrame').and.callFake(addCallback) | ||
cancelIdleCallbackSpy = spyOn(window, 'cancelAnimationFrame').and.callFake(removeCallback) | ||
} else { | ||
requestIdleCallbackSpy = spyOn(window, 'requestIdleCallback').and.callFake(addCallback) | ||
cancelIdleCallbackSpy = spyOn(window, 'cancelIdleCallback').and.callFake(removeCallback) | ||
} | ||
|
||
registerCleanupTask(() => { | ||
requestIdleCallbackSpy.calls.reset() | ||
cancelIdleCallbackSpy.calls.reset() | ||
callbacks.clear() | ||
}) | ||
|
||
return { | ||
triggerIdleCallbacks: () => { | ||
callbacks.forEach((callback) => callback()) | ||
}, | ||
cancelIdleCallbackSpy, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { requestIdleCallback } from './requestIdleCallback' | ||
|
||
describe('requestIdleCallback', () => { | ||
let callback: jasmine.Spy | ||
const originalRequestIdleCallback = window.requestIdleCallback | ||
|
||
beforeEach(() => { | ||
callback = jasmine.createSpy('callback') | ||
}) | ||
|
||
afterEach(() => { | ||
if (originalRequestIdleCallback) { | ||
window.requestIdleCallback = originalRequestIdleCallback | ||
} | ||
}) | ||
|
||
it('should use requestIdleCallback when supported', () => { | ||
if (!window.requestIdleCallback) { | ||
pending('requestIdleCallback not supported') | ||
} | ||
spyOn(window, 'requestIdleCallback').and.callFake((cb) => { | ||
cb({} as IdleDeadline) | ||
return 123 | ||
}) | ||
spyOn(window, 'cancelIdleCallback') | ||
|
||
const cancel = requestIdleCallback(callback) | ||
expect(window.requestIdleCallback).toHaveBeenCalled() | ||
cancel() | ||
expect(window.cancelIdleCallback).toHaveBeenCalledWith(123) | ||
}) | ||
|
||
it('should use requestAnimationFrame when requestIdleCallback is not supported', () => { | ||
if (window.requestIdleCallback) { | ||
window.requestIdleCallback = undefined as any | ||
} | ||
spyOn(window, 'requestAnimationFrame').and.callFake((cb) => { | ||
cb(1) | ||
return 123 | ||
}) | ||
spyOn(window, 'cancelAnimationFrame') | ||
|
||
const cancel = requestIdleCallback(callback) | ||
expect(window.requestAnimationFrame).toHaveBeenCalled() | ||
cancel() | ||
expect(window.cancelAnimationFrame).toHaveBeenCalledWith(123) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { monitor } from '@datadog/browser-core' | ||
|
||
/** | ||
* Use 'requestIdleCallback' when available: it will throttle the mutation processing if the | ||
* browser is busy rendering frames (ex: when frames are below 60fps). When not available, the | ||
* fallback on 'requestAnimationFrame' will still ensure the mutations are processed after any | ||
* browser rendering process (Layout, Recalculate Style, etc.), so we can serialize DOM nodes efficiently. | ||
* | ||
* Note: check both 'requestIdleCallback' and 'cancelIdleCallback' existence because some polyfills only implement 'requestIdleCallback'. | ||
*/ | ||
|
||
export function requestIdleCallback(callback: () => void, opts?: { timeout?: number }) { | ||
if (window.requestIdleCallback && window.cancelIdleCallback) { | ||
const id = window.requestIdleCallback(monitor(callback), opts) | ||
return () => window.cancelIdleCallback(id) | ||
} | ||
const id = window.requestAnimationFrame(monitor(callback)) | ||
return () => window.cancelAnimationFrame(id) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import { LifeCycleEventType, getScrollX, getScrollY, getViewportDimension } from '@datadog/browser-rum-core' | ||
import type { RumConfiguration, LifeCycle } from '@datadog/browser-rum-core' | ||
import { timeStampNow } from '@datadog/browser-core' | ||
import { timeStampNow, isExperimentalFeatureEnabled, ExperimentalFeature } from '@datadog/browser-core' | ||
import { requestIdleCallback } from '../../browser/requestIdleCallback' | ||
import type { BrowserRecord } from '../../types' | ||
import { RecordType } from '../../types' | ||
import type { ElementsScrollPositions } from './elementsScrollPositions' | ||
|
@@ -14,7 +15,8 @@ export function startFullSnapshots( | |
lifeCycle: LifeCycle, | ||
configuration: RumConfiguration, | ||
flushMutations: () => void, | ||
fullSnapshotCallback: (records: BrowserRecord[]) => void | ||
fullSnapshotPendingCallback: () => void, | ||
fullSnapshotReadyCallback: (records: BrowserRecord[]) => void | ||
) { | ||
const takeFullSnapshot = ( | ||
timestamp = timeStampNow(), | ||
|
@@ -65,20 +67,37 @@ export function startFullSnapshots( | |
return records | ||
} | ||
|
||
fullSnapshotCallback(takeFullSnapshot()) | ||
fullSnapshotReadyCallback(takeFullSnapshot()) | ||
|
||
let cancelIdleCallback: (() => void) | undefined | ||
const { unsubscribe } = lifeCycle.subscribe(LifeCycleEventType.VIEW_CREATED, (view) => { | ||
flushMutations() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❓ question: Is this |
||
fullSnapshotCallback( | ||
takeFullSnapshot(view.startClocks.timeStamp, { | ||
shadowRootsController, | ||
status: SerializationContextStatus.SUBSEQUENT_FULL_SNAPSHOT, | ||
elementsScrollPositions, | ||
}) | ||
) | ||
function takeSubsequentFullSnapshot() { | ||
flushMutations() | ||
fullSnapshotReadyCallback( | ||
takeFullSnapshot(view.startClocks.timeStamp, { | ||
shadowRootsController, | ||
status: SerializationContextStatus.SUBSEQUENT_FULL_SNAPSHOT, | ||
elementsScrollPositions, | ||
}) | ||
) | ||
} | ||
|
||
if (isExperimentalFeatureEnabled(ExperimentalFeature.ASYNC_FULL_SNAPSHOT)) { | ||
if (cancelIdleCallback) { | ||
cancelIdleCallback() | ||
} | ||
fullSnapshotPendingCallback() | ||
cancelIdleCallback = requestIdleCallback(takeSubsequentFullSnapshot) | ||
} else { | ||
takeSubsequentFullSnapshot() | ||
} | ||
}) | ||
|
||
return { | ||
RomanGaignault marked this conversation as resolved.
Show resolved
Hide resolved
|
||
stop: unsubscribe, | ||
stop: () => { | ||
unsubscribe() | ||
cancelIdleCallback?.() | ||
}, | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❓ question: By discarding records during the fullsnapshot are we confident not to miss important ones like scroll, mouseInteraction, move?