@openmrs/esm-state 9.0.3-pre.4216 → 9.0.3-pre.4223

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: 4 files with swc (119.62ms)
1
+ [0] Successfully compiled: 4 files with swc (84.64ms)
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/state.d.ts CHANGED
@@ -1,13 +1,15 @@
1
1
  import type { StoreApi } from 'zustand/vanilla';
2
+ type StorageType = 'none' | 'sessionStorage';
2
3
  /**
3
4
  * Creates a Zustand store.
4
5
  *
5
6
  * @param name A name by which the store can be looked up later.
6
7
  * Must be unique across the entire application.
8
+ * @param storageType The type of storage to use for persisting the store's state. Defaults to 'none'.
7
9
  * @param initialState An object which will be the initial state of the store.
8
10
  * @returns The newly created store.
9
11
  */
10
- export declare function createGlobalStore<T>(name: string, initialState: T): StoreApi<T>;
12
+ export declare function createGlobalStore<T>(name: string, initialState: T, storageType?: StorageType): StoreApi<T>;
11
13
  /**
12
14
  * Registers an existing Zustand store.
13
15
  *
@@ -23,9 +25,10 @@ export declare function registerGlobalStore<T>(name: string, store: StoreApi<T>)
23
25
  *
24
26
  * @param name The name of the store to look up.
25
27
  * @param fallbackState The initial value of the new store if no store named `name` exists.
28
+ * @param fallbackStorageType The type of storage to use for the new store if no store named `name` exists. Defaults to 'none'.
26
29
  * @returns The found or newly created store.
27
30
  */
28
- export declare function getGlobalStore<T>(name: string, fallbackState?: T): StoreApi<T>;
31
+ export declare function getGlobalStore<T>(name: string, fallbackState?: T, fallbackStorageType?: StorageType): StoreApi<T>;
29
32
  /**
30
33
  * Subscribes to a store and invokes a callback when the state changes.
31
34
  * The callback is also immediately invoked with the current state upon subscription.
@@ -42,4 +45,5 @@ export declare function getGlobalStore<T>(name: string, fallbackState?: T): Stor
42
45
  */
43
46
  export declare function subscribeTo<T, U = T>(store: StoreApi<T>, handle: (state: T) => void): () => void;
44
47
  export declare function subscribeTo<T, U>(store: StoreApi<T>, select: (state: T) => U, handle: (subState: U) => void): () => void;
48
+ export {};
45
49
  //# sourceMappingURL=state.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"state.d.ts","sourceRoot":"","sources":["../src/state.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAmBhD;;;;;;;GAOG;AACH,wBAAgB,iBAAiB,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,YAAY,EAAE,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,CAwB/E;AAED;;;;;;;GAOG;AACH,wBAAgB,mBAAmB,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,QAAQ,CAAC,CAAC,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,CAsBpF;AAED;;;;;;;GAOG;AACH,wBAAgB,cAAc,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,aAAa,CAAC,EAAE,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,CAa9E;AAID;;;;;;;;;;;;;GAaG;AACH,wBAAgB,WAAW,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,KAAK,EAAE,QAAQ,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,IAAI,GAAG,MAAM,IAAI,CAAC;AAClG,wBAAgB,WAAW,CAAC,CAAC,EAAE,CAAC,EAC9B,KAAK,EAAE,QAAQ,CAAC,CAAC,CAAC,EAClB,MAAM,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,CAAC,EACvB,MAAM,EAAE,CAAC,QAAQ,EAAE,CAAC,KAAK,IAAI,GAC5B,MAAM,IAAI,CAAC"}
