-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Integrate romanG/take-full-snapshot-asynchronously (#2887) into stagi…
…ng-30 Integrated commit sha: 479736c Co-authored-by: roman.gaignault <[email protected]>
- Loading branch information
Showing
11 changed files
with
198 additions
and
40 deletions.
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,39 @@ | ||
import { registerCleanupTask } from '../registerCleanupTask' | ||
|
||
let requestIdleCallbackSpy: jasmine.Spy | ||
let cancelIdleCallbackSpy: jasmine.Spy | ||
|
||
export function mockRequestIdleCallback() { | ||
const callbacks = new Map<number, () => void>() | ||
|
||
function addCallback(callback: (...params: any[]) => any) { | ||
const id = Math.random() | ||
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,33 @@ | ||
import { requestIdleCallback } from './requestIdleCallback' | ||
|
||
describe('requestIdleCallback', () => { | ||
let requestAnimationFrameSpy: jasmine.Spy | ||
let cancelAnimationFrameSpy: jasmine.Spy | ||
let callback: jasmine.Spy | ||
const originalRequestIdleCallback = window.requestIdleCallback | ||
const originalCancelIdleCallback = window.cancelIdleCallback | ||
|
||
beforeEach(() => { | ||
requestAnimationFrameSpy = spyOn(window, 'requestAnimationFrame').and.callFake((cb) => { | ||
cb(0) | ||
return 123 | ||
}) | ||
cancelAnimationFrameSpy = spyOn(window, 'cancelAnimationFrame') | ||
callback = jasmine.createSpy('callback') | ||
}) | ||
|
||
afterEach(() => { | ||
window.requestIdleCallback = originalRequestIdleCallback | ||
window.cancelIdleCallback = originalCancelIdleCallback | ||
}) | ||
|
||
it('should use requestAnimationFrame when requestIdleCallback is not defined', () => { | ||
window.requestIdleCallback = undefined as any | ||
window.cancelIdleCallback = undefined as any | ||
|
||
const cancel = requestIdleCallback(callback) | ||
expect(requestAnimationFrameSpy).toHaveBeenCalled() | ||
cancel() | ||
expect(cancelAnimationFrameSpy).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