Skip to content

Commit

Permalink
feat(dataSource): Create SessionStorageDataSource
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherthielen committed Jan 16, 2019
1 parent 1a934b4 commit a26579c
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/DSRDataStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,21 @@ export class StateObjectDataStore implements DSRDataStore {
export class LocalStorageDataStore implements DSRDataStore {
private router: UIRouter;
private key = 'uiRouterDeepStateRedirect';
private _storage: Storage = localStorage;

constructor(storage?: Storage) {
this._storage = storage || localStorage;
}

private getStore() {
const item = localStorage.getItem(this.key);
const item = this._storage.getItem(this.key);
return JSON.parse(item || '{}');
}

private setStore(contents: any) {
if (contents) {
try {
localStorage.setItem(this.key, JSON.stringify(contents));
this._storage.setItem(this.key, JSON.stringify(contents));
} catch (err) {
console.error(
'UI-Router Deep State Redirect: cannot store object in LocalStorage. Is there a circular reference?',
Expand All @@ -56,7 +61,7 @@ export class LocalStorageDataStore implements DSRDataStore {
console.error(err);
}
} else {
localStorage.removeItem(this.key);
this._storage.removeItem(this.key);
}
}

Expand All @@ -82,3 +87,9 @@ export class LocalStorageDataStore implements DSRDataStore {
this.setStore(store);
}
}

export class SessionStorageDataStore extends LocalStorageDataStore {
constructor() {
super(sessionStorage);
}
}

0 comments on commit a26579c

Please sign in to comment.