@openmrs/esm-context 6.3.1-pre.3124 → 6.3.1-pre.3133
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 +6 -6
- package/package.json +3 -3
- package/src/context.ts +21 -20
package/.turbo/turbo-build.log
CHANGED
package/dist/context.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
interface OpenmrsAppContext {
|
|
2
|
-
[namespace: string]:
|
|
2
|
+
[namespace: string]: NonNullable<object>;
|
|
3
3
|
}
|
|
4
4
|
/**
|
|
5
5
|
* @internal
|
|
@@ -14,7 +14,7 @@ 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 NonNullable<object> = NonNullable<object>>(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.
|
|
@@ -26,12 +26,12 @@ export declare function unregisterContext(namespace: string): void;
|
|
|
26
26
|
* @typeParam T The type of the value stored in the namespace
|
|
27
27
|
* @param namespace The namespace to load properties from
|
|
28
28
|
*/
|
|
29
|
-
export declare function getContext<T extends
|
|
29
|
+
export declare function getContext<T extends NonNullable<object> = NonNullable<object>>(namespace: string): Readonly<T> | null;
|
|
30
30
|
/**
|
|
31
31
|
* Updates a namespace in the global context. If the namespace does not exist, it is registered.
|
|
32
32
|
*/
|
|
33
|
-
export declare function updateContext<T extends
|
|
34
|
-
export type ContextCallback<T extends
|
|
33
|
+
export declare function updateContext<T extends NonNullable<object> = NonNullable<object>>(namespace: string, update: (state: T) => T): void;
|
|
34
|
+
export type ContextCallback<T extends NonNullable<object> = NonNullable<object>> = (state: Readonly<T> | null | undefined) => void;
|
|
35
35
|
/**
|
|
36
36
|
* Subscribes to updates of a given namespace. Note that the returned object is immutable.
|
|
37
37
|
*
|
|
@@ -39,5 +39,5 @@ export type ContextCallback<T extends Record<string | symbol | number, unknown>
|
|
|
39
39
|
* @param callback a function invoked with the current context whenever
|
|
40
40
|
* @returns A function to unsubscribe from the context
|
|
41
41
|
*/
|
|
42
|
-
export declare function subscribeToContext<T extends
|
|
42
|
+
export declare function subscribeToContext<T extends NonNullable<object> = NonNullable<object>>(namespace: string, callback: ContextCallback<T>): () => void;
|
|
43
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.3133",
|
|
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.3133",
|
|
57
|
+
"@openmrs/esm-state": "6.3.1-pre.3133",
|
|
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
|
@@ -5,7 +5,7 @@ import { createStore } from 'zustand/vanilla';
|
|
|
5
5
|
import { registerGlobalStore } from '@openmrs/esm-state';
|
|
6
6
|
|
|
7
7
|
interface OpenmrsAppContext {
|
|
8
|
-
[namespace: string]:
|
|
8
|
+
[namespace: string]: NonNullable<object>;
|
|
9
9
|
}
|
|
10
10
|
|
|
11
11
|
/**
|
|
@@ -26,9 +26,10 @@ 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<
|
|
30
|
-
|
|
31
|
-
|
|
29
|
+
export function registerContext<T extends NonNullable<object> = NonNullable<object>>(
|
|
30
|
+
namespace: string,
|
|
31
|
+
initialValue: T = nothing,
|
|
32
|
+
) {
|
|
32
33
|
contextStore.setState((state) => {
|
|
33
34
|
if (namespace in state) {
|
|
34
35
|
throw new Error(
|
|
@@ -59,9 +60,7 @@ export function unregisterContext(namespace: string) {
|
|
|
59
60
|
* @typeParam T The type of the value stored in the namespace
|
|
60
61
|
* @param namespace The namespace to load properties from
|
|
61
62
|
*/
|
|
62
|
-
export function getContext<T extends
|
|
63
|
-
namespace: string,
|
|
64
|
-
): Readonly<T> | null;
|
|
63
|
+
export function getContext<T extends NonNullable<object> = NonNullable<object>>(namespace: string): Readonly<T> | null;
|
|
65
64
|
/**
|
|
66
65
|
* Returns an _immutable_ version of the state of the namespace as it is currently
|
|
67
66
|
*
|
|
@@ -70,10 +69,10 @@ export function getContext<T extends Record<string | symbol | number, unknown> =
|
|
|
70
69
|
* @param namespace The namespace to load properties from
|
|
71
70
|
* @param selector An optional function which extracts the relevant part of the state
|
|
72
71
|
*/
|
|
73
|
-
export function getContext<
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
72
|
+
export function getContext<T extends NonNullable<object> = NonNullable<object>, U extends NonNullable<object> = T>(
|
|
73
|
+
namespace: string,
|
|
74
|
+
selector: (state: Readonly<T>) => U = (state) => state as unknown as U,
|
|
75
|
+
): Readonly<U> | null {
|
|
77
76
|
const state = contextStore.getState();
|
|
78
77
|
if (namespace in state) {
|
|
79
78
|
return Object.freeze(Object.assign({}, (selector ? selector(state[namespace] as T) : state[namespace]) as U));
|
|
@@ -85,9 +84,10 @@ export function getContext<
|
|
|
85
84
|
/**
|
|
86
85
|
* Updates a namespace in the global context. If the namespace does not exist, it is registered.
|
|
87
86
|
*/
|
|
88
|
-
export function updateContext<
|
|
89
|
-
|
|
90
|
-
|
|
87
|
+
export function updateContext<T extends NonNullable<object> = NonNullable<object>>(
|
|
88
|
+
namespace: string,
|
|
89
|
+
update: (state: T) => T,
|
|
90
|
+
) {
|
|
91
91
|
contextStore.setState((state) => {
|
|
92
92
|
if (!(namespace in state)) {
|
|
93
93
|
state[namespace] = {};
|
|
@@ -98,9 +98,9 @@ export function updateContext<
|
|
|
98
98
|
});
|
|
99
99
|
}
|
|
100
100
|
|
|
101
|
-
export type ContextCallback<
|
|
102
|
-
|
|
103
|
-
|
|
101
|
+
export type ContextCallback<T extends NonNullable<object> = NonNullable<object>> = (
|
|
102
|
+
state: Readonly<T> | null | undefined,
|
|
103
|
+
) => void;
|
|
104
104
|
|
|
105
105
|
/**
|
|
106
106
|
* Subscribes to updates of a given namespace. Note that the returned object is immutable.
|
|
@@ -109,9 +109,10 @@ export type ContextCallback<
|
|
|
109
109
|
* @param callback a function invoked with the current context whenever
|
|
110
110
|
* @returns A function to unsubscribe from the context
|
|
111
111
|
*/
|
|
112
|
-
export function subscribeToContext<
|
|
113
|
-
|
|
114
|
-
|
|
112
|
+
export function subscribeToContext<T extends NonNullable<object> = NonNullable<object>>(
|
|
113
|
+
namespace: string,
|
|
114
|
+
callback: ContextCallback<T>,
|
|
115
|
+
) {
|
|
115
116
|
let previous = getContext<T>(namespace);
|
|
116
117
|
|
|
117
118
|
// set initial value
|