-
Notifications
You must be signed in to change notification settings - Fork 0
/
signalWithStorage.ts
39 lines (35 loc) · 1023 Bytes
/
signalWithStorage.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { signalWithStorageCustom, type Storage } from "../../src/index.ts";
import { existsSync } from "@std/fs";
const STORE_DIR = "./store";
const fsStorage: Storage = {
getItem: (key: string) => {
ensureStorage();
const filePath = `${STORE_DIR}/${key}`;
if (existsSync(filePath)) {
return Deno.readTextFileSync(filePath);
}
return null;
},
setItem: (key: string, value: string) => {
ensureStorage();
const filePath = `${STORE_DIR}/${key}`;
Deno.writeTextFileSync(filePath, value);
},
removeItem: (key: string) => {
ensureStorage();
const filePath = `${STORE_DIR}/${key}`;
Deno.removeSync(filePath);
},
clear: () => {
try {
Deno.removeSync(STORE_DIR, { recursive: true });
} catch (e) {}
ensureStorage();
},
};
function ensureStorage() {
if (existsSync(STORE_DIR)) return;
Deno.mkdirSync(STORE_DIR);
}
export const signalWithStorage = <T>(key: string, initialValue: T) =>
signalWithStorageCustom(key, initialValue, fsStorage);