@openmrs/esm-context 6.3.1-pre.3105 → 6.3.1-pre.3119

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 (206.91ms)
1
+ [0] Successfully compiled: 3 files with swc (223.78ms)
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
@@ -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 {} = {}>(namespace: string, initialValue?: T): void;
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.
@@ -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 {} = {}>(namespace: string): Readonly<T> | null;
29
+ export declare function getContext<T extends Record<string | symbol | number, unknown> = Record<string | symbol | number, any>>(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 {} = {}>(namespace: string, update: (state: T) => T): void;
34
- export type ContextCallback<T extends {} = {}> = (state: Readonly<T> | null | undefined) => void;
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;
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 {} = {}> = (state: Readonly<T> | null | un
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 {} = {}>(namespace: string, callback: ContextCallback<T>): () => void;
42
+ export declare function subscribeToContext<T extends Record<string | symbol | number, unknown> = Record<string | symbol | number, any>>(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.3105",
3
+ "version": "6.3.1-pre.3119",
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.3105",
57
- "@openmrs/esm-state": "6.3.1-pre.3105",
56
+ "@openmrs/esm-globals": "6.3.1-pre.3119",
57
+ "@openmrs/esm-state": "6.3.1-pre.3119",
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<T extends {} = {}>(namespace: string, initialValue: T = nothing) {
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(
@@ -57,7 +59,9 @@ export function unregisterContext(namespace: string) {
57
59
  * @typeParam T The type of the value stored in the namespace
58
60
  * @param namespace The namespace to load properties from
59
61
  */
60
- export function getContext<T extends {} = {}>(namespace: string): Readonly<T> | null;
62
+ export function getContext<T extends Record<string | symbol | number, unknown> = Record<string | symbol | number, any>>(
63
+ namespace: string,
64
+ ): Readonly<T> | null;
61
65
  /**
62
66
  * Returns an _immutable_ version of the state of the namespace as it is currently
63
67
  *
@@ -66,10 +70,10 @@ export function getContext<T extends {} = {}>(namespace: string): Readonly<T> |
66
70
  * @param namespace The namespace to load properties from
67
71
  * @param selector An optional function which extracts the relevant part of the state
68
72
  */
69
- export function getContext<T extends {} = {}, U extends {} = T>(
70
- namespace: string,
71
- selector: (state: Readonly<T>) => U = (state) => state as unknown as U,
72
- ): 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 {
73
77
  const state = contextStore.getState();
74
78
  if (namespace in state) {
75
79
  return Object.freeze(Object.assign({}, (selector ? selector(state[namespace] as T) : state[namespace]) as U));
@@ -81,7 +85,9 @@ export function getContext<T extends {} = {}, U extends {} = T>(
81
85
  /**
82
86
  * Updates a namespace in the global context. If the namespace does not exist, it is registered.
83
87
  */
84
- export function updateContext<T extends {} = {}>(namespace: string, update: (state: T) => T) {
88
+ export function updateContext<
89
+ T extends Record<string | symbol | number, unknown> = Record<string | symbol | number, any>,
90
+ >(namespace: string, update: (state: T) => T) {
85
91
  contextStore.setState((state) => {
86
92
  if (!(namespace in state)) {
87
93
  state[namespace] = {};
@@ -92,7 +98,9 @@ export function updateContext<T extends {} = {}>(namespace: string, update: (sta
92
98
  });
93
99
  }
94
100
 
95
- export type ContextCallback<T extends {} = {}> = (state: Readonly<T> | null | undefined) => void;
101
+ export type ContextCallback<
102
+ T extends Record<string | symbol | number, unknown> = Record<string | symbol | number, any>,
103
+ > = (state: Readonly<T> | null | undefined) => void;
96
104
 
97
105
  /**
98
106
  * Subscribes to updates of a given namespace. Note that the returned object is immutable.
@@ -101,7 +109,9 @@ export type ContextCallback<T extends {} = {}> = (state: Readonly<T> | null | un
101
109
  * @param callback a function invoked with the current context whenever
102
110
  * @returns A function to unsubscribe from the context
103
111
  */
104
- export function subscribeToContext<T extends {} = {}>(namespace: string, callback: ContextCallback<T>) {
112
+ export function subscribeToContext<
113
+ T extends Record<string | symbol | number, unknown> = Record<string | symbol | number, any>,
114
+ >(namespace: string, callback: ContextCallback<T>) {
105
115
  let previous = getContext<T>(namespace);
106
116
 
107
117
  // set initial value