@pushwoosh/spa-context 1.1.3 → 1.1.4
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/index.d.ts +2 -1
- package/index.js +2 -1
- package/package.json +1 -1
- package/useMemoryKey.d.ts +3 -0
- package/useMemoryKey.js +23 -0
package/index.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { SimpleEventEmitter } from './SimpleEventEmitter';
|
|
2
2
|
import type { SPAContextType } from './types';
|
|
3
3
|
import { UniversalKeyValueStore } from './UniversalKeyValueStore';
|
|
4
|
+
import { useMemoryKey } from './useMemoryKey';
|
|
4
5
|
import { useSession } from './useSession';
|
|
5
6
|
import { useSPAContext } from './useSPAContext';
|
|
6
7
|
export type { SPAContextType };
|
|
7
|
-
export { useSPAContext, useSession, SimpleEventEmitter, UniversalKeyValueStore, };
|
|
8
|
+
export { useSPAContext, useSession, useMemoryKey, SimpleEventEmitter, UniversalKeyValueStore, };
|
|
8
9
|
export declare const SPAContextProvider: import("react").Provider<SPAContextType | undefined>;
|
package/index.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { SPAContext } from './context';
|
|
2
2
|
import { SimpleEventEmitter } from './SimpleEventEmitter';
|
|
3
3
|
import { UniversalKeyValueStore } from './UniversalKeyValueStore';
|
|
4
|
+
import { useMemoryKey } from './useMemoryKey';
|
|
4
5
|
import { useSession } from './useSession';
|
|
5
6
|
import { useSPAContext } from './useSPAContext';
|
|
6
|
-
export { useSPAContext, useSession, SimpleEventEmitter, UniversalKeyValueStore };
|
|
7
|
+
export { useSPAContext, useSession, useMemoryKey, SimpleEventEmitter, UniversalKeyValueStore };
|
|
7
8
|
export const SPAContextProvider = SPAContext.Provider;
|
package/package.json
CHANGED
package/useMemoryKey.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { useCallback, useEffect, useState } from 'react';
|
|
2
|
+
import { useSPAContext } from './useSPAContext';
|
|
3
|
+
function resolveInit(init) {
|
|
4
|
+
return typeof init === 'function' ? init() : init;
|
|
5
|
+
}
|
|
6
|
+
export function useMemoryKey(key, init) {
|
|
7
|
+
const {
|
|
8
|
+
keyValueStorage
|
|
9
|
+
} = useSPAContext();
|
|
10
|
+
const [state, setState] = useState(() => {
|
|
11
|
+
const stored = keyValueStorage.getMemory(key);
|
|
12
|
+
return stored !== undefined ? stored : resolveInit(init);
|
|
13
|
+
});
|
|
14
|
+
useEffect(() => {
|
|
15
|
+
return keyValueStorage.subscribe(key, change => {
|
|
16
|
+
setState(change.value !== undefined ? change.value : resolveInit(init));
|
|
17
|
+
});
|
|
18
|
+
}, [keyValueStorage, key, init]);
|
|
19
|
+
const setValue = useCallback(value => {
|
|
20
|
+
keyValueStorage.set(key, value);
|
|
21
|
+
}, [keyValueStorage, key]);
|
|
22
|
+
return [state, setValue];
|
|
23
|
+
}
|