-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
- Loading branch information
Showing
4 changed files
with
100 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
module.exports = { | ||
presets: [ | ||
"@babel/typescript", | ||
[ | ||
"@babel/env", | ||
{ | ||
useBuiltIns: "usage", | ||
corejs: "3.0.0", | ||
}, | ||
], | ||
], | ||
plugins: [ | ||
"@babel/plugin-proposal-class-properties", | ||
], | ||
}; | ||
presets: [ | ||
'@babel/typescript', | ||
[ | ||
'@babel/env', | ||
{ | ||
useBuiltIns: 'usage', | ||
corejs: '3.0.0', | ||
}, | ||
], | ||
], | ||
plugins: [ | ||
'@babel/plugin-proposal-class-properties', | ||
], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,43 @@ | ||
import StorageBuilder from './storagebuilder' | ||
import ScopedStorage from './scopedstorage' | ||
|
||
/** | ||
* Get the storage builder for an app | ||
* @param appId App ID to scope storage | ||
*/ | ||
export function getBuilder(appId: string): StorageBuilder { | ||
return new StorageBuilder(appId) | ||
return new StorageBuilder(appId) | ||
} | ||
|
||
function clearStorage(storage: Storage, pred?: (string) => boolean): void { | ||
Object.keys(storage) | ||
.filter(k => pred ? pred(k) : true) | ||
.map(storage.removeItem.bind(storage)) | ||
/** | ||
* Clear values from storage | ||
* @param storage The storage to clear | ||
* @param pred Callback to check if value should be cleared | ||
*/ | ||
function clearStorage(storage: Storage, pred?: (value: string) => boolean): void { | ||
Object.keys(storage) | ||
.filter(k => pred ? pred(k) : true) | ||
.map(storage.removeItem.bind(storage)) | ||
} | ||
|
||
/** | ||
* Clear all values from all storages | ||
*/ | ||
export function clearAll(): void { | ||
const storages = [ | ||
window.sessionStorage, | ||
window.localStorage, | ||
] | ||
storages.map(s => clearStorage(s)) | ||
const storages = [ | ||
window.sessionStorage, | ||
window.localStorage, | ||
] | ||
storages.map(s => clearStorage(s)) | ||
} | ||
|
||
/** | ||
* Clear ony non persistent values | ||
*/ | ||
export function clearNonPersistent(): void { | ||
const storages = [ | ||
window.sessionStorage, | ||
window.localStorage, | ||
] | ||
storages.map(s => clearStorage(s, k => !k.startsWith(ScopedStorage.GLOBAL_SCOPE_PERSISTENT))) | ||
const storages = [ | ||
window.sessionStorage, | ||
window.localStorage, | ||
] | ||
storages.map(s => clearStorage(s, k => !k.startsWith(ScopedStorage.GLOBAL_SCOPE_PERSISTENT))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,32 @@ | ||
|
||
import Storage from './storage' | ||
import ScopedStorage from './scopedstorage' | ||
|
||
export default class StorageBuilder { | ||
|
||
private appId: string | ||
private persisted = false | ||
private clearedOnLogout = false | ||
|
||
constructor(appId: string) { | ||
this.appId = appId | ||
} | ||
|
||
persist(persist: boolean = true): StorageBuilder { | ||
this.persisted = persist | ||
return this | ||
} | ||
|
||
clearOnLogout(clear: boolean = true): StorageBuilder { | ||
this.clearedOnLogout = clear | ||
return this | ||
} | ||
|
||
build(): Storage { | ||
return new ScopedStorage( | ||
this.appId, | ||
this.persisted ? window.localStorage : window.sessionStorage, | ||
!this.clearedOnLogout | ||
) | ||
} | ||
private appId: string | ||
private persisted = false | ||
private clearedOnLogout = false | ||
|
||
constructor(appId: string) { | ||
this.appId = appId | ||
} | ||
|
||
persist(persist: boolean = true): StorageBuilder { | ||
this.persisted = persist | ||
return this | ||
} | ||
|
||
clearOnLogout(clear: boolean = true): StorageBuilder { | ||
this.clearedOnLogout = clear | ||
return this | ||
} | ||
|
||
build(): Storage { | ||
return new ScopedStorage( | ||
this.appId, | ||
this.persisted ? window.localStorage : window.sessionStorage, | ||
!this.clearedOnLogout, | ||
) | ||
} | ||
|
||
} |