Skip to content

Commit

Permalink
fix: v1.2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
eliot-ye committed Jan 8, 2024
1 parent d609bd0 commit e9e5bf5
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 30 deletions.
20 changes: 12 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,12 @@ const LS = createAsyncStorage(

```ts
interface StorageEngine {
setItem: (key: string, value: string) => Promise<void> | void;
/** 配置是否支持对象存储,如果为 true 则 setItem 的 value 值可能是 JSON,否则为字符串存储 */
supportObject?: boolean;
setItem: (key: string, value: any) => Promise<void> | void;
getItem: (
key: string
) => Promise<string | null | undefined> | string | null | undefined;
) => Promise<any | null | undefined> | any | null | undefined;
removeItem: (key: string) => Promise<void> | void;
onReady?: () => Promise<void>;
}
Expand All @@ -103,7 +105,7 @@ interface StorageEngine {
```ts
import type { StorageEngine } from "gpl-async-storage";

export function ELocalStorage(name = "LS"): StorageEngine | null {
export function ELocalStorage(name = "LS") {
let ready = false;
try {
const testString = "test";
Expand All @@ -113,24 +115,26 @@ export function ELocalStorage(name = "LS"): StorageEngine | null {
ready = true;
}
} catch (error) {
console.error("ELocalStorage", "unready", error);
CusLog.error("ELocalStorage", "unready", error);
}

if (!ready) {
return null;
}

return {
async getItem(key: string) {
const storageEngine: StorageEngine = {
async getItem(key) {
return localStorage.getItem(`${name}_${key}`);
},
async setItem(key: string, value: string) {
async setItem(key, value) {
return localStorage.setItem(`${name}_${key}`, value);
},
async removeItem(key: string) {
async removeItem(key) {
return localStorage.removeItem(`${name}_${key}`);
},
};

return storageEngine;
}
```

Expand Down
20 changes: 6 additions & 14 deletions libs/asyncStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,13 @@ interface SubscribeFn {
(): void;
}

export interface StorageEngine<SO extends boolean> {
supportObject: SO;
setItem: (
key: string,
value: SO extends true ? Object : string
) => Promise<void> | void;
export interface StorageEngine {
/** 配置是否支持对象存储,如果为 true 则 setItem 的 value 值可能是 JSON,否则为字符串存储 */
supportObject?: boolean;
setItem: (key: string, value: any) => Promise<void> | void;
getItem: (
key: string
) => SO extends true
? Object
: Promise<string | null | undefined> | string | null | undefined;
) => Promise<any | null | undefined> | any | null | undefined;
removeItem: (key: string) => Promise<void> | void;
onReady?: () => Promise<void>;
}
Expand All @@ -35,11 +31,7 @@ export enum ErrorMessage {

export function createAsyncStorage<T extends JSONConstraint>(
initialData: T,
engines: (
| StorageEngine<boolean>
| (() => StorageEngine<boolean> | null)
| null
)[],
engines: (StorageEngine | (() => StorageEngine | null) | null)[],
option: Option<T> = {}
) {
type Key = keyof T;
Expand Down
2 changes: 1 addition & 1 deletion libs/engine/cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function ECookie(name = "LS") {
return null;
}

const storageEngine: StorageEngine<false> = {
const storageEngine: StorageEngine = {
supportObject: false,
getItem(key) {
return (
Expand Down
2 changes: 1 addition & 1 deletion libs/engine/indexedDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export function EIndexedDB(name = "asyncStorage", version = 1) {
return null;
}

const storageEngine: StorageEngine<true> = {
const storageEngine: StorageEngine = {
supportObject: true,
onReady() {
return new Promise((resolve) => {
Expand Down
3 changes: 1 addition & 2 deletions libs/engine/localStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ export function ELocalStorage(name = "LS") {
return null;
}

const storageEngine: StorageEngine<false> = {
supportObject: false,
const storageEngine: StorageEngine = {
async getItem(key) {
return localStorage.getItem(`${name}_${key}`);
},
Expand Down
5 changes: 3 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gpl-async-storage",
"version": "1.2.0",
"version": "1.2.1",
"description": "一个异步 API 的本地存储库",
"license": "MulanPSL-2.0",
"author": {
Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const LS = createAsyncStorage(
b: "b",
testObject: { a: 1, b: 2 },
},
[EIndexedDB()]
[EIndexedDB(), localStorage]
);

document.querySelector<HTMLDivElement>("#app")!.innerHTML = `
Expand Down

0 comments on commit e9e5bf5

Please sign in to comment.