@pushwoosh/spa-context 1.1.8 → 1.2.1
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/helpers.d.ts +2 -0
- package/helpers.js +3 -0
- package/index.d.ts +2 -1
- package/index.js +2 -1
- package/package.json +1 -1
- package/useEventValue.d.ts +1 -0
- package/useEventValue.js +20 -0
- package/useMemoryKey.d.ts +1 -2
- package/useMemoryKey.js +1 -3
package/helpers.d.ts
ADDED
package/helpers.js
ADDED
package/index.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { SimpleEventEmitter } from './SimpleEventEmitter';
|
|
2
2
|
import type { SPAContextType } from './types';
|
|
3
3
|
import { UniversalKeyValueStore } from './UniversalKeyValueStore';
|
|
4
|
+
import { useEventValue } from './useEventValue';
|
|
4
5
|
import { useMemoryKey, useMemoryKeySlim } from './useMemoryKey';
|
|
5
6
|
import { useSession } from './useSession';
|
|
6
7
|
import { useSPAContext } from './useSPAContext';
|
|
7
8
|
export type { SPAContextType };
|
|
8
|
-
export { useSPAContext, useSession, useMemoryKey, useMemoryKeySlim, SimpleEventEmitter, UniversalKeyValueStore, };
|
|
9
|
+
export { useSPAContext, useSession, useEventValue, useMemoryKey, useMemoryKeySlim, SimpleEventEmitter, UniversalKeyValueStore, };
|
|
9
10
|
export declare const SPAContextProvider: import("react").Provider<SPAContextType | undefined>;
|
package/index.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { SPAContext } from './context';
|
|
2
2
|
import { SimpleEventEmitter } from './SimpleEventEmitter';
|
|
3
3
|
import { UniversalKeyValueStore } from './UniversalKeyValueStore';
|
|
4
|
+
import { useEventValue } from './useEventValue';
|
|
4
5
|
import { useMemoryKey, useMemoryKeySlim } from './useMemoryKey';
|
|
5
6
|
import { useSession } from './useSession';
|
|
6
7
|
import { useSPAContext } from './useSPAContext';
|
|
7
|
-
export { useSPAContext, useSession, useMemoryKey, useMemoryKeySlim, SimpleEventEmitter, UniversalKeyValueStore };
|
|
8
|
+
export { useSPAContext, useSession, useEventValue, useMemoryKey, useMemoryKeySlim, SimpleEventEmitter, UniversalKeyValueStore };
|
|
8
9
|
export const SPAContextProvider = SPAContext.Provider;
|
package/package.json
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function useEventValue<V = unknown>(key: string, init?: V): [V | undefined, (value: V) => void];
|
package/useEventValue.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { useCallback, useEffect, useState } from 'react';
|
|
2
|
+
import { useSPAContext } from './useSPAContext';
|
|
3
|
+
export function useEventValue(key, init) {
|
|
4
|
+
const {
|
|
5
|
+
eventBus
|
|
6
|
+
} = useSPAContext();
|
|
7
|
+
const [state, setState] = useState(() => {
|
|
8
|
+
return eventBus.getLastPayload(key) ?? init;
|
|
9
|
+
});
|
|
10
|
+
useEffect(() => {
|
|
11
|
+
setState(eventBus.getLastPayload(key) ?? init);
|
|
12
|
+
return eventBus.on(key, change => {
|
|
13
|
+
setState(change);
|
|
14
|
+
}, true);
|
|
15
|
+
}, [eventBus, key, init]);
|
|
16
|
+
const emit = useCallback(value => {
|
|
17
|
+
eventBus.emit(key, value);
|
|
18
|
+
}, [eventBus, key]);
|
|
19
|
+
return [state, emit];
|
|
20
|
+
}
|
package/useMemoryKey.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
+
import { type Initializer } from './helpers';
|
|
1
2
|
import { type SPAContextType } from './types';
|
|
2
|
-
type Initializer<V> = V | (() => V);
|
|
3
3
|
export declare function useMemoryKeySlim<V = unknown>(spaContext: SPAContextType, key: IDBValidKey, init: Initializer<V>): [V, (value: V) => void];
|
|
4
4
|
export declare function useMemoryKey<V = unknown>(key: IDBValidKey, init: Initializer<V>): [V, (value: V) => void];
|
|
5
|
-
export {};
|
package/useMemoryKey.js
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import { useCallback, useEffect, useState } from 'react';
|
|
2
|
+
import { resolveInit } from './helpers';
|
|
2
3
|
import { useSPAContext } from './useSPAContext';
|
|
3
|
-
function resolveInit(init) {
|
|
4
|
-
return typeof init === 'function' ? init() : init;
|
|
5
|
-
}
|
|
6
4
|
export function useMemoryKeySlim(spaContext, key, init) {
|
|
7
5
|
const {
|
|
8
6
|
keyValueStorage
|