Skip to content

Commit

Permalink
feat: add in memory storage
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Janvier committed Sep 3, 2020
1 parent 8b8651d commit d55e76e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 27 deletions.
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -49,6 +49,8 @@ export {
ui,
SplitTest,
UserSessionPersister,
InMemoryPersister,
CookiePersister,
};

export default {
Expand Down
2 changes: 2 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ export function config(userConfig: Partial<Config> = {}) {
_config.onVariationChange = userConfig.onVariationChange;
}
if (userConfig.sessionPersister) {
const userSession = _config.sessionPersister.loadUserSession() || '';
_config.sessionPersister = userConfig.sessionPersister;
_config.sessionPersister.saveUserSession(userSession, _config.userSessionDaysToLive);
}
}

Expand Down
58 changes: 32 additions & 26 deletions src/userSessionPersister.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
}

0 comments on commit d55e76e

Please sign in to comment.