@openmrs/esm-context 6.3.1-pre.3106 → 6.3.1-pre.3124
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +1 -1
- package/dist/context.d.ts +11 -5
- package/package.json +3 -3
- package/src/context.ts +25 -9
package/.turbo/turbo-build.log
CHANGED
package/dist/context.d.ts
CHANGED
|
@@ -14,18 +14,24 @@ export declare const contextStore: import("zustand/vanilla").StoreApi<OpenmrsApp
|
|
|
14
14
|
* @param namespace the namespace to register
|
|
15
15
|
* @param initialValue the initial value of the namespace
|
|
16
16
|
*/
|
|
17
|
-
export declare function registerContext<T extends
|
|
17
|
+
export declare function registerContext<T extends Record<string | symbol | number, unknown> = Record<string | symbol | number, any>>(namespace: string, initialValue?: T): void;
|
|
18
18
|
/**
|
|
19
19
|
* Used by caller to unregister a namespace in the application context. Unregistering a namespace
|
|
20
20
|
* will remove the namespace and all associated data.
|
|
21
21
|
*/
|
|
22
22
|
export declare function unregisterContext(namespace: string): void;
|
|
23
|
-
|
|
23
|
+
/**
|
|
24
|
+
* Returns an _immutable_ version of the state of the namespace as it is currently
|
|
25
|
+
*
|
|
26
|
+
* @typeParam T The type of the value stored in the namespace
|
|
27
|
+
* @param namespace The namespace to load properties from
|
|
28
|
+
*/
|
|
29
|
+
export declare function getContext<T extends Record<string | symbol | number, unknown> = Record<string | symbol | number, any>>(namespace: string): Readonly<T> | null;
|
|
24
30
|
/**
|
|
25
31
|
* Updates a namespace in the global context. If the namespace does not exist, it is registered.
|
|
26
32
|
*/
|
|
27
|
-
export declare function updateContext<T extends
|
|
28
|
-
export type ContextCallback<T extends
|
|
33
|
+
export declare function updateContext<T extends Record<string | symbol | number, unknown> = Record<string | symbol | number, any>>(namespace: string, update: (state: T) => T): void;
|
|
34
|
+
export type ContextCallback<T extends Record<string | symbol | number, unknown> = Record<string | symbol | number, any>> = (state: Readonly<T> | null | undefined) => void;
|
|
29
35
|
/**
|
|
30
36
|
* Subscribes to updates of a given namespace. Note that the returned object is immutable.
|
|
31
37
|
*
|
|
@@ -33,5 +39,5 @@ export type ContextCallback<T extends {} = {}> = (state: Readonly<T> | null | un
|
|
|
33
39
|
* @param callback a function invoked with the current context whenever
|
|
34
40
|
* @returns A function to unsubscribe from the context
|
|
35
41
|
*/
|
|
36
|
-
export declare function subscribeToContext<T extends
|
|
42
|
+
export declare function subscribeToContext<T extends Record<string | symbol | number, unknown> = Record<string | symbol | number, any>>(namespace: string, callback: ContextCallback<T>): () => void;
|
|
37
43
|
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@openmrs/esm-context",
|
|
3
|
-
"version": "6.3.1-pre.
|
|
3
|
+
"version": "6.3.1-pre.3124",
|
|
4
4
|
"license": "MPL-2.0",
|
|
5
5
|
"description": "Utilities for managing the current execution context",
|
|
6
6
|
"type": "module",
|
|
@@ -53,8 +53,8 @@
|
|
|
53
53
|
"@openmrs/esm-state": "6.x"
|
|
54
54
|
},
|
|
55
55
|
"devDependencies": {
|
|
56
|
-
"@openmrs/esm-globals": "6.3.1-pre.
|
|
57
|
-
"@openmrs/esm-state": "6.3.1-pre.
|
|
56
|
+
"@openmrs/esm-globals": "6.3.1-pre.3124",
|
|
57
|
+
"@openmrs/esm-state": "6.3.1-pre.3124",
|
|
58
58
|
"@swc/cli": "^0.7.7",
|
|
59
59
|
"@swc/core": "^1.11.29",
|
|
60
60
|
"concurrently": "^9.1.2",
|
package/src/context.ts
CHANGED
|
@@ -26,7 +26,9 @@ const nothing = Object();
|
|
|
26
26
|
* @param namespace the namespace to register
|
|
27
27
|
* @param initialValue the initial value of the namespace
|
|
28
28
|
*/
|
|
29
|
-
export function registerContext<
|
|
29
|
+
export function registerContext<
|
|
30
|
+
T extends Record<string | symbol | number, unknown> = Record<string | symbol | number, any>,
|
|
31
|
+
>(namespace: string, initialValue: T = nothing) {
|
|
30
32
|
contextStore.setState((state) => {
|
|
31
33
|
if (namespace in state) {
|
|
32
34
|
throw new Error(
|
|
@@ -51,7 +53,15 @@ export function unregisterContext(namespace: string) {
|
|
|
51
53
|
});
|
|
52
54
|
}
|
|
53
55
|
|
|
54
|
-
|
|
56
|
+
/**
|
|
57
|
+
* Returns an _immutable_ version of the state of the namespace as it is currently
|
|
58
|
+
*
|
|
59
|
+
* @typeParam T The type of the value stored in the namespace
|
|
60
|
+
* @param namespace The namespace to load properties from
|
|
61
|
+
*/
|
|
62
|
+
export function getContext<T extends Record<string | symbol | number, unknown> = Record<string | symbol | number, any>>(
|
|
63
|
+
namespace: string,
|
|
64
|
+
): Readonly<T> | null;
|
|
55
65
|
/**
|
|
56
66
|
* Returns an _immutable_ version of the state of the namespace as it is currently
|
|
57
67
|
*
|
|
@@ -60,10 +70,10 @@ export function getContext<T extends {} = {}>(namespace: string): Readonly<T> |
|
|
|
60
70
|
* @param namespace The namespace to load properties from
|
|
61
71
|
* @param selector An optional function which extracts the relevant part of the state
|
|
62
72
|
*/
|
|
63
|
-
export function getContext<
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
): Readonly<U> | null {
|
|
73
|
+
export function getContext<
|
|
74
|
+
T extends Record<string | symbol | number, unknown> = Record<string | symbol | number, any>,
|
|
75
|
+
U extends Record<string | symbol | number, unknown> = T,
|
|
76
|
+
>(namespace: string, selector: (state: Readonly<T>) => U = (state) => state as unknown as U): Readonly<U> | null {
|
|
67
77
|
const state = contextStore.getState();
|
|
68
78
|
if (namespace in state) {
|
|
69
79
|
return Object.freeze(Object.assign({}, (selector ? selector(state[namespace] as T) : state[namespace]) as U));
|
|
@@ -75,7 +85,9 @@ export function getContext<T extends {} = {}, U extends {} = T>(
|
|
|
75
85
|
/**
|
|
76
86
|
* Updates a namespace in the global context. If the namespace does not exist, it is registered.
|
|
77
87
|
*/
|
|
78
|
-
export function updateContext<
|
|
88
|
+
export function updateContext<
|
|
89
|
+
T extends Record<string | symbol | number, unknown> = Record<string | symbol | number, any>,
|
|
90
|
+
>(namespace: string, update: (state: T) => T) {
|
|
79
91
|
contextStore.setState((state) => {
|
|
80
92
|
if (!(namespace in state)) {
|
|
81
93
|
state[namespace] = {};
|
|
@@ -86,7 +98,9 @@ export function updateContext<T extends {} = {}>(namespace: string, update: (sta
|
|
|
86
98
|
});
|
|
87
99
|
}
|
|
88
100
|
|
|
89
|
-
export type ContextCallback<
|
|
101
|
+
export type ContextCallback<
|
|
102
|
+
T extends Record<string | symbol | number, unknown> = Record<string | symbol | number, any>,
|
|
103
|
+
> = (state: Readonly<T> | null | undefined) => void;
|
|
90
104
|
|
|
91
105
|
/**
|
|
92
106
|
* Subscribes to updates of a given namespace. Note that the returned object is immutable.
|
|
@@ -95,7 +109,9 @@ export type ContextCallback<T extends {} = {}> = (state: Readonly<T> | null | un
|
|
|
95
109
|
* @param callback a function invoked with the current context whenever
|
|
96
110
|
* @returns A function to unsubscribe from the context
|
|
97
111
|
*/
|
|
98
|
-
export function subscribeToContext<
|
|
112
|
+
export function subscribeToContext<
|
|
113
|
+
T extends Record<string | symbol | number, unknown> = Record<string | symbol | number, any>,
|
|
114
|
+
>(namespace: string, callback: ContextCallback<T>) {
|
|
99
115
|
let previous = getContext<T>(namespace);
|
|
100
116
|
|
|
101
117
|
// set initial value
|