-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
store.ts
52 lines (41 loc) · 938 Bytes
/
store.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
40
41
42
43
44
45
46
47
48
49
50
51
52
import get from "lodash.get";
import set from "lodash.set";
import { Loader } from "./loaders";
interface StoreOptions {
name?: string;
}
type Config = Record<string, unknown>;
export class Store {
readonly options: StoreOptions;
private config: Config = {};
private loaders: Loader[] = [];
constructor(options: StoreOptions = {}) {
this.options = {
name: "default",
...options,
};
}
get<T>(key: string): T {
return get(this.config, key) as T;
}
set(key: string, value: unknown): void {
set(this.config, key, value);
}
registerLoader(loader: Loader): void {
this.loaders.push(loader);
}
assign(config: Config): void {
this.config = {
...this.config,
...config,
};
}
async init(): Promise<void> {
for (const loader of this.loaders) {
await loader.load(this);
}
}
value(): Record<string, unknown> {
return this.config;
}
}