Skip to content

Commit e10c916

Browse files
committed
feat: 二次封装localforage支持设置过期时间,提供完整的类型提示
1 parent 320ee2b commit e10c916

File tree

4 files changed

+279
-0
lines changed

4 files changed

+279
-0
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
"intro.js": "^7.2.0",
7070
"js-cookie": "^3.0.5",
7171
"jsbarcode": "^3.11.5",
72+
"localforage": "^1.10.0",
7273
"md-editor-v3": "2.7.2",
7374
"mint-filter": "^4.0.3",
7475
"mitt": "^3.0.1",

pnpm-lock.yaml

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/utils/localforage/index.ts

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import forage from "localforage";
2+
import { LocalForage, ProxyStorage, ExpiresData } from "./types.d";
3+
4+
class StorageProxy implements ProxyStorage {
5+
protected storage: LocalForage;
6+
constructor(storageModel) {
7+
this.storage = storageModel;
8+
this.storage.config({
9+
// driver: [forage.LOCALSTORAGE],
10+
name: "pure-admin"
11+
});
12+
}
13+
14+
/**
15+
* @description 将对应键名的数据保存到离线仓库
16+
* @param k 键名
17+
* @param v 键值
18+
* @param m 缓存时间(单位`分`,默认`0`分钟,永久缓存)
19+
*/
20+
public async setItem<T>(k: string, v: T, m = 0): Promise<T> {
21+
return new Promise((resolve, reject) => {
22+
this.storage
23+
.setItem(k, {
24+
data: v,
25+
expires: new Date().getTime() + m * 60 * 1000
26+
})
27+
.then(value => {
28+
resolve(value.data);
29+
})
30+
.catch(err => {
31+
reject(err);
32+
});
33+
});
34+
}
35+
36+
/**
37+
* @description 从离线仓库中获取对应键名的值
38+
* @param k 键名
39+
*/
40+
public async getItem<T>(k: string): Promise<T> {
41+
return new Promise((resolve, reject) => {
42+
this.storage
43+
.getItem(k)
44+
.then((value: ExpiresData<T>) => {
45+
const data =
46+
value.expires > new Date().getTime() || value.expires === 0
47+
? value.data
48+
: null;
49+
resolve(data);
50+
})
51+
.catch(err => {
52+
reject(err);
53+
});
54+
});
55+
}
56+
57+
/**
58+
* @description 从离线仓库中删除对应键名的值
59+
* @param k 键名
60+
*/
61+
public async removeItem(k: string) {
62+
return new Promise<void>((resolve, reject) => {
63+
this.storage
64+
.removeItem(k)
65+
.then(() => {
66+
resolve();
67+
})
68+
.catch(err => {
69+
reject(err);
70+
});
71+
});
72+
}
73+
74+
/**
75+
* @description 从离线仓库中删除所有的键名,重置数据库
76+
*/
77+
public async clear() {
78+
return new Promise<void>((resolve, reject) => {
79+
this.storage
80+
.clear()
81+
.then(() => {
82+
resolve();
83+
})
84+
.catch(err => {
85+
reject(err);
86+
});
87+
});
88+
}
89+
}
90+
91+
/**
92+
* 二次封装 [localforage](https://localforage.docschina.org/) 支持设置过期时间,提供完整的类型提示
93+
*/
94+
export const localForage = () => new StorageProxy(forage);

