Skip to content

Commit

Permalink
feat: add browser and extension session (#11)
Browse files Browse the repository at this point in the history
* feat: add browser and extension session

* fix: fix lint

Co-authored-by: alpers <[email protected]>
  • Loading branch information
linhan and linhan-work authored Jan 5, 2023
1 parent c6204fe commit 7030584
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
28 changes: 28 additions & 0 deletions packages/ui-store/src/BrowserSession.ts
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();
}
}
32 changes: 32 additions & 0 deletions packages/ui-store/src/ExtensionSession.ts
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();
}
}

0 comments on commit 7030584

Please sign in to comment.