forked from openmrs/openmrs-esm-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmock.ts
More file actions
24 lines (18 loc) · 703 Bytes
/
mock.ts
File metadata and controls
24 lines (18 loc) · 703 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
const appContext = {};
const nothing = Object();
export function registerContext<T extends {} = {}>(namespace: string, initialValue: T = nothing) {
appContext[namespace] = initialValue ?? {};
}
export function getContext<T extends {} = {}, U extends {} = T>(
namespace: string,
selector: (state: Readonly<T>) => U = (state) => state as unknown as U,
): Readonly<U> | null {
const value = appContext[namespace];
if (!value) {
return null;
}
return Object.freeze(Object.assign({}, selector ? selector(value) : value));
}
export function updateContext<T extends {} = {}>(namespace: string, update: (state: T) => T) {
appContext[namespace] = update(appContext[namespace] ?? {});
}