Skip to content

Commit

Permalink
Cleanly stop Observers
Browse files Browse the repository at this point in the history
  • Loading branch information
amortemousque committed Feb 13, 2024
1 parent e117f1e commit d5495d8
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 12 deletions.
12 changes: 10 additions & 2 deletions packages/core/src/tools/instrumentMethod.ts
@@ -1,4 +1,5 @@
import { setTimeout } from './timer'
import type { TimeoutId } from './timer'
import { setTimeout, clearTimeout } from './timer'
import { callMonitored } from './monitor'
import { noop } from './utils/functionUtils'

Expand Down Expand Up @@ -125,11 +126,14 @@ export function instrumentSetter<TARGET extends { [key: string]: any }, PROPERTY
return { stop: noop }
}

const timeoutIds = new Set<TimeoutId>()
let instrumentation = (target: TARGET, value: TARGET[PROPERTY]) => {
// put hooked setter into event loop to avoid of set latency
setTimeout(() => {
const timeoutId = setTimeout(() => {
after(target, value)
timeoutIds.delete(timeoutId)
}, 0)
timeoutIds.add(timeoutId)
}

const instrumentationWrapper = function (this: TARGET, value: TARGET[PROPERTY]) {
Expand All @@ -148,6 +152,10 @@ export function instrumentSetter<TARGET extends { [key: string]: any }, PROPERTY
} else {
instrumentation = noop
}
if (timeoutIds) {
timeoutIds.forEach(clearTimeout)
timeoutIds.clear()
}
},
}
}
21 changes: 16 additions & 5 deletions packages/rum/src/domain/record/observers/moveObserver.ts
Expand Up @@ -15,7 +15,7 @@ export type MousemoveCallBack = (
) => void

export function initMoveObserver(configuration: RumConfiguration, cb: MousemoveCallBack): ListenerHandler {
const { throttled: updatePosition } = throttle(
const { throttled: updatePosition, cancel: cancelThrottle } = throttle(
(event: MouseEvent | TouchEvent) => {
const target = getEventTarget(event)
if (hasSerializedNode(target)) {
Expand All @@ -39,10 +39,21 @@ export function initMoveObserver(configuration: RumConfiguration, cb: MousemoveC
}
)

return addEventListeners(configuration, document, [DOM_EVENT.MOUSE_MOVE, DOM_EVENT.TOUCH_MOVE], updatePosition, {
capture: true,
passive: true,
}).stop
const { stop: removeListener } = addEventListeners(
configuration,
document,
[DOM_EVENT.MOUSE_MOVE, DOM_EVENT.TOUCH_MOVE],
updatePosition,
{
capture: true,
passive: true,
}
)

return () => {
removeListener()
cancelThrottle()
}
}

export function tryToComputeCoordinates(event: MouseEvent | TouchEvent) {
Expand Down
14 changes: 11 additions & 3 deletions packages/rum/src/domain/record/observers/scrollObserver.ts
Expand Up @@ -19,7 +19,7 @@ export function initScrollObserver(
defaultPrivacyLevel: DefaultPrivacyLevel,
elementsScrollPositions: ElementsScrollPositions
): ListenerHandler {
const { throttled: updatePosition } = throttle((event: Event) => {
const { throttled: updatePosition, cancel: cancelThrottle } = throttle((event: Event) => {
const target = getEventTarget(event) as HTMLElement | Document
if (
!target ||
Expand All @@ -46,6 +46,14 @@ export function initScrollObserver(
y: scrollPositions.scrollTop,
})
}, SCROLL_OBSERVER_THRESHOLD)
return addEventListener(configuration, document, DOM_EVENT.SCROLL, updatePosition, { capture: true, passive: true })
.stop

const { stop: removeListener } = addEventListener(configuration, document, DOM_EVENT.SCROLL, updatePosition, {
capture: true,
passive: true,
})

return () => {
removeListener()
cancelThrottle()
}
}
Expand Up @@ -35,7 +35,7 @@ export function initVisualViewportResizeObserver(
trailing: false,
}
)
const removeListener = addEventListeners(
const { stop: removeListener } = addEventListeners(
configuration,
visualViewport,
[DOM_EVENT.RESIZE, DOM_EVENT.SCROLL],
Expand All @@ -44,7 +44,7 @@ export function initVisualViewportResizeObserver(
capture: true,
passive: true,
}
).stop
)

return function stop() {
removeListener()
Expand Down

0 comments on commit d5495d8

Please sign in to comment.