-
Notifications
You must be signed in to change notification settings - Fork 142
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
✨ [RUMF-1288] Collect viewport size #1584
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9a71e7c
♻️ Move context files in a dedicated folder and collocate their types
amortemousque 3adb44b
✨ Collect viewport size
amortemousque d353050
Upgrade event format
amortemousque 40f03ee
👌 Create a viewportObservable
amortemousque 8d5dae2
👌 Move viewport observable in rum-core
amortemousque be14888
Fix test
amortemousque 2e4144d
Merge branch 'main' into aymeric/collect-viewport-size
amortemousque 334a861
Merge branch 'main' into aymeric/collect-viewport-size
amortemousque 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
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,59 @@ | ||
import type { Clock } from '@datadog/browser-core/test/specHelper' | ||
import { createNewEvent, mockClock } from '@datadog/browser-core/test/specHelper' | ||
import type { Subscription } from '@datadog/browser-core/src/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', () => { | ||
afterEach(() => { | ||
document.body.style.removeProperty('margin-bottom') | ||
document.body.style.removeProperty('margin-right') | ||
}) | ||
|
||
// 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,46 @@ | ||
import { monitor, Observable, throttle, addEventListener, DOM_EVENT } from '@datadog/browser-core' | ||
|
||
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
2 changes: 1 addition & 1 deletion
2
...rum-core/src/domain/ciTestContext.spec.ts → ...src/domain/contexts/ciTestContext.spec.ts
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
File renamed without changes.
24 changes: 24 additions & 0 deletions
24
packages/rum-core/src/domain/contexts/displayContext.spec.ts
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,24 @@ | ||
import { resetExperimentalFeatures, updateExperimentalFeatures } from '@datadog/browser-core' | ||
import { getDisplayContext, resetDisplayContext } from './displayContext' | ||
|
||
describe('displayContext', () => { | ||
afterEach(() => { | ||
resetExperimentalFeatures() | ||
resetDisplayContext() | ||
}) | ||
|
||
it('should return current display context when ff enabled', () => { | ||
updateExperimentalFeatures(['clickmap']) | ||
|
||
expect(getDisplayContext()).toEqual({ | ||
viewport: { | ||
width: jasmine.any(Number), | ||
height: jasmine.any(Number), | ||
}, | ||
}) | ||
}) | ||
|
||
it('should not return current display context when ff disabled', () => { | ||
expect(getDisplayContext()).not.toBeDefined() | ||
}) | ||
}) |
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,25 @@ | ||
import { isExperimentalFeatureEnabled } from '@datadog/browser-core' | ||
import { getViewportDimension, initViewportObservable } from '../../browser/viewportObservable' | ||
|
||
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, | ||
} | ||
} | ||
|
||
export function resetDisplayContext() { | ||
if (stopListeners) stopListeners() | ||
viewport = undefined | ||
} |
4 changes: 2 additions & 2 deletions
4
...ore/src/domain/foregroundContexts.spec.ts → ...omain/contexts/foregroundContexts.spec.ts
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
2 changes: 1 addition & 1 deletion
2
...rum-core/src/domain/foregroundContexts.ts → ...src/domain/contexts/foregroundContexts.ts
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
10 changes: 5 additions & 5 deletions
10
...m-core/src/domain/internalContext.spec.ts → ...c/domain/contexts/internalContext.spec.ts
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
19 changes: 16 additions & 3 deletions
19
...es/rum-core/src/domain/internalContext.ts → ...re/src/domain/contexts/internalContext.ts
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
2 changes: 1 addition & 1 deletion
2
...core/src/domain/syntheticsContext.spec.ts → ...domain/contexts/syntheticsContext.spec.ts
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
File renamed without changes.
8 changes: 4 additions & 4 deletions
8
...s/rum-core/src/domain/urlContexts.spec.ts → ...e/src/domain/contexts/urlContexts.spec.ts
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
8 changes: 4 additions & 4 deletions
8
.../rum-core/src/domain/viewContexts.spec.ts → .../src/domain/contexts/viewContexts.spec.ts
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
14 changes: 10 additions & 4 deletions
14
packages/rum-core/src/domain/viewContexts.ts → ...-core/src/domain/contexts/viewContexts.ts
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
Oops, something went wrong.
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.
Getting the display context at the assembly stage is the more straightforward way to implement it.
However, for pre-init calls, it means we won't get the display info of the time the event happens, but at the init time. I think it's good enough though