@pushwoosh/spa-context 1.1.7 → 1.2.0

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 ADDED
@@ -0,0 +1,2 @@
1
+ export type Initializer<V> = V | (() => V);
2
+ export declare function resolveInit<V>(init: Initializer<V>): V;
package/helpers.js ADDED
@@ -0,0 +1,3 @@
1
+ export function resolveInit(init) {
2
+ return typeof init === 'function' ? init() : init;
3
+ }
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
@@ -23,11 +23,13 @@
23
23
  "@pushwoosh/frontend-builder-engine": "^2.0.4",
24
24
  "@pushwoosh/http-client": "^2.5.0",
25
25
  "@pushwoosh/session-store": "^2.5.0",
26
+ "@types/history": "^4.7.11",
26
27
  "@types/react": "^18.3.27",
27
28
  "pre-commit": "^1.2.2"
28
29
  },
29
30
  "peerDependencies": {
30
31
  "@pushwoosh/http-client": "^2.5.0",
32
+ "@pushwoosh/rpc-gateway-ai-assistant": "^0.1.101",
31
33
  "@pushwoosh/rpc-gateway-journey-data": "^0.26.206",
32
34
  "@pushwoosh/rpc-gateway-payment-data": "^0.2.135",
33
35
  "@pushwoosh/rpc-gateway-subscription-form": "^0.1.0",
@@ -35,5 +37,5 @@
35
37
  "@pushwoosh/session-store": "^2.5.0",
36
38
  "react": "^16.14.0 || ^18.0.0"
37
39
  },
38
- "version": "1.1.7"
40
+ "version": "1.2.0"
39
41
  }
package/types.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { type History } from 'history';
2
2
  import type { HttpClient, RpcClient } from '@pushwoosh/http-client';
3
+ import type { API as RpcGatewayAIAssistant } from '@pushwoosh/rpc-gateway-ai-assistant';
3
4
  import type { API as RpcGatewayJourneyAPI } from '@pushwoosh/rpc-gateway-journey-data';
4
5
  import type { API as RpcGatewayPaymentAPI } from '@pushwoosh/rpc-gateway-payment-data';
5
6
  import type { API as RpcGatewaySubscriptionFormAPI } from '@pushwoosh/rpc-gateway-subscription-form';
@@ -21,6 +22,7 @@ export type SPAContextType = {
21
22
  readonly rpcClient: RpcClient;
22
23
  readonly sessionStore: SessionStore;
23
24
  readonly rpcGateway: RpcGatewayAPI;
25
+ readonly rpcGatewayAIAssistant: RpcGatewayAIAssistant;
24
26
  readonly rpcGatewayJourney: RpcGatewayJourneyAPI;
25
27
  readonly rpcGatewayPayment: RpcGatewayPaymentAPI;
26
28
  readonly rpcGatewaySubscriptionForm: RpcGatewaySubscriptionFormAPI;
@@ -0,0 +1,2 @@
1
+ import { type Initializer } from './helpers';
2
+ export declare function useEventValue<V = unknown>(key: string, init: Initializer<V>): [V, (value: V) => void];
@@ -0,0 +1,21 @@
1
+ import { useCallback, useEffect, useState } from 'react';
2
+ import { resolveInit } from './helpers';
3
+ import { useSPAContext } from './useSPAContext';
4
+ export function useEventValue(key, init) {
5
+ const {
6
+ eventBus
7
+ } = useSPAContext();
8
+ const [state, setState] = useState(() => {
9
+ const stored = eventBus.getLastPayload(key);
10
+ return stored !== undefined ? stored : resolveInit(init);
11
+ });
12
+ useEffect(() => {
13
+ return eventBus.on(key, change => {
14
+ setState(change !== undefined ? change : resolveInit(init));
15
+ }, true);
16
+ }, [eventBus, key, init]);
17
+ const emit = useCallback(value => {
18
+ eventBus.emit(key, value);
19
+ }, [eventBus, key]);
20
+ return [state, emit];
21
+ }
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