1
+ {"version":3,"file":"state.d.ts","sourceRoot":"","sources":["../src/state.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAmBhD,KAAK,WAAW,GAAG,MAAM,GAAG,gBAAgB,CAAC;AAE7C;;;;;;;;GAQG;AACH,wBAAgB,iBAAiB,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,YAAY,EAAE,CAAC,EAAE,WAAW,GAAE,WAAoB,GAAG,QAAQ,CAAC,CAAC,CAAC,CAwBlH;AAED;;;;;;;GAOG;AACH,wBAAgB,mBAAmB,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,QAAQ,CAAC,CAAC,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,CAsBpF;AAED;;;;;;;;GAQG;AACH,wBAAgB,cAAc,CAAC,CAAC,EAC9B,IAAI,EAAE,MAAM,EACZ,aAAa,CAAC,EAAE,CAAC,EACjB,mBAAmB,GAAE,WAAoB,GACxC,QAAQ,CAAC,CAAC,CAAC,CAab;AAID;;;;;;;;;;;;;GAaG;AACH,wBAAgB,WAAW,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,KAAK,EAAE,QAAQ,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,IAAI,GAAG,MAAM,IAAI,CAAC;AAClG,wBAAgB,WAAW,CAAC,CAAC,EAAE,CAAC,EAC9B,KAAK,EAAE,QAAQ,CAAC,CAAC,CAAC,EAClB,MAAM,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,CAAC,EACvB,MAAM,EAAE,CAAC,QAAQ,EAAE,CAAC,KAAK,IAAI,GAC5B,MAAM,IAAI,CAAC"}
package/dist/state.js CHANGED
@@ -1,4 +1,5 @@
1
1
  /** @module @category Store */ import { shallowEqual } from "@openmrs/esm-utils";
2
+ import { persist, createJSONStorage } from "zustand/middleware";
2
3
  import { createStore } from "zustand/vanilla";
3
4
  import { isTestEnvironment } from "./utils.js";
4
5
  const availableStores = {};
@@ -14,9 +15,10 @@ globalThis.setTimeout?.(()=>{
14
15
  *
15
16
  * @param name A name by which the store can be looked up later.
16
17
  * Must be unique across the entire application.
18
+ * @param storageType The type of storage to use for persisting the store's state. Defaults to 'none'.
17
19
  * @param initialState An object which will be the initial state of the store.
18
20
  * @returns The newly created store.
19
- */ export function createGlobalStore(name, initialState) {
21
+ */ export function createGlobalStore(name, initialState, storageType = 'none') {
20
22
  const available = availableStores[name];
21
23
  if (available) {
22
24
  if (available.active) {
@@ -29,7 +31,7 @@ globalThis.setTimeout?.(()=>{
29
31
  available.active = true;
30
32
  return available.value;
31
33
  } else {
32
- const store = createStore()(()=>initialState);
34
+ const store = createStoreHelper(name, initialState, storageType);
33
35
  availableStores[name] = {
34
36
  value: store,
35
37
  active: true
@@ -70,11 +72,12 @@ globalThis.setTimeout?.(()=>{
70
72
  *
71
73
  * @param name The name of the store to look up.
72
74
  * @param fallbackState The initial value of the new store if no store named `name` exists.
75
+ * @param fallbackStorageType The type of storage to use for the new store if no store named `name` exists. Defaults to 'none'.
73
76
  * @returns The found or newly created store.
74
- */ export function getGlobalStore(name, fallbackState) {
77
+ */ export function getGlobalStore(name, fallbackState, fallbackStorageType = 'none') {
75
78
  const available = availableStores[name];
76
79
  if (!available) {
77
- const store = createStore()(()=>fallbackState ?? {});
80
+ const store = createStoreHelper(name, fallbackState ?? {}, fallbackStorageType);
78
81
  availableStores[name] = {
79
82
  value: store,
80
83
  active: false
@@ -97,3 +100,17 @@ export function subscribeTo(...args) {
97
100
  }
98
101
  });
99
102
  }
103
+ /**
104
+ * @internal
105
+ */ function createStoreHelper(name, initialState, storageType) {
106
+ let store;
107
+ if (storageType === 'sessionStorage') {
108
+ store = createStore()(persist(()=>initialState, {
109
+ name,
110
+ storage: createJSONStorage(()=>sessionStorage)
111
+ }));
112
+ } else {
113
+ store = createStore()(()=>initialState);
114
+ }
115
+ return store;
116
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openmrs/esm-state",
3
- "version": "9.0.3-pre.4216",
3
+ "version": "9.0.3-pre.4223",
4
4
  "license": "MPL-2.0",
5
5
  "description": "Frontend stores & state management for OpenMRS",
6
6
  "type": "module",
@@ -61,8 +61,8 @@
61
61
  "@openmrs/esm-utils": "9.x"
62
62
  },
63
63
  "devDependencies": {
64
- "@openmrs/esm-globals": "9.0.3-pre.4216",
65
- "@openmrs/esm-utils": "9.0.3-pre.4216",
64
+ "@openmrs/esm-globals": "9.0.3-pre.4223",
65
+ "@openmrs/esm-utils": "9.0.3-pre.4223",
66
66
  "@swc/cli": "0.8.0",
67
67
  "@swc/core": "1.15.18",
68
68
  "@vitest/coverage-v8": "^4.0.18",
package/src/state.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  /** @module @category Store */
2
2
  import type {} from '@openmrs/esm-globals';
3
3
  import { shallowEqual } from '@openmrs/esm-utils';
4
+ import { persist, createJSONStorage } from 'zustand/middleware';
4
5
  import type { StoreApi } from 'zustand/vanilla';
5
6
  import { createStore } from 'zustand/vanilla';
6
7
  import { isTestEnvironment } from './utils';
@@ -20,15 +21,18 @@ globalThis.setTimeout?.(() => {
20
21
  }
21
22
  }, 1000);
22
23
 
24
+ type StorageType = 'none' | 'sessionStorage';
25
+
23
26
  /**
24
27
  * Creates a Zustand store.
25
28
  *
26
29
  * @param name A name by which the store can be looked up later.
27
30
  * Must be unique across the entire application.
31
+ * @param storageType The type of storage to use for persisting the store's state. Defaults to 'none'.
28
32
  * @param initialState An object which will be the initial state of the store.
29
33
  * @returns The newly created store.
30
34
  */
31
- export function createGlobalStore<T>(name: string, initialState: T): StoreApi<T> {
35
+ export function createGlobalStore<T>(name: string, initialState: T, storageType: StorageType = 'none'): StoreApi<T> {
32
36
  const available = availableStores[name];
33
37
 
34
38
  if (available) {
@@ -43,7 +47,7 @@ export function createGlobalStore<T>(name: string, initialState: T): StoreApi<T>
43
47
  available.active = true;
44
48
  return available.value as StoreApi<T>;
45
49
  } else {
46
- const store = createStore<T>()(() => initialState);
50
+ const store = createStoreHelper(name, initialState, storageType);
47
51
 
48
52
  availableStores[name] = {
49
53
  value: store,
@@ -92,13 +96,18 @@ export function registerGlobalStore<T>(name: string, store: StoreApi<T>): StoreA
92
96
  *
93
97
  * @param name The name of the store to look up.
94
98
  * @param fallbackState The initial value of the new store if no store named `name` exists.
99
+ * @param fallbackStorageType The type of storage to use for the new store if no store named `name` exists. Defaults to 'none'.
95
100
  * @returns The found or newly created store.
96
101
  */
97
- export function getGlobalStore<T>(name: string, fallbackState?: T): StoreApi<T> {
102
+ export function getGlobalStore<T>(
103
+ name: string,
104
+ fallbackState?: T,
105
+ fallbackStorageType: StorageType = 'none',
106
+ ): StoreApi<T> {
98
107
  const available = availableStores[name];
99
108
 
100
109
  if (!available) {
101
- const store = createStore<T>()(() => fallbackState ?? ({} as unknown as T));
110
+ const store = createStoreHelper(name, fallbackState ?? ({} as unknown as T), fallbackStorageType);
102
111
  availableStores[name] = {
103
112
  value: store,
104
113
  active: false,
@@ -147,3 +156,16 @@ export function subscribeTo<T, U>(...args: SubscribeToArgs<T, U>): () => void {
147
156
  }
148
157
  });
149
158
  }
159
+
160
+ /**
161
+ * @internal
162
+ */
163
+ function createStoreHelper<T>(name: string, initialState: T, storageType: StorageType) {
164
+ let store: StoreApi<T>;
165
+ if (storageType === 'sessionStorage') {
166
+ store = createStore<T>()(persist(() => initialState, { name, storage: createJSONStorage(() => sessionStorage) }));
167
+ } else {
168
+ store = createStore<T>()(() => initialState);
169
+ }
170
+ return store;
171
+ }