-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): Add sessionStorage and coopreate decorator
- Loading branch information
Showing
5 changed files
with
113 additions
and
9 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
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
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
const Global = 'global'; | ||
export class SessionStorage { | ||
constructor() { | ||
let _store = new Map(); | ||
this._scope = new Map(); | ||
this._scope.set(Global, _store); | ||
} | ||
|
||
set(key, value, scope = Global) { | ||
if (!this._scope.has(scope)) this._scope.set(scope, new Map()); | ||
|
||
let store = this._scope.get(scope); | ||
store.set(key, value); | ||
} | ||
|
||
get(key, scope = Global) { | ||
if (!this._scope.has(scope)) return null; | ||
|
||
let store = this._scope.get(scope); | ||
return store.get(key); | ||
} | ||
|
||
remove(key, scope = Global) { | ||
if (!this._scope.has(scope)) return null; | ||
|
||
let store = this._scope.get(scope); | ||
store.delete(key); | ||
} | ||
|
||
clear(scope = Global) { | ||
if (scope === '*') this._scope.clear(); | ||
|
||
if (!this._scope.has(scope)) return void(0); | ||
|
||
let store = this._scope.get(scope); | ||
store.clear(); | ||
} | ||
} | ||
|
||
export const sessionStorage = new SessionStorage(); | ||
|
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { | ||
SessionStorage, | ||
} from '../src/utils/sessionStorage'; | ||
|
||
import 'jest-plugin-console-matchers/setup'; | ||
|
||
describe('Session Storage', () => { | ||
let map = new Map([]); | ||
let val = {type: 'navigate', value: 'pages/index'}; | ||
let key = 'APP_REDIRECT'; | ||
|
||
test('Basic Usage', ()=>{ | ||
let store = new SessionStorage(); | ||
|
||
store.set(key, val); | ||
expect(store.get(key)).toBe(val); | ||
|
||
store.set(key + key, map); | ||
expect(store.get(key + key)).toBe(map); | ||
|
||
store.remove(key); | ||
expect(store.get(key)).toBeFalsy(); | ||
|
||
store.set(key, val); | ||
store.clear(); | ||
expect(store.get(key)).toBeFalsy(); | ||
expect(store.get(key + key)).toBeFalsy(); | ||
}); | ||
|
||
test('with scope', () => { | ||
let store = new SessionStorage(); | ||
|
||
store.set(key, val, 'app'); | ||
|
||
expect(store.get(key)).toBeFalsy(); | ||
expect(store.get(key, 'app')).toBe(val); | ||
|
||
store.remove(key, 'app'); | ||
expect(store.get(key, 'app')).toBeFalsy(); | ||
|
||
expect(store._scope.get('app')).not.toBeFalsy(); | ||
|
||
store.clear('*'); | ||
expect(store._scope.get('app')).toBeFalsy(); | ||
}); | ||
}); |