Skip to content

Commit

Permalink
Merge pull request #78 from trustpilot/add-session-persister-to-config
Browse files Browse the repository at this point in the history
Add session persister to config
  • Loading branch information
NicoJanvier authored Sep 3, 2020
2 parents ae6fb53 + 79b3595 commit 3a5d050
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 33 deletions.
13 changes: 13 additions & 0 deletions src/inMemoryPersister.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { InMemoryPersister } from './inMemoryPersister';
import {
config,
create,
Expand All @@ -12,6 +13,7 @@ import {
} from './main';
import { SplitTest } from './splitTest';
import { uiFactory } from './ui';
import { CookiePersister, UserSessionPersister } from './userSessionPersister';

const ui = uiFactory(
testsObservable,
Expand Down Expand Up @@ -47,6 +49,9 @@ export {
reset,
ui,
SplitTest,
UserSessionPersister,
InMemoryPersister,
CookiePersister,
};

export default {
Expand Down
5 changes: 5 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ export function config(userConfig: Partial<Config> = {}) {
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);
}
}

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

0 comments on commit 3a5d050

Please sign in to comment.