@equinor/fusion-framework-react-app 12.0.4 → 13.0.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/CHANGELOG.md +61 -0
- package/README.md +208 -0
- package/dist/esm/state/index.js +5 -0
- package/dist/esm/state/index.js.map +1 -0
- package/dist/esm/state/useAppState.js +247 -0
- package/dist/esm/state/useAppState.js.map +1 -0
- package/dist/esm/state/useStateSyncEvents.js +48 -0
- package/dist/esm/state/useStateSyncEvents.js.map +1 -0
- package/dist/esm/version.js +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/types/state/index.d.ts +4 -0
- package/dist/types/state/useAppState.d.ts +157 -0
- package/dist/types/state/useStateSyncEvents.d.ts +23 -0
- package/dist/types/version.d.ts +1 -1
- package/package.json +25 -12
- package/src/__tests__/useStateSyncEvents.test.ts +57 -0
- package/src/state/index.ts +12 -0
- package/src/state/useAppState.ts +299 -0
- package/src/state/useStateSyncEvents.ts +59 -0
- package/src/version.ts +1 -1
- package/tsconfig.json +5 -2
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { useLayoutEffect, useRef, useState, useSyncExternalStore } from 'react';
|
|
2
|
+
import { BehaviorSubject } from 'rxjs';
|
|
3
|
+
import { filter } from 'rxjs/operators';
|
|
4
|
+
|
|
5
|
+
import type { EventModule } from '@equinor/fusion-framework-module-event';
|
|
6
|
+
import { StateSyncEvent, type StateSyncEventType } from '@equinor/fusion-framework-module-state';
|
|
7
|
+
|
|
8
|
+
import useAppModule from '../useAppModule';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Subscribes to the app's `state` module sync events (`onStateSync.status`,
|
|
12
|
+
* `onStateSync.change`, `onStateSync.complete`, `onStateSync.error`) and returns the most
|
|
13
|
+
* recent `limit` events, oldest first.
|
|
14
|
+
*
|
|
15
|
+
* Events are only dispatched while the state module's storage is configured for replication
|
|
16
|
+
* (see `PouchDbSyncStorage`) - with the state module's default, local-only storage, this hook
|
|
17
|
+
* returns an empty array.
|
|
18
|
+
*
|
|
19
|
+
* @param limit - Maximum number of most-recent sync events to retain.
|
|
20
|
+
* @returns The most recent sync events, oldest first.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```tsx
|
|
24
|
+
* const events = useStateSyncEvents(20);
|
|
25
|
+
* const lastEvent = events.at(-1);
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* @since 12.1.0
|
|
29
|
+
*/
|
|
30
|
+
export const useStateSyncEvents = (limit: number): StateSyncEventType[] => {
|
|
31
|
+
const eventProvider = useAppModule<EventModule>('event');
|
|
32
|
+
const [event$] = useState(() => new BehaviorSubject<StateSyncEventType[]>([]));
|
|
33
|
+
|
|
34
|
+
// Read through a ref so changing `limit` trims the log without resubscribing.
|
|
35
|
+
const limitRef = useRef(limit);
|
|
36
|
+
limitRef.current = limit;
|
|
37
|
+
|
|
38
|
+
useLayoutEffect(() => {
|
|
39
|
+
// Narrow the shared event stream down to the sync-related events this hook exposes.
|
|
40
|
+
const subscription = eventProvider.event$.pipe(filter(StateSyncEvent.is)).subscribe((event) => {
|
|
41
|
+
const next = [...event$.getValue(), event];
|
|
42
|
+
// Clamp to 0 so `slice(-0)` (a no-op, unlike `slice(-1)`) can't retain the whole log.
|
|
43
|
+
const limit = Math.max(0, limitRef.current);
|
|
44
|
+
event$.next(limit === 0 ? [] : next.length > limit ? next.slice(-limit) : next);
|
|
45
|
+
});
|
|
46
|
+
return () => subscription.unsubscribe();
|
|
47
|
+
}, [eventProvider, event$]);
|
|
48
|
+
|
|
49
|
+
return useSyncExternalStore(
|
|
50
|
+
(onChange) => {
|
|
51
|
+
const subscription = event$.subscribe(onChange);
|
|
52
|
+
return () => subscription.unsubscribe();
|
|
53
|
+
},
|
|
54
|
+
() => event$.getValue(),
|
|
55
|
+
() => event$.getValue(),
|
|
56
|
+
);
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
export default useStateSyncEvents;
|
package/src/version.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
// Generated by genversion.
|
|
2
|
-
export const version = '
|
|
2
|
+
export const version = '13.0.0';
|
package/tsconfig.json
CHANGED
|
@@ -22,10 +22,13 @@
|
|
|
22
22
|
"path": "../../modules/analytics"
|
|
23
23
|
},
|
|
24
24
|
{
|
|
25
|
-
"path": "../../modules/
|
|
25
|
+
"path": "../../modules/navigation"
|
|
26
26
|
},
|
|
27
27
|
{
|
|
28
|
-
"path": "../../modules/
|
|
28
|
+
"path": "../../modules/state"
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
"path": "../../modules/ag-grid"
|
|
29
32
|
},
|
|
30
33
|
{
|
|
31
34
|
"path": "../modules/module"
|