src/utils/localforage/types.d.ts

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
// https://github.com/localForage/localForage/blob/master/typings/localforage.d.ts
2+
3+
interface LocalForageDbInstanceOptions {
4+
name?: string;
5+
6+
storeName?: string;
7+
}
8+
9+
interface LocalForageOptions extends LocalForageDbInstanceOptions {
10+
driver?: string | string[];
11+
12+
size?: number;
13+
14+
version?: number;
15+
16+
description?: string;
17+
}
18+
19+
interface LocalForageDbMethodsCore {
20+
getItem<T>(
21+
key: string,
22+
callback?: (err: any, value: T | null) => void
23+
): Promise<T | null>;
24+
25+
setItem<T>(
26+
key: string,
27+
value: T,
28+
callback?: (err: any, value: T) => void
29+
): Promise<T>;
30+
31+
removeItem(key: string, callback?: (err: any) => void): Promise<void>;
32+
33+
clear(callback?: (err: any) => void): Promise<void>;
34+
35+
length(callback?: (err: any, numberOfKeys: number) => void): Promise<number>;
36+
37+
key(
38+
keyIndex: number,
39+
callback?: (err: any, key: string) => void
40+
): Promise<string>;
41+
42+
keys(callback?: (err: any, keys: string[]) => void): Promise<string[]>;
43+
44+
iterate<T, U>(
45+
iteratee: (value: T, key: string, iterationNumber: number) => U,
46+
callback?: (err: any, result: U) => void
47+
): Promise<U>;
48+
}
49+
50+
interface LocalForageDropInstanceFn {
51+
(
52+
dbInstanceOptions?: LocalForageDbInstanceOptions,
53+
callback?: (err: any) => void
54+
): Promise<void>;
55+
}
56+
57+
interface LocalForageDriverMethodsOptional {
58+
dropInstance?: LocalForageDropInstanceFn;
59+
}
60+
61+
// duplicating LocalForageDriverMethodsOptional to preserve TS v2.0 support,
62+
// since Partial<> isn't supported there
63+
interface LocalForageDbMethodsOptional {
64+
dropInstance: LocalForageDropInstanceFn;
65+
}
66+
67+
interface LocalForageDriverDbMethods
68+
extends LocalForageDbMethodsCore,
69+
LocalForageDriverMethodsOptional {}
70+
71+
interface LocalForageDriverSupportFunc {
72+
(): Promise<boolean>;
73+
}
74+
75+
interface LocalForageDriver extends LocalForageDriverDbMethods {
76+
_driver: string;
77+
78+
_initStorage(options: LocalForageOptions): void;
79+
80+
_support?: boolean | LocalForageDriverSupportFunc;
81+
}
82+
83+
interface LocalForageSerializer {
84+
serialize<T>(
85+
value: T | ArrayBuffer | Blob,
86+
callback: (value: string, error: any) => void
87+
): void;
88+
89+
deserialize<T>(value: string): T | ArrayBuffer | Blob;
90+
91+
stringToBuffer(serializedString: string): ArrayBuffer;
92+
93+
bufferToString(buffer: ArrayBuffer): string;
94+
}
95+
96+
interface LocalForageDbMethods
97+
extends LocalForageDbMethodsCore,
98+
LocalForageDbMethodsOptional {}
99+
100+
export interface LocalForage extends LocalForageDbMethods {
101+
LOCALSTORAGE: string;
102+
WEBSQL: string;
103+
INDEXEDDB: string;
104+
105+
/**
106+
* Set and persist localForage options. This must be called before any other calls to localForage are made, but can be called after localForage is loaded.
107+
* If you set any config values with this method they will persist after driver changes, so you can call config() then setDriver()
108+
* @param {LocalForageOptions} options?
109+
*/
110+
config(options: LocalForageOptions): boolean;
111+
config(options: string): any;
112+
config(): LocalForageOptions;
113+
114+
/**
115+
* Create a new instance of localForage to point to a different store.
116+
* All the configuration options used by config are supported.
117+
* @param {LocalForageOptions} options
118+
*/
119+
createInstance(options: LocalForageOptions): LocalForage;
120+
121+
driver(): string;
122+
123+
/**
124+
* Force usage of a particular driver or drivers, if available.
125+
* @param {string} driver
126+
*/
127+
setDriver(
128+
driver: string | string[],
129+
callback?: () => void,
130+
errorCallback?: (error: any) => void
131+
): Promise<void>;
132+
133+
defineDriver(
134+
driver: LocalForageDriver,
135+
callback?: () => void,
136+
errorCallback?: (error: any) => void
137+
): Promise<void>;
138+
139+
/**
140+
* Return a particular driver
141+
* @param {string} driver
142+
*/
143+
getDriver(driver: string): Promise<LocalForageDriver>;
144+
145+
getSerializer(
146+
callback?: (serializer: LocalForageSerializer) => void
147+
): Promise<LocalForageSerializer>;
148+
149+
supports(driverName: string): boolean;
150+
151+
ready(callback?: (error: any) => void): Promise<void>;
152+
}
153+
154+
// Customize
155+
156+
export interface ProxyStorage {
157+
setItem<T>(k: string, v: T, m: number): Promise<T>;
158+
getItem<T>(k: string): Promise<T>;
159+
removeItem(k: string): Promise<void>;
160+
clear(): Promise<void>;
161+
}
162+
163+
export interface ExpiresData<T> {
164+
data: T;
165+
expires: number;
166+
}

0 commit comments

Comments
 (0)