@spoosh/react 0.15.1 → 0.15.2
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.mts +38 -12
- package/dist/index.d.ts +38 -12
- package/dist/index.js +23 -36
- package/dist/index.mjs +24 -37
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -71,6 +71,9 @@ type ExtractAllSubscriptionEvents<T> = SubscriptionReturnType<T> extends {
|
|
|
71
71
|
type ExtractSubscriptionError<T> = SubscriptionReturnType<T> extends {
|
|
72
72
|
error: infer E;
|
|
73
73
|
} ? E : unknown;
|
|
74
|
+
type ExtractSubscriptionParamNames<T> = SubscriptionReturnType<T> extends {
|
|
75
|
+
params: infer P;
|
|
76
|
+
} ? P extends Record<infer K, string | number> ? K extends string ? K : never : never : never;
|
|
74
77
|
|
|
75
78
|
/**
|
|
76
79
|
* Base options for `useRead` hook.
|
|
@@ -476,7 +479,31 @@ interface UseSSEOptions extends UseSSEOptionsBase {
|
|
|
476
479
|
/** Accumulate strategy for combining events. Defaults to 'replace'. */
|
|
477
480
|
accumulate?: AccumulateStrategy | Record<string, AccumulateStrategy | AccumulatorFn>;
|
|
478
481
|
}
|
|
479
|
-
|
|
482
|
+
type SSEReturnType<T> = T extends (...args: never[]) => infer R ? R : never;
|
|
483
|
+
type ExtractSSETriggerQuery<R> = R extends {
|
|
484
|
+
query: infer Q;
|
|
485
|
+
} ? {
|
|
486
|
+
query?: Q;
|
|
487
|
+
} : unknown;
|
|
488
|
+
type ExtractSSETriggerBody<R> = R extends {
|
|
489
|
+
body: infer B;
|
|
490
|
+
} ? {
|
|
491
|
+
body?: B;
|
|
492
|
+
} : unknown;
|
|
493
|
+
type ExtractSSETriggerParams<R> = R extends {
|
|
494
|
+
params: infer P;
|
|
495
|
+
} ? {
|
|
496
|
+
params?: P;
|
|
497
|
+
} : unknown;
|
|
498
|
+
type SSETriggerOptionsFromFn<TSubFn> = SSEReturnType<TSubFn> extends infer R ? ExtractSSETriggerQuery<R> & ExtractSSETriggerBody<R> & ExtractSSETriggerParams<R> : object;
|
|
499
|
+
type SSETriggerOptions<TQuery, TBody, TParams> = (TQuery extends never ? unknown : {
|
|
500
|
+
query?: TQuery;
|
|
501
|
+
}) & (TBody extends never ? unknown : {
|
|
502
|
+
body?: TBody;
|
|
503
|
+
}) & (TParams extends never ? unknown : {
|
|
504
|
+
params?: TParams;
|
|
505
|
+
});
|
|
506
|
+
interface UseSSEResultBase<TEvents, TError> {
|
|
480
507
|
/** Accumulated data keyed by event type */
|
|
481
508
|
data: Partial<TEvents> | undefined;
|
|
482
509
|
/** Connection or parse error */
|
|
@@ -487,19 +514,18 @@ interface UseSSEResult<TEvents, TError> {
|
|
|
487
514
|
loading: boolean;
|
|
488
515
|
/** Plugin metadata */
|
|
489
516
|
meta: Record<string, never>;
|
|
490
|
-
/**
|
|
491
|
-
* Manually trigger connection with optional body/query overrides
|
|
492
|
-
* @param options - Optional body and query parameters
|
|
493
|
-
*/
|
|
494
|
-
trigger: (options?: {
|
|
495
|
-
body?: unknown;
|
|
496
|
-
query?: unknown;
|
|
497
|
-
}) => Promise<void>;
|
|
498
517
|
/** Disconnect from the SSE endpoint */
|
|
499
518
|
disconnect: () => void;
|
|
500
519
|
/** Reset accumulated data */
|
|
501
520
|
reset: () => void;
|
|
502
521
|
}
|
|
522
|
+
type UseSSEResult<TEvents, TError, TSubFn> = UseSSEResultBase<TEvents, TError> & {
|
|
523
|
+
/**
|
|
524
|
+
* Manually trigger connection with optional body/query/params overrides
|
|
525
|
+
* @param options - Optional body, query, and params parameters
|
|
526
|
+
*/
|
|
527
|
+
trigger: (options?: SSETriggerOptionsFromFn<TSubFn>) => Promise<void>;
|
|
528
|
+
};
|
|
503
529
|
|
|
504
530
|
type InferError<T, TDefaultError> = unknown extends T ? TDefaultError : T;
|
|
505
531
|
type WriteResolverContext<TSchema, TMethod, TDefaultError> = ResolverContext<TSchema, ExtractMethodData<TMethod>, InferError<ExtractMethodError<TMethod>, TDefaultError>, ExtractMethodQuery<TMethod>, ExtractMethodBody<TMethod>, ExtractResponseParamNames<TMethod> extends never ? never : Record<ExtractResponseParamNames<TMethod>, string | number>>;
|
|
@@ -535,13 +561,13 @@ type UseSSEFn<TDefaultError, TSchema, TPlugins extends PluginArray> = {
|
|
|
535
561
|
}>;
|
|
536
562
|
}, const TSelectedEvents extends readonly Extract<ExtractAllSubscriptionEventKeys<TSubFn>, string>[]>(subFn: TSubFn, sseOptions: TypedUseSSEOptions<Extract<ExtractAllSubscriptionEventKeys<TSubFn>, string>, InferSSEEvents<TSubFn>, TSelectedEvents> & {
|
|
537
563
|
events: TSelectedEvents;
|
|
538
|
-
}): UseSSEResult<FilteredEvents<InferSSEEvents<TSubFn>, TSelectedEvents>, InferError<ExtractMethodError<TSubFn>, TDefaultError
|
|
564
|
+
}): UseSSEResult<FilteredEvents<InferSSEEvents<TSubFn>, TSelectedEvents>, InferError<ExtractMethodError<TSubFn>, TDefaultError>, TSubFn>;
|
|
539
565
|
<TSubFn extends (api: SubscriptionApiClient<TSchema, TDefaultError>) => {
|
|
540
566
|
_subscription: true;
|
|
541
567
|
events: Record<string, {
|
|
542
568
|
data: unknown;
|
|
543
569
|
}>;
|
|
544
|
-
}>(subFn: TSubFn, sseOptions?: Omit<TypedUseSSEOptions<Extract<ExtractAllSubscriptionEventKeys<TSubFn>, string>, InferSSEEvents<TSubFn>>, "events">): UseSSEResult<InferSSEEvents<TSubFn>, InferError<ExtractMethodError<TSubFn>, TDefaultError
|
|
570
|
+
}>(subFn: TSubFn, sseOptions?: Omit<TypedUseSSEOptions<Extract<ExtractAllSubscriptionEventKeys<TSubFn>, string>, InferSSEEvents<TSubFn>>, "events">): UseSSEResult<InferSSEEvents<TSubFn>, InferError<ExtractMethodError<TSubFn>, TDefaultError>, TSubFn>;
|
|
545
571
|
};
|
|
546
572
|
/**
|
|
547
573
|
* Base hooks that are always available.
|
|
@@ -737,4 +763,4 @@ type PluginHooksConfig<TPlugins extends readonly SpooshPlugin<PluginTypeConfig>[
|
|
|
737
763
|
plugins: TPlugins;
|
|
738
764
|
};
|
|
739
765
|
|
|
740
|
-
export { type ApiClient, type BasePagesOptions, type BasePagesResult, type BaseReadOptions, type BaseReadResult, type BaseSubscriptionOptions, type BaseSubscriptionResult, type BaseWriteResult, type ExtractAllSubscriptionEventKeys, type ExtractAllSubscriptionEvents, type ExtractCoreMethodOptions, type ExtractMethodBody, type ExtractMethodData, type ExtractMethodError, type ExtractMethodOptions, type ExtractMethodQuery, type ExtractResponseBody, type ExtractResponseParamNames, type ExtractResponseQuery, type ExtractResponseRequestOptions, type ExtractSubscriptionBody, type ExtractSubscriptionError, type ExtractSubscriptionEvents, type ExtractSubscriptionQuery, type PagesApiClient, type PagesTriggerOptions, type PluginHooksConfig, type QueueApiClient, type QueueTriggerInput, type ReactOptionsMap, type ReadApiClient, type ResponseInputFields, type SpooshReactHooks, type SubscriptionApiClient, type SubscriptionTriggerInput, type TriggerOptions, type TypedAccumulateConfig, type TypedParseConfig, type TypedUseSSEOptions, type UsePagesResult, type UseQueueOptions, type UseQueueResult, type UseReadResult, type UseSSEOptions, type UseSSEOptionsBase, type UseSSEResult, type UseWriteResult, type WriteApiClient, type WriteResponseInputFields, type WriteTriggerInput, create };
|
|
766
|
+
export { type ApiClient, type BasePagesOptions, type BasePagesResult, type BaseReadOptions, type BaseReadResult, type BaseSubscriptionOptions, type BaseSubscriptionResult, type BaseWriteResult, type ExtractAllSubscriptionEventKeys, type ExtractAllSubscriptionEvents, type ExtractCoreMethodOptions, type ExtractMethodBody, type ExtractMethodData, type ExtractMethodError, type ExtractMethodOptions, type ExtractMethodQuery, type ExtractResponseBody, type ExtractResponseParamNames, type ExtractResponseQuery, type ExtractResponseRequestOptions, type ExtractSubscriptionBody, type ExtractSubscriptionError, type ExtractSubscriptionEvents, type ExtractSubscriptionParamNames, type ExtractSubscriptionQuery, type PagesApiClient, type PagesTriggerOptions, type PluginHooksConfig, type QueueApiClient, type QueueTriggerInput, type ReactOptionsMap, type ReadApiClient, type ResponseInputFields, type SSETriggerOptions, type SSETriggerOptionsFromFn, type SpooshReactHooks, type SubscriptionApiClient, type SubscriptionTriggerInput, type TriggerOptions, type TypedAccumulateConfig, type TypedParseConfig, type TypedUseSSEOptions, type UsePagesResult, type UseQueueOptions, type UseQueueResult, type UseReadResult, type UseSSEOptions, type UseSSEOptionsBase, type UseSSEResult, type UseSSEResultBase, type UseWriteResult, type WriteApiClient, type WriteResponseInputFields, type WriteTriggerInput, create };
|
package/dist/index.d.ts
CHANGED
|
@@ -71,6 +71,9 @@ type ExtractAllSubscriptionEvents<T> = SubscriptionReturnType<T> extends {
|
|
|
71
71
|
type ExtractSubscriptionError<T> = SubscriptionReturnType<T> extends {
|
|
72
72
|
error: infer E;
|
|
73
73
|
} ? E : unknown;
|
|
74
|
+
type ExtractSubscriptionParamNames<T> = SubscriptionReturnType<T> extends {
|
|
75
|
+
params: infer P;
|
|
76
|
+
} ? P extends Record<infer K, string | number> ? K extends string ? K : never : never : never;
|
|
74
77
|
|
|
75
78
|
/**
|
|
76
79
|
* Base options for `useRead` hook.
|
|
@@ -476,7 +479,31 @@ interface UseSSEOptions extends UseSSEOptionsBase {
|
|
|
476
479
|
/** Accumulate strategy for combining events. Defaults to 'replace'. */
|
|
477
480
|
accumulate?: AccumulateStrategy | Record<string, AccumulateStrategy | AccumulatorFn>;
|
|
478
481
|
}
|
|
479
|
-
|
|
482
|
+
type SSEReturnType<T> = T extends (...args: never[]) => infer R ? R : never;
|
|
483
|
+
type ExtractSSETriggerQuery<R> = R extends {
|
|
484
|
+
query: infer Q;
|
|
485
|
+
} ? {
|
|
486
|
+
query?: Q;
|
|
487
|
+
} : unknown;
|
|
488
|
+
type ExtractSSETriggerBody<R> = R extends {
|
|
489
|
+
body: infer B;
|
|
490
|
+
} ? {
|
|
491
|
+
body?: B;
|
|
492
|
+
} : unknown;
|
|
493
|
+
type ExtractSSETriggerParams<R> = R extends {
|
|
494
|
+
params: infer P;
|
|
495
|
+
} ? {
|
|
496
|
+
params?: P;
|
|
497
|
+
} : unknown;
|
|
498
|
+
type SSETriggerOptionsFromFn<TSubFn> = SSEReturnType<TSubFn> extends infer R ? ExtractSSETriggerQuery<R> & ExtractSSETriggerBody<R> & ExtractSSETriggerParams<R> : object;
|
|
499
|
+
type SSETriggerOptions<TQuery, TBody, TParams> = (TQuery extends never ? unknown : {
|
|
500
|
+
query?: TQuery;
|
|
501
|
+
}) & (TBody extends never ? unknown : {
|
|
502
|
+
body?: TBody;
|
|
503
|
+
}) & (TParams extends never ? unknown : {
|
|
504
|
+
params?: TParams;
|
|
505
|
+
});
|
|
506
|
+
interface UseSSEResultBase<TEvents, TError> {
|
|
480
507
|
/** Accumulated data keyed by event type */
|
|
481
508
|
data: Partial<TEvents> | undefined;
|
|
482
509
|
/** Connection or parse error */
|
|
@@ -487,19 +514,18 @@ interface UseSSEResult<TEvents, TError> {
|
|
|
487
514
|
loading: boolean;
|
|
488
515
|
/** Plugin metadata */
|
|
489
516
|
meta: Record<string, never>;
|
|
490
|
-
/**
|
|
491
|
-
* Manually trigger connection with optional body/query overrides
|
|
492
|
-
* @param options - Optional body and query parameters
|
|
493
|
-
*/
|
|
494
|
-
trigger: (options?: {
|
|
495
|
-
body?: unknown;
|
|
496
|
-
query?: unknown;
|
|
497
|
-
}) => Promise<void>;
|
|
498
517
|
/** Disconnect from the SSE endpoint */
|
|
499
518
|
disconnect: () => void;
|
|
500
519
|
/** Reset accumulated data */
|
|
501
520
|
reset: () => void;
|
|
502
521
|
}
|
|
522
|
+
type UseSSEResult<TEvents, TError, TSubFn> = UseSSEResultBase<TEvents, TError> & {
|
|
523
|
+
/**
|
|
524
|
+
* Manually trigger connection with optional body/query/params overrides
|
|
525
|
+
* @param options - Optional body, query, and params parameters
|
|
526
|
+
*/
|
|
527
|
+
trigger: (options?: SSETriggerOptionsFromFn<TSubFn>) => Promise<void>;
|
|
528
|
+
};
|
|
503
529
|
|
|
504
530
|
type InferError<T, TDefaultError> = unknown extends T ? TDefaultError : T;
|
|
505
531
|
type WriteResolverContext<TSchema, TMethod, TDefaultError> = ResolverContext<TSchema, ExtractMethodData<TMethod>, InferError<ExtractMethodError<TMethod>, TDefaultError>, ExtractMethodQuery<TMethod>, ExtractMethodBody<TMethod>, ExtractResponseParamNames<TMethod> extends never ? never : Record<ExtractResponseParamNames<TMethod>, string | number>>;
|
|
@@ -535,13 +561,13 @@ type UseSSEFn<TDefaultError, TSchema, TPlugins extends PluginArray> = {
|
|
|
535
561
|
}>;
|
|
536
562
|
}, const TSelectedEvents extends readonly Extract<ExtractAllSubscriptionEventKeys<TSubFn>, string>[]>(subFn: TSubFn, sseOptions: TypedUseSSEOptions<Extract<ExtractAllSubscriptionEventKeys<TSubFn>, string>, InferSSEEvents<TSubFn>, TSelectedEvents> & {
|
|
537
563
|
events: TSelectedEvents;
|
|
538
|
-
}): UseSSEResult<FilteredEvents<InferSSEEvents<TSubFn>, TSelectedEvents>, InferError<ExtractMethodError<TSubFn>, TDefaultError
|
|
564
|
+
}): UseSSEResult<FilteredEvents<InferSSEEvents<TSubFn>, TSelectedEvents>, InferError<ExtractMethodError<TSubFn>, TDefaultError>, TSubFn>;
|
|
539
565
|
<TSubFn extends (api: SubscriptionApiClient<TSchema, TDefaultError>) => {
|
|
540
566
|
_subscription: true;
|
|
541
567
|
events: Record<string, {
|
|
542
568
|
data: unknown;
|
|
543
569
|
}>;
|
|
544
|
-
}>(subFn: TSubFn, sseOptions?: Omit<TypedUseSSEOptions<Extract<ExtractAllSubscriptionEventKeys<TSubFn>, string>, InferSSEEvents<TSubFn>>, "events">): UseSSEResult<InferSSEEvents<TSubFn>, InferError<ExtractMethodError<TSubFn>, TDefaultError
|
|
570
|
+
}>(subFn: TSubFn, sseOptions?: Omit<TypedUseSSEOptions<Extract<ExtractAllSubscriptionEventKeys<TSubFn>, string>, InferSSEEvents<TSubFn>>, "events">): UseSSEResult<InferSSEEvents<TSubFn>, InferError<ExtractMethodError<TSubFn>, TDefaultError>, TSubFn>;
|
|
545
571
|
};
|
|
546
572
|
/**
|
|
547
573
|
* Base hooks that are always available.
|
|
@@ -737,4 +763,4 @@ type PluginHooksConfig<TPlugins extends readonly SpooshPlugin<PluginTypeConfig>[
|
|
|
737
763
|
plugins: TPlugins;
|
|
738
764
|
};
|
|
739
765
|
|
|
740
|
-
export { type ApiClient, type BasePagesOptions, type BasePagesResult, type BaseReadOptions, type BaseReadResult, type BaseSubscriptionOptions, type BaseSubscriptionResult, type BaseWriteResult, type ExtractAllSubscriptionEventKeys, type ExtractAllSubscriptionEvents, type ExtractCoreMethodOptions, type ExtractMethodBody, type ExtractMethodData, type ExtractMethodError, type ExtractMethodOptions, type ExtractMethodQuery, type ExtractResponseBody, type ExtractResponseParamNames, type ExtractResponseQuery, type ExtractResponseRequestOptions, type ExtractSubscriptionBody, type ExtractSubscriptionError, type ExtractSubscriptionEvents, type ExtractSubscriptionQuery, type PagesApiClient, type PagesTriggerOptions, type PluginHooksConfig, type QueueApiClient, type QueueTriggerInput, type ReactOptionsMap, type ReadApiClient, type ResponseInputFields, type SpooshReactHooks, type SubscriptionApiClient, type SubscriptionTriggerInput, type TriggerOptions, type TypedAccumulateConfig, type TypedParseConfig, type TypedUseSSEOptions, type UsePagesResult, type UseQueueOptions, type UseQueueResult, type UseReadResult, type UseSSEOptions, type UseSSEOptionsBase, type UseSSEResult, type UseWriteResult, type WriteApiClient, type WriteResponseInputFields, type WriteTriggerInput, create };
|
|
766
|
+
export { type ApiClient, type BasePagesOptions, type BasePagesResult, type BaseReadOptions, type BaseReadResult, type BaseSubscriptionOptions, type BaseSubscriptionResult, type BaseWriteResult, type ExtractAllSubscriptionEventKeys, type ExtractAllSubscriptionEvents, type ExtractCoreMethodOptions, type ExtractMethodBody, type ExtractMethodData, type ExtractMethodError, type ExtractMethodOptions, type ExtractMethodQuery, type ExtractResponseBody, type ExtractResponseParamNames, type ExtractResponseQuery, type ExtractResponseRequestOptions, type ExtractSubscriptionBody, type ExtractSubscriptionError, type ExtractSubscriptionEvents, type ExtractSubscriptionParamNames, type ExtractSubscriptionQuery, type PagesApiClient, type PagesTriggerOptions, type PluginHooksConfig, type QueueApiClient, type QueueTriggerInput, type ReactOptionsMap, type ReadApiClient, type ResponseInputFields, type SSETriggerOptions, type SSETriggerOptionsFromFn, type SpooshReactHooks, type SubscriptionApiClient, type SubscriptionTriggerInput, type TriggerOptions, type TypedAccumulateConfig, type TypedParseConfig, type TypedUseSSEOptions, type UsePagesResult, type UseQueueOptions, type UseQueueResult, type UseReadResult, type UseSSEOptions, type UseSSEOptionsBase, type UseSSEResult, type UseSSEResultBase, type UseWriteResult, type WriteApiClient, type WriteResponseInputFields, type WriteTriggerInput, create };
|
package/dist/index.js
CHANGED
|
@@ -688,11 +688,9 @@ function createUseSubscription(options) {
|
|
|
688
688
|
});
|
|
689
689
|
const controllerRef = (0, import_react5.useRef)(null);
|
|
690
690
|
const subscriptionVersionRef = (0, import_react5.useRef)(0);
|
|
691
|
-
const
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
}
|
|
695
|
-
const controller = (0, import_core5.createSubscriptionController)({
|
|
691
|
+
const queryKeyChanged = controllerRef.current && controllerRef.current.queryKey !== queryKey;
|
|
692
|
+
if (!controllerRef.current || queryKeyChanged) {
|
|
693
|
+
const controller2 = (0, import_core5.createSubscriptionController)({
|
|
696
694
|
channel: capturedCall.path,
|
|
697
695
|
baseAdapter: adapter,
|
|
698
696
|
stateManager,
|
|
@@ -703,33 +701,18 @@ function createUseSubscription(options) {
|
|
|
703
701
|
path: capturedCall.path,
|
|
704
702
|
method: capturedCall.method
|
|
705
703
|
});
|
|
706
|
-
controllerRef.current = controller;
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
queryKey,
|
|
710
|
-
adapter,
|
|
711
|
-
operationType,
|
|
712
|
-
capturedCall.path,
|
|
713
|
-
capturedCall.method
|
|
714
|
-
]);
|
|
704
|
+
controllerRef.current = { controller: controller2, queryKey };
|
|
705
|
+
}
|
|
706
|
+
const controller = controllerRef.current.controller;
|
|
715
707
|
const subscribe = (0, import_react5.useCallback)(
|
|
716
708
|
(callback) => {
|
|
717
|
-
const controller = getOrCreateController();
|
|
718
709
|
return controller.subscribe(callback);
|
|
719
710
|
},
|
|
720
|
-
[
|
|
711
|
+
[controller]
|
|
721
712
|
);
|
|
722
|
-
const emptyStateRef = (0, import_react5.useRef)({
|
|
723
|
-
data: void 0,
|
|
724
|
-
error: void 0,
|
|
725
|
-
isConnected: false
|
|
726
|
-
});
|
|
727
713
|
const getSnapshot = (0, import_react5.useCallback)(() => {
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
}
|
|
731
|
-
return controllerRef.current.getState();
|
|
732
|
-
}, []);
|
|
714
|
+
return controller.getState();
|
|
715
|
+
}, [controller]);
|
|
733
716
|
const state = (0, import_react5.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
|
|
734
717
|
const [isPending, setIsPending] = (0, import_react5.useState)(enabled);
|
|
735
718
|
(0, import_react5.useEffect)(() => {
|
|
@@ -737,14 +720,13 @@ function createUseSubscription(options) {
|
|
|
737
720
|
return;
|
|
738
721
|
}
|
|
739
722
|
setIsPending(true);
|
|
740
|
-
const controller = getOrCreateController();
|
|
741
723
|
controller.mount();
|
|
742
724
|
controller.subscribe();
|
|
743
725
|
return () => {
|
|
744
726
|
subscriptionVersionRef.current++;
|
|
745
727
|
controller.unsubscribe();
|
|
746
728
|
};
|
|
747
|
-
}, [queryKey, enabled,
|
|
729
|
+
}, [queryKey, enabled, controller]);
|
|
748
730
|
(0, import_react5.useEffect)(() => {
|
|
749
731
|
if (state.isConnected || state.data !== void 0 || state.error !== void 0) {
|
|
750
732
|
setIsPending(false);
|
|
@@ -752,18 +734,15 @@ function createUseSubscription(options) {
|
|
|
752
734
|
}, [state.isConnected, state.data, state.error]);
|
|
753
735
|
const disconnect = (0, import_react5.useCallback)(() => {
|
|
754
736
|
subscriptionVersionRef.current++;
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
}
|
|
758
|
-
}, []);
|
|
737
|
+
controller.unsubscribe();
|
|
738
|
+
}, [controller]);
|
|
759
739
|
const trigger = (0, import_react5.useCallback)(async () => {
|
|
760
740
|
setIsPending(true);
|
|
761
741
|
subscriptionVersionRef.current++;
|
|
762
|
-
const controller = getOrCreateController();
|
|
763
742
|
controller.unsubscribe();
|
|
764
743
|
controller.mount();
|
|
765
744
|
await controller.subscribe();
|
|
766
|
-
}, [
|
|
745
|
+
}, [controller]);
|
|
767
746
|
const loading = isPending;
|
|
768
747
|
return {
|
|
769
748
|
meta: {},
|
|
@@ -822,12 +801,19 @@ function createUseSSE(options) {
|
|
|
822
801
|
if (!capturedCall) {
|
|
823
802
|
throw new Error("useSSE requires calling a method");
|
|
824
803
|
}
|
|
804
|
+
const requestOptions = capturedCall.options;
|
|
805
|
+
const resolvedPath = (0, import_core6.resolvePathString)(
|
|
806
|
+
capturedCall.path,
|
|
807
|
+
requestOptions?.params
|
|
808
|
+
);
|
|
809
|
+
const paramsKey = requestOptions?.params ? JSON.stringify(requestOptions.params) : "";
|
|
825
810
|
const currentOptionsRef = (0, import_react6.useRef)(
|
|
826
811
|
capturedCall.options
|
|
827
812
|
);
|
|
813
|
+
currentOptionsRef.current = capturedCall.options;
|
|
828
814
|
const adapter = (0, import_react6.useMemo)(
|
|
829
815
|
() => transport.createSubscriptionAdapter({
|
|
830
|
-
channel:
|
|
816
|
+
channel: resolvedPath,
|
|
831
817
|
method: capturedCall.method,
|
|
832
818
|
baseUrl: config.baseUrl,
|
|
833
819
|
globalHeaders: config.defaultOptions.headers,
|
|
@@ -835,7 +821,7 @@ function createUseSSE(options) {
|
|
|
835
821
|
eventEmitter,
|
|
836
822
|
devtoolMeta: events ? { listenedEvents: events } : void 0
|
|
837
823
|
}),
|
|
838
|
-
[
|
|
824
|
+
[resolvedPath, capturedCall.method, paramsKey]
|
|
839
825
|
);
|
|
840
826
|
const [accumulatedData, setAccumulatedData] = (0, import_react6.useState)({});
|
|
841
827
|
const eventSet = (0, import_react6.useMemo)(
|
|
@@ -928,6 +914,7 @@ function createUseSSE(options) {
|
|
|
928
914
|
setAccumulatedData({});
|
|
929
915
|
}, []);
|
|
930
916
|
const trigger = (0, import_react6.useCallback)(
|
|
917
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
931
918
|
async (opts) => {
|
|
932
919
|
reset();
|
|
933
920
|
const triggerOpts = {
|
package/dist/index.mjs
CHANGED
|
@@ -712,11 +712,9 @@ function createUseSubscription(options) {
|
|
|
712
712
|
});
|
|
713
713
|
const controllerRef = useRef5(null);
|
|
714
714
|
const subscriptionVersionRef = useRef5(0);
|
|
715
|
-
const
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
}
|
|
719
|
-
const controller = createSubscriptionController({
|
|
715
|
+
const queryKeyChanged = controllerRef.current && controllerRef.current.queryKey !== queryKey;
|
|
716
|
+
if (!controllerRef.current || queryKeyChanged) {
|
|
717
|
+
const controller2 = createSubscriptionController({
|
|
720
718
|
channel: capturedCall.path,
|
|
721
719
|
baseAdapter: adapter,
|
|
722
720
|
stateManager,
|
|
@@ -727,33 +725,18 @@ function createUseSubscription(options) {
|
|
|
727
725
|
path: capturedCall.path,
|
|
728
726
|
method: capturedCall.method
|
|
729
727
|
});
|
|
730
|
-
controllerRef.current = controller;
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
queryKey,
|
|
734
|
-
adapter,
|
|
735
|
-
operationType,
|
|
736
|
-
capturedCall.path,
|
|
737
|
-
capturedCall.method
|
|
738
|
-
]);
|
|
728
|
+
controllerRef.current = { controller: controller2, queryKey };
|
|
729
|
+
}
|
|
730
|
+
const controller = controllerRef.current.controller;
|
|
739
731
|
const subscribe = useCallback4(
|
|
740
732
|
(callback) => {
|
|
741
|
-
const controller = getOrCreateController();
|
|
742
733
|
return controller.subscribe(callback);
|
|
743
734
|
},
|
|
744
|
-
[
|
|
735
|
+
[controller]
|
|
745
736
|
);
|
|
746
|
-
const emptyStateRef = useRef5({
|
|
747
|
-
data: void 0,
|
|
748
|
-
error: void 0,
|
|
749
|
-
isConnected: false
|
|
750
|
-
});
|
|
751
737
|
const getSnapshot = useCallback4(() => {
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
}
|
|
755
|
-
return controllerRef.current.getState();
|
|
756
|
-
}, []);
|
|
738
|
+
return controller.getState();
|
|
739
|
+
}, [controller]);
|
|
757
740
|
const state = useSyncExternalStore5(subscribe, getSnapshot, getSnapshot);
|
|
758
741
|
const [isPending, setIsPending] = useState4(enabled);
|
|
759
742
|
useEffect4(() => {
|
|
@@ -761,14 +744,13 @@ function createUseSubscription(options) {
|
|
|
761
744
|
return;
|
|
762
745
|
}
|
|
763
746
|
setIsPending(true);
|
|
764
|
-
const controller = getOrCreateController();
|
|
765
747
|
controller.mount();
|
|
766
748
|
controller.subscribe();
|
|
767
749
|
return () => {
|
|
768
750
|
subscriptionVersionRef.current++;
|
|
769
751
|
controller.unsubscribe();
|
|
770
752
|
};
|
|
771
|
-
}, [queryKey, enabled,
|
|
753
|
+
}, [queryKey, enabled, controller]);
|
|
772
754
|
useEffect4(() => {
|
|
773
755
|
if (state.isConnected || state.data !== void 0 || state.error !== void 0) {
|
|
774
756
|
setIsPending(false);
|
|
@@ -776,18 +758,15 @@ function createUseSubscription(options) {
|
|
|
776
758
|
}, [state.isConnected, state.data, state.error]);
|
|
777
759
|
const disconnect = useCallback4(() => {
|
|
778
760
|
subscriptionVersionRef.current++;
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
}
|
|
782
|
-
}, []);
|
|
761
|
+
controller.unsubscribe();
|
|
762
|
+
}, [controller]);
|
|
783
763
|
const trigger = useCallback4(async () => {
|
|
784
764
|
setIsPending(true);
|
|
785
765
|
subscriptionVersionRef.current++;
|
|
786
|
-
const controller = getOrCreateController();
|
|
787
766
|
controller.unsubscribe();
|
|
788
767
|
controller.mount();
|
|
789
768
|
await controller.subscribe();
|
|
790
|
-
}, [
|
|
769
|
+
}, [controller]);
|
|
791
770
|
const loading = isPending;
|
|
792
771
|
return {
|
|
793
772
|
meta: {},
|
|
@@ -806,7 +785,7 @@ function createUseSubscription(options) {
|
|
|
806
785
|
|
|
807
786
|
// src/useSSE/index.ts
|
|
808
787
|
import { useState as useState5, useRef as useRef6, useEffect as useEffect5, useCallback as useCallback5, useMemo } from "react";
|
|
809
|
-
import { createSelectorProxy as createSelectorProxy6 } from "@spoosh/core";
|
|
788
|
+
import { createSelectorProxy as createSelectorProxy6, resolvePathString } from "@spoosh/core";
|
|
810
789
|
function isSSETransport(transport) {
|
|
811
790
|
const t = transport;
|
|
812
791
|
return typeof t.createSubscriptionAdapter === "function" && typeof t.utils?.resolveParser === "function" && typeof t.utils?.resolveAccumulator === "function";
|
|
@@ -846,12 +825,19 @@ function createUseSSE(options) {
|
|
|
846
825
|
if (!capturedCall) {
|
|
847
826
|
throw new Error("useSSE requires calling a method");
|
|
848
827
|
}
|
|
828
|
+
const requestOptions = capturedCall.options;
|
|
829
|
+
const resolvedPath = resolvePathString(
|
|
830
|
+
capturedCall.path,
|
|
831
|
+
requestOptions?.params
|
|
832
|
+
);
|
|
833
|
+
const paramsKey = requestOptions?.params ? JSON.stringify(requestOptions.params) : "";
|
|
849
834
|
const currentOptionsRef = useRef6(
|
|
850
835
|
capturedCall.options
|
|
851
836
|
);
|
|
837
|
+
currentOptionsRef.current = capturedCall.options;
|
|
852
838
|
const adapter = useMemo(
|
|
853
839
|
() => transport.createSubscriptionAdapter({
|
|
854
|
-
channel:
|
|
840
|
+
channel: resolvedPath,
|
|
855
841
|
method: capturedCall.method,
|
|
856
842
|
baseUrl: config.baseUrl,
|
|
857
843
|
globalHeaders: config.defaultOptions.headers,
|
|
@@ -859,7 +845,7 @@ function createUseSSE(options) {
|
|
|
859
845
|
eventEmitter,
|
|
860
846
|
devtoolMeta: events ? { listenedEvents: events } : void 0
|
|
861
847
|
}),
|
|
862
|
-
[
|
|
848
|
+
[resolvedPath, capturedCall.method, paramsKey]
|
|
863
849
|
);
|
|
864
850
|
const [accumulatedData, setAccumulatedData] = useState5({});
|
|
865
851
|
const eventSet = useMemo(
|
|
@@ -952,6 +938,7 @@ function createUseSSE(options) {
|
|
|
952
938
|
setAccumulatedData({});
|
|
953
939
|
}, []);
|
|
954
940
|
const trigger = useCallback5(
|
|
941
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
955
942
|
async (opts) => {
|
|
956
943
|
reset();
|
|
957
944
|
const triggerOpts = {
|