mirror of
https://github.com/stoatchat/javascript-client-sdk.git
synced 2026-07-19 17:13:31 -04:00
0f2cd21df8
Signed-off-by: Chris Hultin <chris.hultin@gmail.com>
51 lines
1.1 KiB
TypeScript
51 lines
1.1 KiB
TypeScript
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);
|
|
}
|
|
}
|
|
}
|