Skip to content
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

♻️ Use registerCleanupTask for visibilityState cleanup #3063

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions packages/core/src/browser/pageExitObservable.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Configuration } from '../domain/configuration'
import { createNewEvent, restorePageVisibility, setPageVisibility, registerCleanupTask } from '../../test'
import { createNewEvent, setPageVisibility, registerCleanupTask } from '../../test'
import type { PageExitEvent } from './pageExitObservable'
import { PageExitReason, createPageExitObservable } from './pageExitObservable'

Expand All @@ -13,10 +13,6 @@ describe('createPageExitObservable', () => {
registerCleanupTask(createPageExitObservable(configuration).subscribe(onExitSpy).unsubscribe)
})

afterEach(() => {
restorePageVisibility()
})

it('notifies when the page fires beforeunload', () => {
window.dispatchEvent(createNewEvent('beforeunload'))

Expand Down
13 changes: 1 addition & 12 deletions packages/core/src/domain/session/sessionManager.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
import {
createNewEvent,
expireCookie,
mockClock,
registerCleanupTask,
restorePageVisibility,
setPageVisibility,
} from '../../../test'
import { createNewEvent, expireCookie, mockClock, registerCleanupTask, setPageVisibility } from '../../../test'
import type { Clock } from '../../../test'
import { getCookie, setCookie } from '../../browser/cookie'
import { isIE } from '../../tools/utils/browserDetection'
Expand Down Expand Up @@ -365,10 +358,6 @@ describe('startSessionManager', () => {
setPageVisibility('hidden')
})

afterEach(() => {
restorePageVisibility()
})

it('should expire the session after expiration delay', () => {
const sessionManager = startSessionManagerWithDefaults()
const expireSessionSpy = jasmine.createSpy()
Expand Down
10 changes: 6 additions & 4 deletions packages/core/test/emulate/mockVisibilityState.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { registerCleanupTask } from '../registerCleanupTask'

export function setPageVisibility(visibility: 'visible' | 'hidden') {
registerCleanupTask(() => {
delete (document as any).visibilityState
})

Object.defineProperty(document, 'visibilityState', {
get() {
return visibility
},
configurable: true,
})
}

export function restorePageVisibility() {
delete (document as any).visibilityState
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { RelativeTime } from '@datadog/browser-core'
import { registerCleanupTask, restorePageVisibility, setPageVisibility } from '@datadog/browser-core/test'
import { registerCleanupTask, setPageVisibility } from '@datadog/browser-core/test'
import type { RumPerformanceEntry } from '../../../browser/performanceObservable'
import { RumPerformanceEntryType } from '../../../browser/performanceObservable'
import { createPerformanceEntry, mockPerformanceObserver, mockRumConfiguration } from '../../../../test'
Expand All @@ -20,7 +20,6 @@ describe('trackFirstContentfulPaint', () => {
registerCleanupTask(() => {
firstHidden.stop()
firstContentfulPaint.stop()
restorePageVisibility()
})
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { RelativeTime } from '@datadog/browser-core'
import { DOM_EVENT } from '@datadog/browser-core'
import { createNewEvent, restorePageVisibility, setPageVisibility } from '@datadog/browser-core/test'
import { createNewEvent, setPageVisibility } from '@datadog/browser-core/test'
import { mockRumConfiguration } from '../../../../test'
import { trackFirstHidden } from './trackFirstHidden'

Expand All @@ -9,7 +9,6 @@ describe('trackFirstHidden', () => {
let firstHidden: { timeStamp: RelativeTime; stop: () => void }

afterEach(() => {
restorePageVisibility()
firstHidden.stop()
})

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type Duration, type RelativeTime } from '@datadog/browser-core'
import { registerCleanupTask, restorePageVisibility, setPageVisibility } from '@datadog/browser-core/test'
import { registerCleanupTask, setPageVisibility } from '@datadog/browser-core/test'
import {
appendElement,
appendText,
Expand Down Expand Up @@ -29,7 +29,6 @@ describe('firstInputTimings', () => {
registerCleanupTask(() => {
firstHidden.stop()
firstInputTimings.stop()
restorePageVisibility()
})
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import type { RelativeTime } from '@datadog/browser-core'
import { DOM_EVENT } from '@datadog/browser-core'
import {
setPageVisibility,
createNewEvent,
restorePageVisibility,
registerCleanupTask,
} from '@datadog/browser-core/test'
import { setPageVisibility, createNewEvent, registerCleanupTask } from '@datadog/browser-core/test'
import type { RumPerformanceEntry } from '../../../browser/performanceObservable'
import { RumPerformanceEntryType } from '../../../browser/performanceObservable'
import { appendElement, createPerformanceEntry, mockPerformanceObserver, mockRumConfiguration } from '../../../../test'
Expand All @@ -32,7 +27,6 @@ describe('trackLargestContentfulPaint', () => {
registerCleanupTask(() => {
firstHidden.stop()
largestContentfulPaint.stop()
restorePageVisibility()
})
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { RelativeTime, Duration } from '@datadog/browser-core'
import { clocksOrigin, Observable } from '@datadog/browser-core'
import type { Clock } from '@datadog/browser-core/test'
import { mockClock, setPageVisibility, restorePageVisibility } from '@datadog/browser-core/test'
import { mockClock, setPageVisibility } from '@datadog/browser-core/test'
import { ViewLoadingType } from '../../../rawRumEvent.types'
import { createPerformanceEntry, mockRumConfiguration } from '../../../../test'
import { PAGE_ACTIVITY_END_DELAY, PAGE_ACTIVITY_VALIDATION_DELAY } from '../../waitPageActivityEnd'
Expand Down Expand Up @@ -46,7 +46,6 @@ describe('trackLoadingTime', () => {

afterEach(() => {
stopLoadingTimeTracking()
restorePageVisibility()
clock.cleanup()
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { DeflateEncoderStreamId, PageExitReason, isIE } from '@datadog/browser-c
import type { ViewHistory, ViewHistoryEntry, RumConfiguration } from '@datadog/browser-rum-core'
import { LifeCycle, LifeCycleEventType } from '@datadog/browser-rum-core'
import type { Clock } from '@datadog/browser-core/test'
import { mockClock, registerCleanupTask, restorePageVisibility } from '@datadog/browser-core/test'
import { mockClock, registerCleanupTask } from '@datadog/browser-core/test'
import { createRumSessionManagerMock } from '../../../../rum-core/test'
import type { BrowserRecord, SegmentContext } from '../../types'
import { RecordType } from '../../types'
Expand Down Expand Up @@ -114,10 +114,6 @@ describe('startSegmentCollection', () => {
})

describe('segment flush strategy', () => {
afterEach(() => {
restorePageVisibility()
})

it('does not flush empty segments', () => {
emulatePageUnload()
worker.processAllMessages()
Expand Down