-
Notifications
You must be signed in to change notification settings - Fork 332
Expand file tree
/
Copy pathtempStorage.ts
More file actions
43 lines (34 loc) · 1.01 KB
/
tempStorage.ts
File metadata and controls
43 lines (34 loc) · 1.01 KB
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
40
41
42
43
import { Repo } from "./repo";
export const TempStorageItemType = {
tempCode: 1,
} as const;
export type TempStorageItemType = ValueOf<typeof TempStorageItemType>;
export interface TempStorageItem {
key: string;
value: any;
savedAt: number;
type: TempStorageItemType;
}
export const TEMP_ENTRY_MIN_TIME = 60_000;
export class TempStorageDAO extends Repo<TempStorageItem> {
constructor() {
super("tempStorage");
}
save(value: TempStorageItem) {
return super._save(value.key, value);
}
async getValue<T>(key: string) {
return (await super.get(key))?.value as T | undefined;
}
async entries(type?: TempStorageItemType) {
const data = await super.find((key, value) => {
return type ? value.type === type : true;
});
return data;
}
async staleEntries(keeps: Set<string>) {
const now = Date.now();
const entries = await new TempStorageDAO().entries();
return entries.filter((entry) => !keeps.has(entry.key) && now - entry.savedAt > TEMP_ENTRY_MIN_TIME);
}
}