Skip to content

Commit

Permalink
Keep untracked session in sessionContextHistory
Browse files Browse the repository at this point in the history
  • Loading branch information
amortemousque committed Feb 1, 2024
1 parent 4308cbb commit e708f8d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 16 deletions.
46 changes: 34 additions & 12 deletions packages/core/src/domain/session/sessionStore.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,13 @@ describe('session store', () => {
expect(sessionStoreManager.getSession().id).toBeDefined()
expectTrackedSessionToBeInStore()
expect(expireSpy).not.toHaveBeenCalled()
expect(renewSpy).toHaveBeenCalled()
expect(renewSpy).toHaveBeenCalledTimes(1)
}
)

it(
'when session not in cache, session not in store and new session not tracked, ' +
'should store not tracked session',
'should store not tracked session and trigger renew session',
() => {
setupSessionStore(() => ({ isTracked: false, trackingType: FakeTrackingType.NOT_TRACKED }))

Expand All @@ -150,7 +150,7 @@ describe('session store', () => {
expect(sessionStoreManager.getSession().id).toBeUndefined()
expectNotTrackedSessionToBeInStore()
expect(expireSpy).not.toHaveBeenCalled()
expect(renewSpy).not.toHaveBeenCalled()
expect(renewSpy).toHaveBeenCalledTimes(1)
}
)

Expand All @@ -163,7 +163,7 @@ describe('session store', () => {
expect(sessionStoreManager.getSession().id).toBe(FIRST_ID)
expectTrackedSessionToBeInStore(FIRST_ID)
expect(expireSpy).not.toHaveBeenCalled()
expect(renewSpy).toHaveBeenCalled()
expect(renewSpy).toHaveBeenCalledTimes(1)
})

it(
Expand All @@ -181,13 +181,13 @@ describe('session store', () => {
expect(sessionId).not.toBe(FIRST_ID)
expectTrackedSessionToBeInStore(sessionId)
expect(expireSpy).toHaveBeenCalled()
expect(renewSpy).toHaveBeenCalled()
expect(renewSpy).toHaveBeenCalledTimes(1)
}
)

it(
'when session in cache, session not in store and new session not tracked, ' +
'should expire session and store not tracked session',
'should expire session, store not tracked session and trigger renew session',
() => {
setSessionInStore(FakeTrackingType.TRACKED, FIRST_ID)
setupSessionStore(() => ({ isTracked: false, trackingType: FakeTrackingType.NOT_TRACKED }))
Expand All @@ -199,13 +199,13 @@ describe('session store', () => {
expect(sessionStoreManager.getSession()[PRODUCT_KEY]).toBeDefined()
expectNotTrackedSessionToBeInStore()
expect(expireSpy).toHaveBeenCalled()
expect(renewSpy).not.toHaveBeenCalled()
expect(renewSpy).toHaveBeenCalledTimes(1)
}
)

it(
'when session not tracked in cache, session not in store and new session not tracked, ' +
'should expire session and store not tracked session',
'should expire session, store not tracked session and trigger renew session',
() => {
setSessionInStore(FakeTrackingType.NOT_TRACKED)
setupSessionStore(() => ({ isTracked: false, trackingType: FakeTrackingType.NOT_TRACKED }))
Expand All @@ -217,7 +217,7 @@ describe('session store', () => {
expect(sessionStoreManager.getSession()[PRODUCT_KEY]).toBeDefined()
expectNotTrackedSessionToBeInStore()
expect(expireSpy).toHaveBeenCalled()
expect(renewSpy).not.toHaveBeenCalled()
expect(renewSpy).toHaveBeenCalledTimes(1)
}
)

Expand Down Expand Up @@ -248,13 +248,13 @@ describe('session store', () => {
expect(sessionStoreManager.getSession().id).toBe(SECOND_ID)
expectTrackedSessionToBeInStore(SECOND_ID)
expect(expireSpy).toHaveBeenCalled()
expect(renewSpy).toHaveBeenCalled()
expect(renewSpy).toHaveBeenCalledTimes(1)
}
)

it(
'when session in cache is different session than in store and store session is not tracked, ' +
'should expire session and store not tracked session',
'should expire session, store not tracked session and trigger renew',
() => {
setSessionInStore(FakeTrackingType.TRACKED, FIRST_ID)
setupSessionStore((rawTrackingType) => ({
Expand All @@ -268,7 +268,7 @@ describe('session store', () => {
expect(sessionStoreManager.getSession().id).toBeUndefined()
expectNotTrackedSessionToBeInStore()
expect(expireSpy).toHaveBeenCalled()
expect(renewSpy).not.toHaveBeenCalled()
expect(renewSpy).toHaveBeenCalledTimes(1)
}
)
})
Expand All @@ -281,6 +281,7 @@ describe('session store', () => {

expect(sessionStoreManager.getSession().id).toBeUndefined()
expect(expireSpy).not.toHaveBeenCalled()
expect(renewSpy).not.toHaveBeenCalled()
})

it('when session not in cache and session in store, should do nothing', () => {
Expand All @@ -291,6 +292,7 @@ describe('session store', () => {

expect(sessionStoreManager.getSession().id).toBeUndefined()
expect(expireSpy).not.toHaveBeenCalled()
expect(renewSpy).not.toHaveBeenCalled()
})

it('when session in cache and session not in store, should expire session', () => {
Expand All @@ -302,6 +304,7 @@ describe('session store', () => {

expect(sessionStoreManager.getSession().id).toBeUndefined()
expect(expireSpy).toHaveBeenCalled()
expect(renewSpy).not.toHaveBeenCalled()
})

it('when session in cache is same session than in store, should expand session', () => {
Expand All @@ -314,6 +317,7 @@ describe('session store', () => {
expect(sessionStoreManager.getSession().id).toBe(FIRST_ID)
expect(sessionStoreManager.getSession().expire).toBe(getStoreExpiration())
expect(expireSpy).not.toHaveBeenCalled()
expect(renewSpy).not.toHaveBeenCalled()
})

it('when session in cache is different session than in store, should expire session', () => {
Expand All @@ -326,7 +330,25 @@ describe('session store', () => {
expect(sessionStoreManager.getSession().id).toBeUndefined()
expectTrackedSessionToBeInStore(SECOND_ID)
expect(expireSpy).toHaveBeenCalled()
expect(renewSpy).not.toHaveBeenCalled()
})

it(
'when session in cache is different session than in store and store session is not tracked, ' +
'should expire session and trigger renew',
() => {
setSessionInStore(FakeTrackingType.TRACKED, FIRST_ID)
setupSessionStore(() => ({ isTracked: false, trackingType: FakeTrackingType.NOT_TRACKED }))
setSessionInStore(FakeTrackingType.NOT_TRACKED)

sessionStoreManager.expandSession()

expect(sessionStoreManager.getSession().id).toBeUndefined()
expectNotTrackedSessionToBeInStore()
expect(expireSpy).toHaveBeenCalled()
expect(renewSpy).toHaveBeenCalledTimes(1)
}
)
})

describe('regular watch', () => {
Expand Down
11 changes: 7 additions & 4 deletions packages/core/src/domain/session/sessionStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,15 @@ export function startSessionStore<TrackingType extends string>(
let sessionCache: SessionState = retrieveActiveSession()

function expandOrRenewSession() {
let isTracked: boolean
processSessionStoreOperations(
{
process: (sessionState) => {
const synchronizedSession = synchronizeSession(sessionState)
isTracked = expandOrRenewSessionState(synchronizedSession)
expandOrRenewSessionState(synchronizedSession)
return synchronizedSession
},
after: (sessionState) => {
if (isTracked && !hasSessionInCache()) {
if (!hasSessionInCache()) {
renewSessionInCache(sessionState)
}
sessionCache = sessionState
Expand Down Expand Up @@ -116,6 +115,11 @@ export function startSessionStore<TrackingType extends string>(
if (hasSessionInCache()) {
if (isSessionInCacheOutdated(sessionState)) {
expireSessionInCache()

// Only renew if the session is not tracked, because we want tracked session to be renewed on user activity
if (sessionState[productKey] && !computeSessionState(sessionState[productKey]).isTracked) {
renewSessionInCache(sessionState)
}
} else {
sessionCache = sessionState
}
Expand All @@ -130,7 +134,6 @@ export function startSessionStore<TrackingType extends string>(
sessionState.id = generateUUID()
sessionState.created = String(dateNow())
}
return isTracked
}

function hasSessionInCache() {
Expand Down

0 comments on commit e708f8d

Please sign in to comment.