Skip to content

Commit

Permalink
♻️ [RUMF-1046] use instrumentMethod in locationChangeObservable
Browse files Browse the repository at this point in the history
  • Loading branch information
BenoitZugmeyer committed Oct 6, 2021
1 parent 6b6b3cd commit febcd3e
Showing 1 changed file with 28 additions and 18 deletions.
46 changes: 28 additions & 18 deletions packages/rum-core/src/browser/locationChangeObservable.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { monitor, addEventListener, DOM_EVENT, Observable } from '@datadog/browser-core'
import { addEventListener, DOM_EVENT, Observable, callMonitored, instrumentMethod } from '@datadog/browser-core'

export interface LocationChange {
oldLocation: Readonly<Location>
Expand Down Expand Up @@ -32,25 +32,35 @@ export function createLocationChangeObservable(location: Location) {
}

function trackHistory(onHistoryChange: () => void) {
// eslint-disable-next-line @typescript-eslint/unbound-method
const originalPushState = history.pushState
history.pushState = monitor(function (this: History['pushState']) {
originalPushState.apply(this, arguments as any)
onHistoryChange()
})
// eslint-disable-next-line @typescript-eslint/unbound-method
const originalReplaceState = history.replaceState
history.replaceState = monitor(function (this: History['replaceState']) {
originalReplaceState.apply(this, arguments as any)
onHistoryChange()
})
const { stop: stopInstrumentingPushState } = instrumentMethod(
history,
'pushState',
(original) =>
function () {
original.apply(this, arguments as any)
callMonitored(onHistoryChange)
}
)

const { stop: stopInstrumentingReplaceState } = instrumentMethod(
history,
'replaceState',
(original) =>
function () {
original.apply(this, arguments as any)
callMonitored(onHistoryChange)
}
)

const { stop: removeListener } = addEventListener(window, DOM_EVENT.POP_STATE, onHistoryChange)
const stop = () => {
removeListener()
history.pushState = originalPushState
history.replaceState = originalReplaceState

return {
stop: () => {
stopInstrumentingPushState()
stopInstrumentingReplaceState()
removeListener()
},
}
return { stop }
}

function trackHash(onHashChange: () => void) {
Expand Down

0 comments on commit febcd3e

Please sign in to comment.