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

🐛 [RUM] fix view update after its end #373

Merged
merged 5 commits into from
Apr 24, 2020
Merged
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
2 changes: 1 addition & 1 deletion packages/core/src/sessionManagement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export function startSessionManagement<Type extends string>(
const renewObservable = new Observable<void>()
let currentSessionId = retrieveActiveSession(sessionCookie).id

const expandOrRenewSession = utils.throttle(() => {
const { throttled: expandOrRenewSession } = utils.throttle(() => {
const session = retrieveActiveSession(sessionCookie)
const { type, isTracked } = computeSessionState(session[sessionTypeKey])
session[sessionTypeKey] = type
Expand Down
45 changes: 28 additions & 17 deletions packages/core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,30 +34,41 @@ export function throttle(
fn: () => void,
wait: number,
options?: { leading?: boolean; trailing?: boolean }
): () => void {
): { throttled: () => void; stop: () => void } {
const needLeadingExecution = options && options.leading !== undefined ? options.leading : true
const needTrailingExecution = options && options.trailing !== undefined ? options.trailing : true
let inWaitPeriod = false
let hasPendingExecution = false
let pendingTimeoutId: number
let isStopped = false

return function(this: any) {
if (inWaitPeriod) {
hasPendingExecution = true
return
}
if (needLeadingExecution) {
fn.apply(this)
} else {
hasPendingExecution = true
}
inWaitPeriod = true
setTimeout(() => {
if (needTrailingExecution && hasPendingExecution) {
return {
throttled(this: any) {
if (isStopped) {
return
}
if (inWaitPeriod) {
hasPendingExecution = true
return
}
if (needLeadingExecution) {
fn.apply(this)
} else {
hasPendingExecution = true
}
inWaitPeriod = false
hasPendingExecution = false
}, wait)
inWaitPeriod = true
pendingTimeoutId = window.setTimeout(() => {
if (needTrailingExecution && hasPendingExecution) {
fn.apply(this)
}
inWaitPeriod = false
hasPendingExecution = false
}, wait)
},
stop() {
window.clearTimeout(pendingTimeoutId)
isStopped = true
},
}
}

Expand Down
35 changes: 31 additions & 4 deletions packages/core/test/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ describe('utils', () => {
describe('throttle', () => {
let spy: jasmine.Spy
let throttled: () => void
let stop: () => void

beforeEach(() => {
jasmine.clock().install()
Expand All @@ -17,7 +18,7 @@ describe('utils', () => {

describe('when {leading: false, trailing:false}', () => {
beforeEach(() => {
throttled = throttle(spy, 2, { leading: false, trailing: false })
throttled = throttle(spy, 2, { leading: false, trailing: false }).throttled
})

it('should not call throttled function', () => {
Expand Down Expand Up @@ -61,7 +62,7 @@ describe('utils', () => {

describe('when {leading: false, trailing:true}', () => {
beforeEach(() => {
throttled = throttle(spy, 2, { leading: false })
throttled = throttle(spy, 2, { leading: false }).throttled
})

it('should call throttled function after the wait period', () => {
Expand Down Expand Up @@ -105,7 +106,7 @@ describe('utils', () => {

describe('when {leading: true, trailing:false}', () => {
beforeEach(() => {
throttled = throttle(spy, 2, { trailing: false })
throttled = throttle(spy, 2, { trailing: false }).throttled
})

it('should call throttled function immediately', () => {
Expand Down Expand Up @@ -149,7 +150,7 @@ describe('utils', () => {

describe('when {leading: true, trailing:true}', () => {
beforeEach(() => {
throttled = throttle(spy, 2)
throttled = throttle(spy, 2).throttled
})

it('should call throttled function immediately', () => {
Expand Down Expand Up @@ -190,6 +191,32 @@ describe('utils', () => {
expect(spy).toHaveBeenCalledTimes(2)
})
})

describe('stop', () => {
beforeEach(() => {
const result = throttle(spy, 2)
stop = result.stop
throttled = result.throttled
})

it('should abort pending execution', () => {
throttled()
throttled()
expect(spy).toHaveBeenCalledTimes(1)

stop()

jasmine.clock().tick(2)
expect(spy).toHaveBeenCalledTimes(1)
})

it('should dismiss future calls', () => {
stop()
throttled()
jasmine.clock().tick(2)
expect(spy).toHaveBeenCalledTimes(0)
})
})
})

describe('format', () => {
Expand Down
12 changes: 9 additions & 3 deletions packages/rum/src/viewCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,13 @@ function newView(
viewContext = { id, location, sessionId: session.getId() }

// Update the view every time the measures are changing
const scheduleViewUpdate = throttle(monitor(updateView), THROTTLE_VIEW_UPDATE_PERIOD, {
leading: false,
})
const { throttled: scheduleViewUpdate, stop: stopScheduleViewUpdate } = throttle(
monitor(updateView),
THROTTLE_VIEW_UPDATE_PERIOD,
{
leading: false,
}
)
function updateMeasures(newMeasures: Partial<ViewMeasures>) {
measures = { ...measures, ...newMeasures }
scheduleViewUpdate()
Expand All @@ -110,6 +114,8 @@ function newView(
end() {
stopTimingsTracking()
stopEventCountsTracking()
// prevent pending view updates execution
stopScheduleViewUpdate()
// Make a final view update
updateView()
},
Expand Down
21 changes: 21 additions & 0 deletions packages/rum/test/viewCollection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,4 +297,25 @@ describe('rum view measures', () => {
userActionCount: 0,
})
})

it('should not update measures after ending a view', () => {
jasmine.clock().install()
expect(getRumEventCount()).toEqual(1)

lifeCycle.notify(LifeCycleEventType.RESOURCE_ADDED_TO_BATCH)

expect(getRumEventCount()).toEqual(1)

history.pushState({}, '', '/bar')

expect(getRumEventCount()).toEqual(3)
expect(getViewEvent(1).id).toEqual(getViewEvent(0).id)
expect(getViewEvent(2).id).not.toEqual(getViewEvent(0).id)

jasmine.clock().tick(THROTTLE_VIEW_UPDATE_PERIOD)

expect(getRumEventCount()).toEqual(3)

jasmine.clock().uninstall()
})
})