@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.
@@ -1,3 +1,3 @@
1
- [0] Successfully compiled: 3 files with swc (212.03ms)
1
+ [0] Successfully compiled: 3 files with swc (170.42ms)
2
2
  [0] swc --strip-leading-paths src -d dist exited with code 0
3
3
  [1] tsc --project tsconfig.build.json exited with code 0
package/dist/context.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  interface OpenmrsAppContext {
2
- [namespace: string]: unknown;
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 Record<string | symbol | number, unknown> = Record<string | symbol | number, any>>(namespace: string, initialValue?: T): void;
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 Record<string | symbol | number, unknown> = Record<string | symbol | number, any>>(namespace: string): Readonly<T> | null;
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 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;
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 Record<string | symbol | number, unknown> = Record<string | symbol | number, any>>(namespace: string, callback: ContextCallback<T>): () => void;
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.3124",
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.3124",
57
- "@openmrs/esm-state": "6.3.1-pre.3124",
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]: unknown;
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
- T extends Record<string | symbol | number, unknown> = Record<string | symbol | number, any>,
31
- >(namespace: string, initialValue: T = nothing) {
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 Record<string | symbol | number, unknown> = Record<string | symbol | number, any>>(
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
- 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 {
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
- T extends Record<string | symbol | number, unknown> = Record<string | symbol | number, any>,
90
- >(namespace: string, update: (state: T) => T) {
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
- T extends Record<string | symbol | number, unknown> = Record<string | symbol | number, any>,
103
- > = (state: Readonly<T> | null | undefined) => void;
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
- T extends Record<string | symbol | number, unknown> = Record<string | symbol | number, any>,
114
- >(namespace: string, callback: ContextCallback<T>) {
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