Skip to content

Commit

Permalink
Rename storages concepts for better clarity
Browse files Browse the repository at this point in the history
The terms "local" and "session" are not self-explanatory and can be confused with the concept of a user session. To avoid confusion, the terms "tab storage" and "browser storage" were adopted to refer to "session storage" and "local storage" respectively.
  • Loading branch information
marcospassos authored May 10, 2020
1 parent b5126df commit 262da44
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 54 deletions.
18 changes: 9 additions & 9 deletions src/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ export class Container {

private createContext(): Context {
return Context.load(
this.getInternalSessionStorage('context'),
this.getInternalApplicationStorage('context'),
this.getGlobalTabStorage('context'),
this.getGlobalBrowserStorage('context'),
this.configuration.tokenScope,
);
}
Expand Down Expand Up @@ -176,7 +176,7 @@ export class Container {

return new MonitoredQueue<string>(
new CapacityRestrictedQueue(
new PersistentQueue(this.getInternalSessionStorage('queue'), tab.id),
new PersistentQueue(this.getGlobalTabStorage('queue'), tab.id),
this.configuration.beaconQueueSize,
),
this.getLogger('BeaconQueue'),
Expand All @@ -197,19 +197,19 @@ export class Container {
return new NullLogger();
}

public getSessionStorage(namespace: string, ...subnamespace: string[]): Storage {
return this.getInternalSessionStorage('external', namespace, ...subnamespace);
public getTabStorage(namespace: string, ...subnamespace: string[]): Storage {
return this.getGlobalTabStorage('external', namespace, ...subnamespace);
}

public getApplicationStorage(namespace: string, ...subnamespace: string[]): Storage {
return this.getInternalApplicationStorage('external', namespace, ...subnamespace);
public getBrowserStorage(namespace: string, ...subnamespace: string[]): Storage {
return this.getGlobalBrowserStorage('external', namespace, ...subnamespace);
}

private getInternalSessionStorage(namespace: string, ...subnamespace: string[]): Storage {
private getGlobalTabStorage(namespace: string, ...subnamespace: string[]): Storage {
return new NamespacedStorage(sessionStorage, this.resolveStorageNamespace(namespace, ...subnamespace));
}

private getInternalApplicationStorage(namespace: string, ...subnamespace: string[]): Storage {
private getGlobalBrowserStorage(namespace: string, ...subnamespace: string[]): Storage {
return new NamespacedStorage(localStorage, this.resolveStorageNamespace(namespace, ...subnamespace));
}

Expand Down
14 changes: 7 additions & 7 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ export default class Context {
this.tokenStorage = tokenStorage;
}

public static load(sessionStorage: Storage, applicationStorage: Storage, tokenScope: TokenScope): Context {
let tabId: string | null = sessionStorage.getItem('tab');
public static load(tabStorage: Storage, browserStorage: Storage, tokenScope: TokenScope): Context {
let tabId: string | null = tabStorage.getItem('tab');
let newTab = false;

if (tabId === null) {
Expand All @@ -28,20 +28,20 @@ export default class Context {

const tab = new Tab(tabId, newTab);

sessionStorage.removeItem('tab');
tabStorage.removeItem('tab');

tab.addListener('unload', () => sessionStorage.setItem('tab', tab.id));
tab.addListener('unload', () => tabStorage.setItem('tab', tab.id));

switch (tokenScope) {
case 'isolated':
return new Context(tab, new InMemoryStorage());

case 'global':
return new Context(tab, new PersistentStorage(applicationStorage));
return new Context(tab, new PersistentStorage(browserStorage));

case 'contextual': {
const primaryStorage = new PersistentStorage(sessionStorage, `${tabId}.token`);
const secondaryStorage = new PersistentStorage(applicationStorage);
const primaryStorage = new PersistentStorage(tabStorage, `${tabId}.token`);
const secondaryStorage = new PersistentStorage(browserStorage);

if (tab.isNew) {
primaryStorage.setToken(secondaryStorage.getToken());
Expand Down
8 changes: 4 additions & 4 deletions src/facade/sdkFacade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,12 @@ export default class SdkFacade {
return this.sdk.getLogger(...namespace);
}

public getSessionStorage(namespace: string, ...subnamespace: string[]): Storage {
return this.sdk.getSessionStorage(namespace, ...subnamespace);
public getTabStorage(namespace: string, ...subnamespace: string[]): Storage {
return this.sdk.getTabStorage(namespace, ...subnamespace);
}

public getApplicationStorage(namespace: string, ...subnamespace: string[]): Storage {
return this.sdk.getApplicationStorage(namespace, ...subnamespace);
public getBrowserStorage(namespace: string, ...subnamespace: string[]): Storage {
return this.sdk.getBrowserStorage(namespace, ...subnamespace);
}

public close(): Promise<void> {
Expand Down
8 changes: 4 additions & 4 deletions src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,12 @@ export default class Sdk {
return this.container.getLogger(...namespace);
}

public getSessionStorage(namespace: string, ...subnamespace: string[]): Storage {
return this.container.getSessionStorage(namespace, ...subnamespace);
public getTabStorage(namespace: string, ...subnamespace: string[]): Storage {
return this.container.getTabStorage(namespace, ...subnamespace);
}

public getApplicationStorage(namespace: string, ...subnamespace: string[]): Storage {
return this.container.getApplicationStorage(namespace, ...subnamespace);
public getBrowserStorage(namespace: string, ...subnamespace: string[]): Storage {
return this.container.getBrowserStorage(namespace, ...subnamespace);
}

public async close(): Promise<void> {
Expand Down
8 changes: 4 additions & 4 deletions test/container.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ test('should flush the beacon queue on initialization', async () => {
expect(server).toReceiveMessage(expect.objectContaining({payload: payload}));
});

test('should provide an isolated session storage', () => {
test('should provide an isolated tab storage', () => {
const container = new Container({
...configuration,
debug: true,
Expand All @@ -96,7 +96,7 @@ test('should provide an isolated session storage', () => {
jest.spyOn(window.Storage.prototype, 'setItem');
jest.spyOn(window.Storage.prototype, 'removeItem');

const storage = container.getSessionStorage('session', 'foo');
const storage = container.getTabStorage('session', 'foo');
storage.setItem('key', 'value');
storage.removeItem('key');

Expand All @@ -106,7 +106,7 @@ test('should provide an isolated session storage', () => {
expect(window.sessionStorage.removeItem).toHaveBeenCalledWith(namespacedKey);
});

test('should provide an isolated application storage', () => {
test('should provide an isolated browser storage', () => {
const container = new Container({
...configuration,
debug: true,
Expand All @@ -115,7 +115,7 @@ test('should provide an isolated application storage', () => {
jest.spyOn(window.Storage.prototype, 'setItem');
jest.spyOn(window.Storage.prototype, 'removeItem');

const storage = container.getApplicationStorage('app', 'foo');
const storage = container.getBrowserStorage('app', 'foo');
storage.setItem('key', 'value');
storage.removeItem('key');

Expand Down
28 changes: 14 additions & 14 deletions test/context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ describe('A context', () => {
});

test('should share token across all tabs if the token scope is global', () => {
const sessionStorage = new DumbStorage();
const applicationStorage = new DumbStorage();
const tabStorage = new DumbStorage();
const browserStorage = new DumbStorage();

const contextA = Context.load(sessionStorage, applicationStorage, 'global');
const contextA = Context.load(tabStorage, browserStorage, 'global');

contextA.setToken(carolToken);

const contextB = Context.load(sessionStorage, applicationStorage, 'global');
const contextB = Context.load(tabStorage, browserStorage, 'global');

expect(contextA.getToken()).toEqual(carolToken);
expect(contextB.getToken()).toEqual(carolToken);
Expand All @@ -46,11 +46,11 @@ describe('A context', () => {
});

test('should share token across related tabs if the token scope is contextual', () => {
const sessionStorage = new DumbStorage();
const applicationStorage = new DumbStorage();
const tabStorage = new DumbStorage();
const browserStorage = new DumbStorage();

// Open the tab A
const contextA = Context.load(sessionStorage, applicationStorage, 'contextual');
const contextA = Context.load(tabStorage, browserStorage, 'contextual');

contextA.setToken(carolToken);

Expand All @@ -60,7 +60,7 @@ describe('A context', () => {
tabEventEmulator.newTab();

// Open tab B from tab A
const contextB = Context.load(sessionStorage, applicationStorage, 'contextual');
const contextB = Context.load(tabStorage, browserStorage, 'contextual');

// Both tabs should have carol's token
expect(contextA.getToken()).toEqual(carolToken);
Expand All @@ -75,7 +75,7 @@ describe('A context', () => {
tabEventEmulator.newTab();

// Open tab C from tab B
const contextC = Context.load(sessionStorage, applicationStorage, 'contextual');
const contextC = Context.load(tabStorage, browserStorage, 'contextual');

// Both tab B and C should have the erick's token
expect(contextA.getToken()).toEqual(carolToken);
Expand All @@ -88,7 +88,7 @@ describe('A context', () => {
tabEventEmulator.newTab();

// Open tab D from tab A
const contextD = Context.load(sessionStorage, applicationStorage, 'contextual');
const contextD = Context.load(tabStorage, browserStorage, 'contextual');

// Both tab A and D should have the carol's token
expect(contextA.getToken()).toEqual(carolToken);
Expand All @@ -98,14 +98,14 @@ describe('A context', () => {
});

test('should not share token across tabs if the token scope is isolated', () => {
const sessionStorage = new DumbStorage();
const applicationStorage = new DumbStorage();
const tabStorage = new DumbStorage();
const browserStorage = new DumbStorage();

const contextA = Context.load(sessionStorage, applicationStorage, 'isolated');
const contextA = Context.load(tabStorage, browserStorage, 'isolated');

contextA.setToken(carolToken);

const contextB = Context.load(sessionStorage, applicationStorage, 'isolated');
const contextB = Context.load(tabStorage, browserStorage, 'isolated');

expect(contextA.getToken()).toEqual(carolToken);
expect(contextB.getToken()).toBeNull();
Expand Down
18 changes: 9 additions & 9 deletions test/facade/sdkFacade.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -487,13 +487,13 @@ describe('A SDK facade', () => {
});

test('should provide an isolated session storage', () => {
const getSessionStorage = jest.fn(() => new DumbStorage());
const getTabStorage = jest.fn(() => new DumbStorage());

jest.spyOn(Sdk, 'init')
.mockImplementationOnce(config => {
const sdk = Sdk.init(config);

jest.spyOn(sdk, 'getSessionStorage').mockImplementation(getSessionStorage);
jest.spyOn(sdk, 'getTabStorage').mockImplementation(getTabStorage);

return sdk;
});
Expand All @@ -503,19 +503,19 @@ describe('A SDK facade', () => {
track: false,
});

sdkFacade.getSessionStorage('a', 'b', 'c');
sdkFacade.getTabStorage('a', 'b', 'c');

expect(getSessionStorage).toHaveBeenLastCalledWith('a', 'b', 'c');
expect(getTabStorage).toHaveBeenLastCalledWith('a', 'b', 'c');
});

test('should provide an isolated application storage', () => {
const getApplicationStorage = jest.fn(() => new DumbStorage());
test('should provide an isolated browser storage', () => {
const getBrowserStorage = jest.fn(() => new DumbStorage());

jest.spyOn(Sdk, 'init')
.mockImplementationOnce(config => {
const sdk = Sdk.init(config);

jest.spyOn(sdk, 'getApplicationStorage').mockImplementation(getApplicationStorage);
jest.spyOn(sdk, 'getBrowserStorage').mockImplementation(getBrowserStorage);

return sdk;
});
Expand All @@ -525,9 +525,9 @@ describe('A SDK facade', () => {
track: false,
});

sdkFacade.getApplicationStorage('a', 'b', 'c');
sdkFacade.getBrowserStorage('a', 'b', 'c');

expect(getApplicationStorage).toHaveBeenLastCalledWith('a', 'b', 'c');
expect(getBrowserStorage).toHaveBeenLastCalledWith('a', 'b', 'c');
});

test('should close the SDK on close', async () => {
Expand Down
6 changes: 3 additions & 3 deletions test/sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,19 +328,19 @@ describe('A SDK', () => {
jest.spyOn(Storage.prototype, 'setItem');

const sdk = Sdk.init(configuration);
const storage = sdk.getSessionStorage('foo', 'bar');
const storage = sdk.getTabStorage('foo', 'bar');

storage.setItem('key', 'value');

const namespacedKey = `croct[${configuration.appId}].external.foo.bar.key`;
expect(window.sessionStorage.setItem).toHaveBeenCalledWith(namespacedKey, 'value');
});

test('should provide an isolated application storage', () => {
test('should provide an isolated browser storage', () => {
jest.spyOn(Storage.prototype, 'setItem');

const sdk = Sdk.init(configuration);
const storage = sdk.getApplicationStorage('foo', 'bar');
const storage = sdk.getBrowserStorage('foo', 'bar');

storage.setItem('key', 'value');

Expand Down

0 comments on commit 262da44

Please sign in to comment.