diff --git a/src/inMemoryPersister.ts b/src/inMemoryPersister.ts new file mode 100644 index 0000000..b0a388d --- /dev/null +++ b/src/inMemoryPersister.ts @@ -0,0 +1,13 @@ +import { UserSessionPersister } from './userSessionPersister'; + +export class InMemoryPersister implements UserSessionPersister { + private endOfLife: number = 0; + private storage: string = ''; + public loadUserSession() { + return !this.endOfLife || (Date.now() > this.endOfLife) ? '' : this.storage; + } + public saveUserSession(userSession: string, daysToLive: number) { + this.storage = userSession; + this.endOfLife = Date.now() + (daysToLive * 24 * 60 * 60 * 1000); + } +} diff --git a/src/index.ts b/src/index.ts index 9185589..c520121 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,4 @@ +import { InMemoryPersister } from './inMemoryPersister'; import { config, create, @@ -12,6 +13,7 @@ import { } from './main'; import { SplitTest } from './splitTest'; import { uiFactory } from './ui'; +import { CookiePersister, UserSessionPersister } from './userSessionPersister'; const ui = uiFactory( testsObservable, @@ -47,6 +49,9 @@ export { reset, ui, SplitTest, + UserSessionPersister, + InMemoryPersister, + CookiePersister, }; export default { diff --git a/src/main.ts b/src/main.ts index 773e7ad..aeec0c6 100644 --- a/src/main.ts +++ b/src/main.ts @@ -30,6 +30,11 @@ export function config(userConfig: Partial = {}) { if (userConfig.onVariationChange) { _config.onVariationChange = userConfig.onVariationChange; } + if (userConfig.sessionPersister) { + const session = _config.sessionPersister.loadUserSession() || ''; + _config.sessionPersister = userConfig.sessionPersister; + _config.sessionPersister.saveUserSession(session, _config.userSessionDaysToLive); + } } /** diff --git a/src/userSessionPersister.ts b/src/userSessionPersister.ts index 41a18a2..ebc03ff 100644 --- a/src/userSessionPersister.ts +++ b/src/userSessionPersister.ts @@ -1,40 +1,35 @@ import config from './config'; - export interface UserSessionPersister { loadUserSession(): string | null; saveUserSession(userSession: string, daysToLive: number): void; } - -function createCookie(name: string, value: string, days: number): void { - let expires = ''; - if (days) { - const date = new Date(); - date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); - expires = '; expires=' + date.toUTCString(); - } - document.cookie = name + '=' + value + expires + '; path=/'; -} - -function readCookie(name: string): string | null { - const nameEq = name + '='; - const ca = document.cookie.split(';'); - for (let c of ca) { - while (c.charAt(0) === ' ') { - c = c.substring(1, c.length); - } - if (c.indexOf(nameEq) === 0) { - return c.substring(nameEq.length, c.length); - } - } - return null; -} - export class CookiePersister implements UserSessionPersister { - public loadUserSession() { - return readCookie(config.cookieName); - } - - public saveUserSession(userSession: string, daysToLive: number) { - createCookie(config.cookieName, userSession, daysToLive); - } + private static createCookie(name: string, value: string, days: number): void { + let expires = ''; + if (days) { + const date = new Date(); + date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); + expires = '; expires=' + date.toUTCString(); + } + document.cookie = name + '=' + value + expires + '; path=/'; + } + private static readCookie(name: string): string | null { + const nameEq = name + '='; + const ca = document.cookie.split(';'); + for (let c of ca) { + while (c.charAt(0) === ' ') { + c = c.substring(1, c.length); + } + if (c.indexOf(nameEq) === 0) { + return c.substring(nameEq.length, c.length); + } + } + return null; + } + public loadUserSession() { + return CookiePersister.readCookie(config.cookieName); + } + public saveUserSession(userSession: string, daysToLive: number) { + CookiePersister.createCookie(config.cookieName, userSession, daysToLive); + } }