forked from stoatchat/javascript-client-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectStorage.ts
More file actions
50 lines (45 loc) · 1.05 KB
/
Copy pathObjectStorage.ts
File metadata and controls
50 lines (45 loc) · 1.05 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
44
45
46
47
48
49
50
import type { SetStoreFunction } from "solid-js/store";
import { createStore } from "solid-js/store";
import { type Hydrators, hydrate } from "../hydration/index.js";
/**
* Wrapper around Solid.js store
*/
export class ObjectStorage<T> {
private store: Record<string, T>;
readonly set: SetStoreFunction<Record<string, T>>;
/**
* Create new object storage
*/
constructor() {
const [store, setStore] = createStore({});
this.store = store as never;
this.set = setStore;
this.get = this.get.bind(this);
}
/**
* Get object by ID
* @param id ID
* @returns Object
*/
get(id: string): T | undefined {
return this.store[id];
}
/**
* Hydrate some data into storage
* @param id ID
* @param type Hydration type
* @param context Context
* @param data Input Data
*/
hydrate(
id: string,
type: keyof Hydrators,
context: unknown,
data?: unknown
): void {
if (data) {
data = { partial: false, ...data };
this.set(id, hydrate(type, data as never, context, true) as T);
}
}
}