-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add browser and extension session (#11)
* feat: add browser and extension session * fix: fix lint Co-authored-by: alpers <[email protected]>
- Loading branch information
1 parent
c6204fe
commit 7030584
Showing
2 changed files
with
60 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright 2021-2022 zcloak authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import storage from 'store/storages/sessionStorage'; | ||
|
||
import { BaseStore } from './BaseStore'; | ||
|
||
export class BrowserStore extends BaseStore { | ||
public all(fn: (key: string, value: unknown) => void): void { | ||
storage.each((value: unknown, key: string): void => { | ||
fn(key, value); | ||
}); | ||
} | ||
|
||
public get(key: string, fn: (value: unknown) => void): void { | ||
fn(storage.read(key) as unknown); | ||
} | ||
|
||
public remove(key: string, fn?: () => void): void { | ||
storage.remove(key); | ||
fn && fn(); | ||
} | ||
|
||
public set(key: string, value: unknown, fn?: () => void): void { | ||
storage.write(key, value as string); | ||
fn && fn(); | ||
} | ||
} |
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,32 @@ | ||
// Copyright 2021-2022 zcloak authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { BaseStore } from './BaseStore'; | ||
|
||
const session = chrome.storage.session; | ||
|
||
export class ExtensionSession extends BaseStore { | ||
public all(fn: (key: string, value: string) => void) { | ||
session.get(null, (items) => { | ||
for (const key in items) { | ||
fn(key, items[key]); | ||
} | ||
}); | ||
} | ||
|
||
public get(key: string, fn: (value: unknown) => void) { | ||
session.get(key, (item) => { | ||
fn(item[key] as unknown); | ||
}); | ||
} | ||
|
||
public remove(key: string, fn?: () => void) { | ||
session.remove(key); | ||
fn && fn(); | ||
} | ||
|
||
public set(key: string, value: unknown, fn?: () => void) { | ||
session.set({ [key]: value }); | ||
fn && fn(); | ||
} | ||
} |