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

feat(rum-core): run tasks that block Angular stabilization in a separate Zone #1344

Open
wants to merge 1 commit 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
4 changes: 3 additions & 1 deletion packages/rum-core/src/common/after-frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
*
*/

import { runInOwnZone } from './utils'

const RAF_TIMEOUT = 100

/**
Expand All @@ -43,7 +45,7 @@ export default function afterFrame(callback) {
cancelAnimationFrame(raf)
setTimeout(callback)
}
const timeout = setTimeout(handler, RAF_TIMEOUT)
const timeout = runInOwnZone(() => setTimeout(handler, RAF_TIMEOUT))

const raf = requestAnimationFrame(handler)
}
3 changes: 2 additions & 1 deletion packages/rum-core/src/common/http/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/

import { HTTP_REQUEST_TIMEOUT } from '../constants'
import { runInOwnZone } from '../utils'
import { isResponseSuccessful } from './response-status'

// keepalive flag tends to limit the payload size to 64 KB
Expand Down Expand Up @@ -60,7 +61,7 @@ export function sendFetchRequest(
if (typeof AbortController === 'function') {
const controller = new AbortController()
timeoutConfig.signal = controller.signal
setTimeout(() => controller.abort(), timeout)
runInOwnZone(() => setTimeout(() => controller.abort(), timeout))
}

let fetchResponse
Expand Down
6 changes: 5 additions & 1 deletion packages/rum-core/src/common/queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
*
*/

import { runInOwnZone } from './utils'

class Queue {
constructor(onFlush, opts = {}) {
this.onFlush = onFlush
Expand All @@ -33,7 +35,9 @@ class Queue {
}

_setTimer() {
this.timeoutId = setTimeout(() => this.flush(), this.flushInterval)
this.timeoutId = runInOwnZone(() =>
setTimeout(() => this.flush(), this.flushInterval)
)
}

_clear() {
Expand Down
12 changes: 8 additions & 4 deletions packages/rum-core/src/common/throttle.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
*
*/

import { runInOwnZone } from './utils'

export default function throttle(fn, onThrottle, opts) {
var context = this
var limit = opts.limit
Expand All @@ -32,10 +34,12 @@ export default function throttle(fn, onThrottle, opts) {
return function () {
counter++
if (typeof timeoutId === 'undefined') {
timeoutId = setTimeout(function () {
counter = 0
timeoutId = undefined
}, interval)
runInOwnZone(() => {
timeoutId = setTimeout(function () {
counter = 0
timeoutId = undefined
}, interval)
})
}
if (counter > limit && typeof onThrottle === 'function') {
return onThrottle.apply(context, arguments)
Expand Down
22 changes: 21 additions & 1 deletion packages/rum-core/src/common/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,25 @@ function isBeaconInspectionEnabled() {
return false
}

/**
* Run the given function in a separate zone if ZoneJs used.
* i.e. in Angular, where this can be used to let functions like `setTimeout` and `setInterval` not block the stabilization.
* (https://angular.io/api/core/ApplicationRef#isStable)
*/
function runInOwnZone(fn) {
if (typeof window.Zone === 'undefined') {
return fn()
}

if (!window.ApmZone) {
window.ApmZone = Zone.root.fork({
name: 'ApmZone'
})
}

return window.ApmZone.run(fn)
}

export {
extend,
merge,
Expand Down Expand Up @@ -470,5 +489,6 @@ export {
isPerfTimelineSupported,
isBrowser,
isPerfTypeSupported,
isBeaconInspectionEnabled
isBeaconInspectionEnabled,
runInOwnZone
}
6 changes: 4 additions & 2 deletions packages/rum-core/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ import {
isPlatformSupported,
scheduleMicroTask,
scheduleMacroTask,
isBrowser
isBrowser,
runInOwnZone
} from './common/utils'
import { patchAll, patchEventHandler } from './common/patching'
import { observePageVisibility, observePageClicks } from './common/observers'
Expand Down Expand Up @@ -86,5 +87,6 @@ export {
CLICK,
bootstrap,
observePageVisibility,
observePageClicks
observePageClicks,
runInOwnZone
}