-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Mutualize recorder and rum code to avoid reflow as much a possible
- Loading branch information
1 parent
d353050
commit 40f03ee
Showing
10 changed files
with
132 additions
and
85 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { Clock, createNewEvent } from '../../test/specHelper' | ||
import { mockClock } from '../../test/specHelper' | ||
import type { Subscription } from '../tools/observable' | ||
import type { ViewportDimension } from './viewportObservable' | ||
import { getViewportDimension, initViewportObservable } from './viewportObservable' | ||
|
||
describe('viewportObservable', () => { | ||
let viewportSubscription: Subscription | ||
let viewportDimension: ViewportDimension | ||
let clock: Clock | ||
|
||
beforeEach(() => { | ||
viewportSubscription = initViewportObservable().subscribe((dimension) => { | ||
viewportDimension = dimension | ||
}) | ||
clock = mockClock() | ||
}) | ||
|
||
afterEach(() => { | ||
viewportSubscription.unsubscribe() | ||
clock.cleanup() | ||
}) | ||
|
||
const addVerticalScrollBar = () => { | ||
document.body.style.setProperty('margin-bottom', '5000px') | ||
} | ||
const addHorizontalScrollBar = () => { | ||
document.body.style.setProperty('margin-right', '5000px') | ||
} | ||
|
||
it('should track viewport resize', () => { | ||
window.dispatchEvent(createNewEvent('resize')) | ||
clock.tick(200) | ||
|
||
expect(viewportDimension).toEqual({ width: jasmine.any(Number), height: jasmine.any(Number) }) | ||
}) | ||
|
||
describe('get layout width and height has similar native behaviour', () => { | ||
// innerWidth includes the thickness of the sidebar while `visualViewport.width` and clientWidth exclude it | ||
it('without scrollbars', () => { | ||
expect(getViewportDimension()).toEqual({ width: window.innerWidth, height: window.innerHeight }) | ||
}) | ||
|
||
it('with scrollbars', () => { | ||
addHorizontalScrollBar() | ||
addVerticalScrollBar() | ||
expect([ | ||
// Some devices don't follow specification of including scrollbars | ||
{ width: window.innerWidth, height: window.innerHeight }, | ||
{ width: document.documentElement.clientWidth, height: document.documentElement.clientHeight }, | ||
]).toContain(getViewportDimension()) | ||
}) | ||
}) | ||
}) |
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 { monitor } from '../tools/monitor' | ||
import { Observable } from '../tools/observable' | ||
import { throttle, addEventListener, DOM_EVENT } from '../tools/utils' | ||
|
||
export interface ViewportDimension { | ||
height: number | ||
width: number | ||
} | ||
|
||
let viewportObservable: Observable<ViewportDimension> | undefined | ||
|
||
export function initViewportObservable() { | ||
if (!viewportObservable) { | ||
viewportObservable = createViewportObservable() | ||
} | ||
return viewportObservable | ||
} | ||
|
||
export function createViewportObservable() { | ||
const observable = new Observable<ViewportDimension>(() => { | ||
const { throttled: updateDimension } = throttle( | ||
monitor(() => { | ||
observable.notify(getViewportDimension()) | ||
}), | ||
200 | ||
) | ||
|
||
return addEventListener(window, DOM_EVENT.RESIZE, updateDimension, { capture: true, passive: true }).stop | ||
}) | ||
|
||
return observable | ||
} | ||
|
||
// excludes the width and height of any rendered classic scrollbar that is fixed to the visual viewport | ||
export function getViewportDimension(): ViewportDimension { | ||
const visual = window.visualViewport | ||
if (visual) { | ||
return { | ||
width: Number(visual.width * visual.scale), | ||
height: Number(visual.height * visual.scale), | ||
} | ||
} | ||
|
||
return { | ||
width: Number(window.innerWidth || 0), | ||
height: Number(window.innerHeight || 0), | ||
} | ||
} |
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,12 +1,24 @@ | ||
import { isExperimentalFeatureEnabled } from '@datadog/browser-core' | ||
import { isExperimentalFeatureEnabled, initViewportObservable, getViewportDimension } from '@datadog/browser-core' | ||
|
||
let viewport: { width: number; height: number } | undefined | ||
let stopListeners: (() => void) | undefined | ||
|
||
export function getDisplayContext() { | ||
if (!isExperimentalFeatureEnabled('clickmap')) return | ||
|
||
if (!viewport) { | ||
viewport = getViewportDimension() | ||
stopListeners = initViewportObservable().subscribe((viewportDimension) => { | ||
viewport = viewportDimension | ||
}).unsubscribe | ||
} | ||
|
||
return { | ||
viewport: { | ||
width: window.innerWidth, | ||
height: window.innerHeight, | ||
}, | ||
viewport, | ||
} | ||
} | ||
|
||
export function resetDisplayContext() { | ||
if (stopListeners) stopListeners() | ||
viewport = undefined | ||
} |
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
Submodule rum-events-format
updated
5 files
+1 β8 | samples/rum/action.json | |
+0 β6 | samples/rum/extended_error.json | |
+0 β11 | samples/rum/view.json | |
+0 β88 | schemas/rum/_common-schema.json | |
+0 β33 | schemas/rum/action-schema.json |