Skip to content

Added support for sessionStorage for cache. #194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/__tests__/browserPlatform-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,32 @@ describe('browserPlatform', () => {
});
});

describe('sessionStorage', () => {
const plat = browserPlatform({ bootstrap: 'sessionStorage' });

// Make sure that all of the get/set tests for the localStorage tests
// also work with sessionStorage.
it('returns null or undefined for missing value', async () => {
const value = await plat.localStorage.get(lsKeyPrefix + 'unused-key');
expect(value).not.toBe(expect.anything());
});

it('can get and set value', async () => {
const key = lsKeyPrefix + 'get-set-key';
await plat.localStorage.set(key, 'hello');
const value = await plat.localStorage.get(key);
expect(value).toEqual('hello');
});

it('can delete value', async () => {
const key = lsKeyPrefix + 'delete-key';
await plat.localStorage.set(key, 'hello');
await plat.localStorage.clear(key);
const value = plat.localStorage.get(key);
expect(value).not.toBe(expect.anything());
});
});

describe('localStorage', () => {
// Since we're not currently running these tests in an actual browser, this is really using a
// mock implementation of window.localStorage, but these tests still verify that our async
Expand Down
16 changes: 12 additions & 4 deletions src/browserPlatform.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,28 @@ export default function makeBrowserPlatform(options) {
};

try {
if (window.localStorage) {
// Allow the user to specify which storage type to use. If the user specifies session storage
// then use that. Otherwise default to local storage. Both of these implement the same
// storage interface, documentation here: https://developer.mozilla.org/en-US/docs/Web/API/Storage
const storage =
((options || {}).bootstrap || '').toUpperCase() === 'SESSIONSTORAGE'
? window.sessionStorage
: window.localStorage;

if (storage) {
ret.localStorage = {
get: key =>
new Promise(resolve => {
resolve(window.localStorage.getItem(key));
resolve(storage.getItem(key));
}),
set: (key, value) =>
new Promise(resolve => {
window.localStorage.setItem(key, value);
storage.setItem(key, value);
resolve();
}),
clear: key =>
new Promise(resolve => {
window.localStorage.removeItem(key);
storage.removeItem(key);
resolve();
}),
};
Expand Down