diff --git a/src/index.ts b/src/index.ts index 00ec097..0e8ceac 100644 --- a/src/index.ts +++ b/src/index.ts @@ -12,7 +12,7 @@ import { } from './main'; import { SplitTest } from './splitTest'; import { uiFactory } from './ui'; -import { UserSessionPersister } from './userSessionPersister'; +import { UserSessionPersister, InMemoryPersister, CookiePersister } from './userSessionPersister'; const ui = uiFactory( testsObservable, @@ -49,6 +49,8 @@ export { ui, SplitTest, UserSessionPersister, + InMemoryPersister, + CookiePersister, }; export default { diff --git a/src/main.ts b/src/main.ts index dd4be4b..0c44bd0 100644 --- a/src/main.ts +++ b/src/main.ts @@ -31,7 +31,9 @@ export function config(userConfig: Partial = {}) { _config.onVariationChange = userConfig.onVariationChange; } if (userConfig.sessionPersister) { + const userSession = _config.sessionPersister.loadUserSession() || ''; _config.sessionPersister = userConfig.sessionPersister; + _config.sessionPersister.saveUserSession(userSession, _config.userSessionDaysToLive); } } diff --git a/src/userSessionPersister.ts b/src/userSessionPersister.ts index 41a18a2..679503a 100644 --- a/src/userSessionPersister.ts +++ b/src/userSessionPersister.ts @@ -1,40 +1,46 @@ 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(); +export class CookiePersister implements UserSessionPersister { + public loadUserSession() { + return CookiePersister.readCookie(config.cookieName); } - 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); + public saveUserSession(userSession: string, daysToLive: number) { + CookiePersister.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(); } - if (c.indexOf(nameEq) === 0) { - return c.substring(nameEq.length, c.length); + 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; } - return null; } - -export class CookiePersister implements UserSessionPersister { +export class InMemoryPersister implements UserSessionPersister { + private endOfLife: number = 0; + private storage: string = ''; public loadUserSession() { - return readCookie(config.cookieName); + return !this.endOfLife || (Date.now() > this.endOfLife) ? '' : this.storage; } - public saveUserSession(userSession: string, daysToLive: number) { - createCookie(config.cookieName, userSession, daysToLive); + this.storage = userSession; + this.endOfLife = Date.now() + (daysToLive*24*60*60*1000); } -} +} \ No newline at end of file