rxfy-react 0.2.0 → 1.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/dist/index.d.ts CHANGED
@@ -1,32 +1,82 @@
1
1
  import * as react from 'react';
2
- import { IEdge, StatusEnum } from 'rxfy';
3
-
4
- declare function useEdge<TData>(edge: IEdge<TData>): ({
5
- type: StatusEnum.IDLE;
6
- } & {
7
- type: StatusEnum;
8
- }) | ({
9
- type: StatusEnum.PENDING;
10
- } & {
11
- type: StatusEnum;
12
- }) | ({
13
- type: StatusEnum.REJECTED;
14
- error: unknown;
15
- } & {
16
- type: StatusEnum;
17
- }) | ({
18
- type: StatusEnum.FULFILLED;
19
- value: TData;
20
- } & {
21
- type: StatusEnum;
22
- });
23
- type IEdgeProps<TData> = {
24
- edge: IEdge<TData>;
25
- children: IRenderFn<TData>;
26
- rejected?: IRenderFn<unknown>;
27
- pending?: React.ReactNode;
2
+ import { PropsWithChildren } from 'react';
3
+ import { IWrapped, StatusEnum, IModelRegistry, DehydratedState, IAtom, ModelDescriptor, ModelStore, MutationDefs, QueryShapeOf, StateDescriptor } from 'rxfy';
4
+ import { Observable } from 'rxjs';
5
+
6
+ type IRenderable<TData> = React.ReactNode | ((data: TData) => React.ReactNode);
7
+
8
+ type ObservableLike<T> = Observable<T> | T;
9
+ /**
10
+ * Tracks an observable as an IWrapped status for rendering.
11
+ *
12
+ * Contract: `source$` must be referentially stable across renders (memoize it, e.g. from
13
+ * useStateData or useMemo). A new identity restarts the pipeline from PENDING — intended for
14
+ * genuine source changes (new params / reload), but an observable created inline in render
15
+ * restarts every render and never settles.
16
+ *
17
+ * Reload is not part of the status object; trigger it via the StateHandle's reload()
18
+ * or getAttachedReload(source$).
19
+ */
20
+ declare function usePending<T>(source$: ObservableLike<T>, getDefaultValue?: () => T): IWrapped<T, StatusEnum.PENDING | StatusEnum.FULFILLED | StatusEnum.REJECTED>;
21
+
22
+ type IPendingProps<T> = {
23
+ value$: ObservableLike<T>;
24
+ pending?: IRenderable<void>;
25
+ rejected?: IRenderable<IWrapped<T, StatusEnum.REJECTED>>;
26
+ children: IRenderable<T>;
27
+ getDefaultValue?: () => T;
28
+ };
29
+ declare function Pending<T>({ value$, rejected, pending, children, getDefaultValue, }: IPendingProps<T>): react.ReactNode;
30
+
31
+ declare const ModelRegistryContext: react.Context<IModelRegistry | null>;
32
+ declare function useModelRegistry(): IModelRegistry;
33
+
34
+ /**
35
+ * Two-pass SSR for strict renderToString environments (the Apollo getDataFromTree pattern):
36
+ * render → await fetches that suspended into the registry's query cache → render again,
37
+ * until a pass completes with nothing in flight. Each waterfall level costs one extra pass.
38
+ */
39
+ declare function collectStateData(registry: IModelRegistry, render: () => string): Promise<string>;
40
+
41
+ /** True when the app opted into SSR — gates useStateData's server-side Suspense behavior. */
42
+ declare const SsrContext: react.Context<boolean>;
43
+ declare global {
44
+ interface Window {
45
+ /** Push protocol for streamed hydration chunks (see rxfy-react/next HydrationStream). */
46
+ __RXFY_SSR__?: DehydratedState[];
47
+ }
48
+ }
49
+ type StoreProviderProps = PropsWithChildren<{
50
+ /** Enables server-side fetch-and-suspend in useStateData. Pass the same value on server and client. */
51
+ ssr?: boolean;
52
+ /** Per-request registry created by server code so it can dehydrate after rendering. */
53
+ registry?: IModelRegistry;
54
+ /** Snapshot from dehydrate() for prop-based hydration (buffered/two-pass SSR). */
55
+ dehydratedState?: DehydratedState;
56
+ }>;
57
+ declare function StoreProvider({ children, ssr, registry: external, dehydratedState }: StoreProviderProps): react.JSX.Element;
58
+
59
+ /**
60
+ * Binds an IAtom (entity handle, field Lens, or plain Atom) to React as `[value, set]`.
61
+ * `atom$` must be referentially stable across renders — memoize it (e.g. a Lens via useMemo).
62
+ */
63
+ declare function useAtom<T>(atom$: IAtom<T>): [T, (value: T) => void];
64
+
65
+ declare function useModelStore<T>(descriptor: ModelDescriptor<T>): ModelStore<T>;
66
+
67
+ declare function useObservable<T>(observable: Observable<T>, initialValue: T): T;
68
+ declare function useObservable<T>(observable: Observable<T>): T | undefined;
69
+
70
+ type BoundMutations<TShape, TMutations extends MutationDefs<TShape>> = {
71
+ [K in keyof TMutations]: TMutations[K] extends (prev: TShape, ...args: infer A) => TShape ? (...args: A) => void : never;
72
+ };
73
+ type StateHandle<TShape, TMutations extends MutationDefs<TShape> = Record<never, never>> = {
74
+ /** Normalized query state — entity ids only. Read entity data through model stores. */
75
+ readonly data$: Observable<QueryShapeOf<TShape>>;
76
+ readonly set: (value: TShape | ((prev: TShape) => TShape)) => void;
77
+ readonly reload: () => void;
78
+ readonly mutations: BoundMutations<TShape, TMutations>;
28
79
  };
29
- declare function Edge<TData>({ edge, children, rejected, pending }: IEdgeProps<TData>): react.ReactNode;
30
- type IRenderFn<TData> = React.ReactNode | ((data: TData) => React.ReactNode);
80
+ declare function useStateData<TParams, TShape, TMutations extends MutationDefs<TShape>>(state: StateDescriptor<TParams, TShape, TMutations>, fetchFn: (params: TParams, signal: AbortSignal) => Promise<TShape>, params: TParams): StateHandle<TShape, TMutations>;
31
81
 
32
- export { Edge, type IRenderFn, useEdge };
82
+ export { type BoundMutations, type IPendingProps, ModelRegistryContext, type ObservableLike, Pending, SsrContext, type StateHandle, StoreProvider, type StoreProviderProps, collectStateData, useAtom, useModelRegistry, useModelStore, useObservable, usePending, useStateData };