@pixotope/query 0.2.0 → 0.5.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/.turbo/turbo-build.log +10 -10
- package/.turbo/turbo-check-types.log +1 -1
- package/.turbo/turbo-test.log +5 -5
- package/CHANGELOG.md +20 -0
- package/dist/index.cjs +64 -44
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +92 -6
- package/dist/index.d.ts +92 -6
- package/dist/index.js +59 -43
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
- package/src/factories.ts +23 -12
- package/src/index.ts +3 -0
- package/src/proxyBinders.ts +24 -0
- package/src/useQuery.test.ts +4 -5
- package/src/useQuery.ts +39 -36
- package/src/useSubscription.ts +0 -7
package/dist/index.d.cts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as _pixotope_zmq_client from '@pixotope/zmq-client';
|
|
2
|
+
import { ZMQClient, TSocketServerCallOptions, ZMQProxyWSClient } from '@pixotope/zmq-client';
|
|
2
3
|
|
|
3
4
|
interface QueryOptions<TData = unknown, TError = unknown, TParams = void, TSelectData = TData, Stateless extends true | undefined = undefined> {
|
|
4
5
|
queryFn: (params: TParams | undefined | void) => Promise<TData>;
|
|
@@ -45,6 +46,67 @@ interface MutateOptions<TData = unknown, TError = unknown, TParams = void> {
|
|
|
45
46
|
onError?: (error: TError, variables: TParams) => void;
|
|
46
47
|
onSettled?: (data: TData | undefined, error: TError | null, variables: TParams) => void;
|
|
47
48
|
}
|
|
49
|
+
type MutationStatus = "idle" | "loading" | "success" | "error";
|
|
50
|
+
declare class ComparisonFailError<TPayload = unknown> extends Error {
|
|
51
|
+
readonly payload: TPayload;
|
|
52
|
+
constructor(message: string, payload: TPayload);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* `useMutation` is a hook that allows you to perform a mutation/push updates to the services while
|
|
56
|
+
* providing extensive API to tap into the status and results of the mutation.
|
|
57
|
+
* @param mutationFn The mutation function to be called when the `mutate` function is called.
|
|
58
|
+
* `useMutation` does not care about what the function does, all it needs it a function that returns
|
|
59
|
+
* a promise.
|
|
60
|
+
* @param options Options to configure the mutation.
|
|
61
|
+
* @returns An object with the following properties:
|
|
62
|
+
* - `mutateAsync`: A function that takes in the variables and returns a promise that resolves to the
|
|
63
|
+
* result of the mutation. This function can be awaited to get the result of the mutation. Callbacks can
|
|
64
|
+
* be passed to this function to be called when the mutation is successful or when it fails. It's good idea
|
|
65
|
+
* to wrap this function in a try/catch block.
|
|
66
|
+
* - `mutate`: A function that takes in the variables and returns nothing. This function does not
|
|
67
|
+
* return a promise. Callbacks can be passed to this function to be called when the mutation is
|
|
68
|
+
* successful or when it fails.
|
|
69
|
+
* - `status`: The status of the mutation. Can be one of `idle`, `loading`, `success` or `error`.
|
|
70
|
+
* - `result`: The result of the mutation. This is undefined until the mutation is successful.
|
|
71
|
+
* - `reset`: A function that resets the mutation to the initial state.
|
|
72
|
+
* - `error`: The error object if the mutation has failed.
|
|
73
|
+
* @example
|
|
74
|
+
* ```typescript
|
|
75
|
+
* const mutationRes = useMutation({
|
|
76
|
+
* mutationFn: (variables) => {
|
|
77
|
+
* return ZMQHelper.callSomethingAsync(variables)
|
|
78
|
+
* },
|
|
79
|
+
* onSuccess: (data) => {
|
|
80
|
+
* // do something with the result
|
|
81
|
+
* },
|
|
82
|
+
* onError: (error) => {
|
|
83
|
+
* // do something with the error
|
|
84
|
+
* },
|
|
85
|
+
* onSettled: (data, error) => {
|
|
86
|
+
* // do something when the mutation is done
|
|
87
|
+
* }});
|
|
88
|
+
*
|
|
89
|
+
* // call the mutation
|
|
90
|
+
* const res = await mutationRes.mutateAsync({ foo: "bar" });
|
|
91
|
+
* // OR
|
|
92
|
+
* mutationRes.mutate({ foo: "bar" }, {
|
|
93
|
+
* onSuccess: (data) => {
|
|
94
|
+
* // do something with the result
|
|
95
|
+
* // for example show a toast message
|
|
96
|
+
* // stop local loading state
|
|
97
|
+
* });
|
|
98
|
+
* ```
|
|
99
|
+
*/
|
|
100
|
+
declare const useMutation: <TData = unknown, TError = unknown, TParams = void>(options: MutationOptions<TData, TError, TParams>) => {
|
|
101
|
+
mutateAsync: (params: TParams, overrideTargetMachine?: string) => Promise<TData>;
|
|
102
|
+
status: MutationStatus;
|
|
103
|
+
result: TData | undefined;
|
|
104
|
+
mutate: (variables: TParams, options?: MutateOptions<TData, TError, TParams> & {
|
|
105
|
+
overrideTargetMachine?: string;
|
|
106
|
+
}) => void;
|
|
107
|
+
reset: () => void;
|
|
108
|
+
error: TError | null;
|
|
109
|
+
};
|
|
48
110
|
|
|
49
111
|
type MutationInfo<TData, TError, TParams> = {
|
|
50
112
|
data: TData;
|
|
@@ -52,7 +114,12 @@ type MutationInfo<TData, TError, TParams> = {
|
|
|
52
114
|
params: TParams;
|
|
53
115
|
};
|
|
54
116
|
type FnToParamsBase = Record<string, MutationInfo<unknown, unknown, unknown>>;
|
|
55
|
-
|
|
117
|
+
type TPromiseFunction<TResponse, TParams> = (args: Omit<ZMQClient.Call<TResponse, {
|
|
118
|
+
functionName: string;
|
|
119
|
+
parameters: TParams;
|
|
120
|
+
options?: Omit<TSocketServerCallOptions, "toCallIfNoResponse">;
|
|
121
|
+
}>, "id">) => Promise<TResponse>;
|
|
122
|
+
declare const useCallMutationBase: <TFnToParams extends FnToParamsBase, TData = unknown, TError = unknown, TParams = unknown>(promiseFunc: TPromiseFunction<TData, TParams>, functionName: keyof TFnToParams, service: string, options?: Omit<MutationOptions<TData, TError, TParams>, "mutationFn"> & {
|
|
56
123
|
avoidTimeout?: boolean;
|
|
57
124
|
customTimeout?: number;
|
|
58
125
|
}) => {
|
|
@@ -65,7 +132,7 @@ declare const useCallMutationBase: <TFnToParams extends FnToParamsBase, TData =
|
|
|
65
132
|
reset: () => void;
|
|
66
133
|
error: TError | null;
|
|
67
134
|
};
|
|
68
|
-
declare function buildMutationHook<TFnToParams extends FnToParamsBase>(
|
|
135
|
+
declare function buildMutationHook<TFnToParams extends FnToParamsBase>(promiseFunc: TPromiseFunction<unknown, unknown>, genTargetService: (machine: string | undefined) => string): <TFn extends keyof TFnToParams>(func: TFn, options?: Parameters<typeof useCallMutationBase<TFnToParams, TFnToParams[TFn]["data"], TFnToParams[TFn]["error"], TFnToParams[TFn]["params"]>>[3] & {
|
|
69
136
|
machine?: string;
|
|
70
137
|
}) => {
|
|
71
138
|
mutateAsync: (params: TFnToParams[TFn]["params"], overrideTargetMachine?: string) => Promise<TFnToParams[TFn]["data"]>;
|
|
@@ -77,11 +144,11 @@ declare function buildMutationHook<TFnToParams extends FnToParamsBase>(socketCli
|
|
|
77
144
|
reset: () => void;
|
|
78
145
|
error: TFnToParams[TFn]["error"] | null;
|
|
79
146
|
};
|
|
80
|
-
declare const useCallQueryBase: <TFnToParams extends FnToParamsBase, TData = unknown, TError = unknown, TParams = unknown, TSelect = TData>(
|
|
147
|
+
declare const useCallQueryBase: <TFnToParams extends FnToParamsBase, TData = unknown, TError = unknown, TParams = unknown, TSelect = TData>(promiseFunction: TPromiseFunction<TData, TParams>, functionName: keyof TFnToParams, params: TParams, service: string, options?: Omit<QueryOptions<TData, TError, TParams, TSelect>, "queryFn"> & {
|
|
81
148
|
avoidTimeout?: boolean;
|
|
82
149
|
customTimeout?: number;
|
|
83
150
|
}) => UseQueryResult<TSelect, TError, TParams, TSelect, undefined>;
|
|
84
|
-
declare function buildQueryHook<TFnToParams extends FnToParamsBase>(
|
|
151
|
+
declare function buildQueryHook<TFnToParams extends FnToParamsBase>(promiseFunction: TPromiseFunction<unknown, unknown>, getTargetService: (machine: string | undefined) => string): <TFn extends keyof TFnToParams, TSelect = TFnToParams[TFn]["data"]>(func: TFn, params: TFnToParams[TFn]["params"], options?: Parameters<typeof useCallQueryBase<TFnToParams, TFnToParams[TFn]["data"], TFnToParams[TFn]["error"], TFnToParams[TFn]["params"], TSelect>>[4] & {
|
|
85
152
|
machine?: string;
|
|
86
153
|
}) => UseQueryResult<TSelect, TFnToParams[TFn]["error"], TFnToParams[TFn]["params"], TSelect, undefined>;
|
|
87
154
|
|
|
@@ -153,6 +220,19 @@ type WithData<TData> = {
|
|
|
153
220
|
type SubscriptionCoreReturnType<TData, Stateless extends true | undefined> = {
|
|
154
221
|
reSubscribe: () => void;
|
|
155
222
|
} & (Stateless extends true ? {} : WithStatus & WithData<TData>);
|
|
223
|
+
/**
|
|
224
|
+
* @description `useSubscriptionCore` | This hook handle core subscription logic
|
|
225
|
+
* and provides a way to subscribe and unsubscribe to a topic via any callback.
|
|
226
|
+
* The hook expose a handler function. All the consumer code need to do is to
|
|
227
|
+
* call this handler function with the message received from the topic or pass
|
|
228
|
+
* it directly to the callback function that is passed to the subscriberFn.
|
|
229
|
+
* - `status` - current status of the subscription
|
|
230
|
+
* - `data` - the data after the select function is applied to the message
|
|
231
|
+
* - `reSubscribe` - function that can be called to re-subscribe to the topic
|
|
232
|
+
*
|
|
233
|
+
* An example how this hook works can be found in {@link ./useSubscription.ts}
|
|
234
|
+
*/
|
|
235
|
+
declare function useSubscriptionCore<TData = unknown, TMessage = TData, Stateless extends true | undefined = undefined>(options: SubscriptionCoreOptions<TData, TMessage, Stateless>): SubscriptionCoreReturnType<TData, Stateless>;
|
|
156
236
|
|
|
157
237
|
type SubscriptionOptions<TData, TMessage = unknown, Stateless extends true | undefined = undefined> = Omit<SubscriptionCoreOptions<TData, TMessage, Stateless>, "subscriberFn"> & {
|
|
158
238
|
service: string;
|
|
@@ -169,11 +249,17 @@ declare function useSubscription<TData = unknown, TMessage = TData, Stateless ex
|
|
|
169
249
|
declare function buildServiceSubscriptionHook(subscriptionHandler: TSubscriptionHandlerFunction<unknown>, targetMachine: (machine: string | undefined) => string): <TData, TMessage = TData, Stateless extends true | undefined = undefined>(options: Omit<SubscriptionOptions<TData, TMessage, Stateless>, "service"> & {
|
|
170
250
|
machine?: string;
|
|
171
251
|
}) => SubscriptionCoreReturnType<TData, Stateless>;
|
|
252
|
+
|
|
172
253
|
declare function bindClientSocketProxy(socketClientProxy: ZMQProxyWSClient): (opts: {
|
|
173
254
|
service: string;
|
|
174
255
|
name: string;
|
|
175
256
|
callback: (message: unknown) => void;
|
|
176
257
|
maxDownwardDepth?: number;
|
|
177
258
|
}) => () => void;
|
|
259
|
+
declare function bindPromiseFunction(clientSocketProxy: ZMQProxyWSClient): (args: Omit<_pixotope_zmq_client.ZMQClient.Call<unknown, {
|
|
260
|
+
functionName: string;
|
|
261
|
+
parameters: unknown;
|
|
262
|
+
options?: Omit<_pixotope_zmq_client.TSocketServerCallOptions, "toCallIfNoResponse">;
|
|
263
|
+
}>, "id">) => Promise<unknown>;
|
|
178
264
|
|
|
179
|
-
export { type MutationInfo, type QueryOptions, type SubscriptionOptions, type TSubscriptionHandlerFunction, type UseQueryResult, bindClientSocketProxy, buildMutationHook, buildQueryHook, buildServiceSubscriptionHook, useQuery, useSubscription };
|
|
265
|
+
export { ComparisonFailError, type Connection, type MessageHandler, type MutateOptions, type MutationFunction, type MutationInfo, type MutationOptions, type QueryOptions, type SubscriptionCoreOptions, type SubscriptionCoreReturnType, type SubscriptionOptions, type TPromiseFunction, type TSubscriptionHandlerFunction, type UseQueryResult, bindClientSocketProxy, bindPromiseFunction, buildMutationHook, buildQueryHook, buildServiceSubscriptionHook, useMutation, useQuery, useSubscription, useSubscriptionCore };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as _pixotope_zmq_client from '@pixotope/zmq-client';
|
|
2
|
+
import { ZMQClient, TSocketServerCallOptions, ZMQProxyWSClient } from '@pixotope/zmq-client';
|
|
2
3
|
|
|
3
4
|
interface QueryOptions<TData = unknown, TError = unknown, TParams = void, TSelectData = TData, Stateless extends true | undefined = undefined> {
|
|
4
5
|
queryFn: (params: TParams | undefined | void) => Promise<TData>;
|
|
@@ -45,6 +46,67 @@ interface MutateOptions<TData = unknown, TError = unknown, TParams = void> {
|
|
|
45
46
|
onError?: (error: TError, variables: TParams) => void;
|
|
46
47
|
onSettled?: (data: TData | undefined, error: TError | null, variables: TParams) => void;
|
|
47
48
|
}
|
|
49
|
+
type MutationStatus = "idle" | "loading" | "success" | "error";
|
|
50
|
+
declare class ComparisonFailError<TPayload = unknown> extends Error {
|
|
51
|
+
readonly payload: TPayload;
|
|
52
|
+
constructor(message: string, payload: TPayload);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* `useMutation` is a hook that allows you to perform a mutation/push updates to the services while
|
|
56
|
+
* providing extensive API to tap into the status and results of the mutation.
|
|
57
|
+
* @param mutationFn The mutation function to be called when the `mutate` function is called.
|
|
58
|
+
* `useMutation` does not care about what the function does, all it needs it a function that returns
|
|
59
|
+
* a promise.
|
|
60
|
+
* @param options Options to configure the mutation.
|
|
61
|
+
* @returns An object with the following properties:
|
|
62
|
+
* - `mutateAsync`: A function that takes in the variables and returns a promise that resolves to the
|
|
63
|
+
* result of the mutation. This function can be awaited to get the result of the mutation. Callbacks can
|
|
64
|
+
* be passed to this function to be called when the mutation is successful or when it fails. It's good idea
|
|
65
|
+
* to wrap this function in a try/catch block.
|
|
66
|
+
* - `mutate`: A function that takes in the variables and returns nothing. This function does not
|
|
67
|
+
* return a promise. Callbacks can be passed to this function to be called when the mutation is
|
|
68
|
+
* successful or when it fails.
|
|
69
|
+
* - `status`: The status of the mutation. Can be one of `idle`, `loading`, `success` or `error`.
|
|
70
|
+
* - `result`: The result of the mutation. This is undefined until the mutation is successful.
|
|
71
|
+
* - `reset`: A function that resets the mutation to the initial state.
|
|
72
|
+
* - `error`: The error object if the mutation has failed.
|
|
73
|
+
* @example
|
|
74
|
+
* ```typescript
|
|
75
|
+
* const mutationRes = useMutation({
|
|
76
|
+
* mutationFn: (variables) => {
|
|
77
|
+
* return ZMQHelper.callSomethingAsync(variables)
|
|
78
|
+
* },
|
|
79
|
+
* onSuccess: (data) => {
|
|
80
|
+
* // do something with the result
|
|
81
|
+
* },
|
|
82
|
+
* onError: (error) => {
|
|
83
|
+
* // do something with the error
|
|
84
|
+
* },
|
|
85
|
+
* onSettled: (data, error) => {
|
|
86
|
+
* // do something when the mutation is done
|
|
87
|
+
* }});
|
|
88
|
+
*
|
|
89
|
+
* // call the mutation
|
|
90
|
+
* const res = await mutationRes.mutateAsync({ foo: "bar" });
|
|
91
|
+
* // OR
|
|
92
|
+
* mutationRes.mutate({ foo: "bar" }, {
|
|
93
|
+
* onSuccess: (data) => {
|
|
94
|
+
* // do something with the result
|
|
95
|
+
* // for example show a toast message
|
|
96
|
+
* // stop local loading state
|
|
97
|
+
* });
|
|
98
|
+
* ```
|
|
99
|
+
*/
|
|
100
|
+
declare const useMutation: <TData = unknown, TError = unknown, TParams = void>(options: MutationOptions<TData, TError, TParams>) => {
|
|
101
|
+
mutateAsync: (params: TParams, overrideTargetMachine?: string) => Promise<TData>;
|
|
102
|
+
status: MutationStatus;
|
|
103
|
+
result: TData | undefined;
|
|
104
|
+
mutate: (variables: TParams, options?: MutateOptions<TData, TError, TParams> & {
|
|
105
|
+
overrideTargetMachine?: string;
|
|
106
|
+
}) => void;
|
|
107
|
+
reset: () => void;
|
|
108
|
+
error: TError | null;
|
|
109
|
+
};
|
|
48
110
|
|
|
49
111
|
type MutationInfo<TData, TError, TParams> = {
|
|
50
112
|
data: TData;
|
|
@@ -52,7 +114,12 @@ type MutationInfo<TData, TError, TParams> = {
|
|
|
52
114
|
params: TParams;
|
|
53
115
|
};
|
|
54
116
|
type FnToParamsBase = Record<string, MutationInfo<unknown, unknown, unknown>>;
|
|
55
|
-
|
|
117
|
+
type TPromiseFunction<TResponse, TParams> = (args: Omit<ZMQClient.Call<TResponse, {
|
|
118
|
+
functionName: string;
|
|
119
|
+
parameters: TParams;
|
|
120
|
+
options?: Omit<TSocketServerCallOptions, "toCallIfNoResponse">;
|
|
121
|
+
}>, "id">) => Promise<TResponse>;
|
|
122
|
+
declare const useCallMutationBase: <TFnToParams extends FnToParamsBase, TData = unknown, TError = unknown, TParams = unknown>(promiseFunc: TPromiseFunction<TData, TParams>, functionName: keyof TFnToParams, service: string, options?: Omit<MutationOptions<TData, TError, TParams>, "mutationFn"> & {
|
|
56
123
|
avoidTimeout?: boolean;
|
|
57
124
|
customTimeout?: number;
|
|
58
125
|
}) => {
|
|
@@ -65,7 +132,7 @@ declare const useCallMutationBase: <TFnToParams extends FnToParamsBase, TData =
|
|
|
65
132
|
reset: () => void;
|
|
66
133
|
error: TError | null;
|
|
67
134
|
};
|
|
68
|
-
declare function buildMutationHook<TFnToParams extends FnToParamsBase>(
|
|
135
|
+
declare function buildMutationHook<TFnToParams extends FnToParamsBase>(promiseFunc: TPromiseFunction<unknown, unknown>, genTargetService: (machine: string | undefined) => string): <TFn extends keyof TFnToParams>(func: TFn, options?: Parameters<typeof useCallMutationBase<TFnToParams, TFnToParams[TFn]["data"], TFnToParams[TFn]["error"], TFnToParams[TFn]["params"]>>[3] & {
|
|
69
136
|
machine?: string;
|
|
70
137
|
}) => {
|
|
71
138
|
mutateAsync: (params: TFnToParams[TFn]["params"], overrideTargetMachine?: string) => Promise<TFnToParams[TFn]["data"]>;
|
|
@@ -77,11 +144,11 @@ declare function buildMutationHook<TFnToParams extends FnToParamsBase>(socketCli
|
|
|
77
144
|
reset: () => void;
|
|
78
145
|
error: TFnToParams[TFn]["error"] | null;
|
|
79
146
|
};
|
|
80
|
-
declare const useCallQueryBase: <TFnToParams extends FnToParamsBase, TData = unknown, TError = unknown, TParams = unknown, TSelect = TData>(
|
|
147
|
+
declare const useCallQueryBase: <TFnToParams extends FnToParamsBase, TData = unknown, TError = unknown, TParams = unknown, TSelect = TData>(promiseFunction: TPromiseFunction<TData, TParams>, functionName: keyof TFnToParams, params: TParams, service: string, options?: Omit<QueryOptions<TData, TError, TParams, TSelect>, "queryFn"> & {
|
|
81
148
|
avoidTimeout?: boolean;
|
|
82
149
|
customTimeout?: number;
|
|
83
150
|
}) => UseQueryResult<TSelect, TError, TParams, TSelect, undefined>;
|
|
84
|
-
declare function buildQueryHook<TFnToParams extends FnToParamsBase>(
|
|
151
|
+
declare function buildQueryHook<TFnToParams extends FnToParamsBase>(promiseFunction: TPromiseFunction<unknown, unknown>, getTargetService: (machine: string | undefined) => string): <TFn extends keyof TFnToParams, TSelect = TFnToParams[TFn]["data"]>(func: TFn, params: TFnToParams[TFn]["params"], options?: Parameters<typeof useCallQueryBase<TFnToParams, TFnToParams[TFn]["data"], TFnToParams[TFn]["error"], TFnToParams[TFn]["params"], TSelect>>[4] & {
|
|
85
152
|
machine?: string;
|
|
86
153
|
}) => UseQueryResult<TSelect, TFnToParams[TFn]["error"], TFnToParams[TFn]["params"], TSelect, undefined>;
|
|
87
154
|
|
|
@@ -153,6 +220,19 @@ type WithData<TData> = {
|
|
|
153
220
|
type SubscriptionCoreReturnType<TData, Stateless extends true | undefined> = {
|
|
154
221
|
reSubscribe: () => void;
|
|
155
222
|
} & (Stateless extends true ? {} : WithStatus & WithData<TData>);
|
|
223
|
+
/**
|
|
224
|
+
* @description `useSubscriptionCore` | This hook handle core subscription logic
|
|
225
|
+
* and provides a way to subscribe and unsubscribe to a topic via any callback.
|
|
226
|
+
* The hook expose a handler function. All the consumer code need to do is to
|
|
227
|
+
* call this handler function with the message received from the topic or pass
|
|
228
|
+
* it directly to the callback function that is passed to the subscriberFn.
|
|
229
|
+
* - `status` - current status of the subscription
|
|
230
|
+
* - `data` - the data after the select function is applied to the message
|
|
231
|
+
* - `reSubscribe` - function that can be called to re-subscribe to the topic
|
|
232
|
+
*
|
|
233
|
+
* An example how this hook works can be found in {@link ./useSubscription.ts}
|
|
234
|
+
*/
|
|
235
|
+
declare function useSubscriptionCore<TData = unknown, TMessage = TData, Stateless extends true | undefined = undefined>(options: SubscriptionCoreOptions<TData, TMessage, Stateless>): SubscriptionCoreReturnType<TData, Stateless>;
|
|
156
236
|
|
|
157
237
|
type SubscriptionOptions<TData, TMessage = unknown, Stateless extends true | undefined = undefined> = Omit<SubscriptionCoreOptions<TData, TMessage, Stateless>, "subscriberFn"> & {
|
|
158
238
|
service: string;
|
|
@@ -169,11 +249,17 @@ declare function useSubscription<TData = unknown, TMessage = TData, Stateless ex
|
|
|
169
249
|
declare function buildServiceSubscriptionHook(subscriptionHandler: TSubscriptionHandlerFunction<unknown>, targetMachine: (machine: string | undefined) => string): <TData, TMessage = TData, Stateless extends true | undefined = undefined>(options: Omit<SubscriptionOptions<TData, TMessage, Stateless>, "service"> & {
|
|
170
250
|
machine?: string;
|
|
171
251
|
}) => SubscriptionCoreReturnType<TData, Stateless>;
|
|
252
|
+
|
|
172
253
|
declare function bindClientSocketProxy(socketClientProxy: ZMQProxyWSClient): (opts: {
|
|
173
254
|
service: string;
|
|
174
255
|
name: string;
|
|
175
256
|
callback: (message: unknown) => void;
|
|
176
257
|
maxDownwardDepth?: number;
|
|
177
258
|
}) => () => void;
|
|
259
|
+
declare function bindPromiseFunction(clientSocketProxy: ZMQProxyWSClient): (args: Omit<_pixotope_zmq_client.ZMQClient.Call<unknown, {
|
|
260
|
+
functionName: string;
|
|
261
|
+
parameters: unknown;
|
|
262
|
+
options?: Omit<_pixotope_zmq_client.TSocketServerCallOptions, "toCallIfNoResponse">;
|
|
263
|
+
}>, "id">) => Promise<unknown>;
|
|
178
264
|
|
|
179
|
-
export { type MutationInfo, type QueryOptions, type SubscriptionOptions, type TSubscriptionHandlerFunction, type UseQueryResult, bindClientSocketProxy, buildMutationHook, buildQueryHook, buildServiceSubscriptionHook, useQuery, useSubscription };
|
|
265
|
+
export { ComparisonFailError, type Connection, type MessageHandler, type MutateOptions, type MutationFunction, type MutationInfo, type MutationOptions, type QueryOptions, type SubscriptionCoreOptions, type SubscriptionCoreReturnType, type SubscriptionOptions, type TPromiseFunction, type TSubscriptionHandlerFunction, type UseQueryResult, bindClientSocketProxy, bindPromiseFunction, buildMutationHook, buildQueryHook, buildServiceSubscriptionHook, useMutation, useQuery, useSubscription, useSubscriptionCore };
|
package/dist/index.js
CHANGED
|
@@ -264,6 +264,7 @@ import { useCallback as useCallback2, useEffect, useRef as useRef2, useState as
|
|
|
264
264
|
function normalizeQueryKey(queryKey) {
|
|
265
265
|
return JSON.stringify(queryKey);
|
|
266
266
|
}
|
|
267
|
+
var getRandomUUID = () => Math.random().toString(36).substring(2, 15);
|
|
267
268
|
var useQuery = (_a) => {
|
|
268
269
|
var _b = _a, {
|
|
269
270
|
queryFn
|
|
@@ -285,7 +286,6 @@ var useQuery = (_a) => {
|
|
|
285
286
|
key: queryKeyPassed
|
|
286
287
|
} = options;
|
|
287
288
|
const queryKey = normalizeQueryKey(queryKeyPassed != null ? queryKeyPassed : []);
|
|
288
|
-
const promiseResolutionInProgress = useRef2(false);
|
|
289
289
|
const lastFetchedAt = useRef2(null);
|
|
290
290
|
const lastQueryKey = useRef2(void 0);
|
|
291
291
|
const queryFnRef = useRef2(queryFn);
|
|
@@ -314,6 +314,7 @@ var useQuery = (_a) => {
|
|
|
314
314
|
stateless,
|
|
315
315
|
timeout
|
|
316
316
|
};
|
|
317
|
+
const requestIdRef = useRef2(getRandomUUID());
|
|
317
318
|
const [status, setStatus] = useState2(() => {
|
|
318
319
|
return "idle";
|
|
319
320
|
});
|
|
@@ -346,7 +347,9 @@ var useQuery = (_a) => {
|
|
|
346
347
|
const refetch = useCallback2(
|
|
347
348
|
(params = void 0) => __async(null, null, function* () {
|
|
348
349
|
var _a2, _b2, _c, _d, _e, _f, _g, _h;
|
|
349
|
-
|
|
350
|
+
const requestId = getRandomUUID();
|
|
351
|
+
requestIdRef.current = requestId;
|
|
352
|
+
if (!internalOptionsRef.current.enabled) {
|
|
350
353
|
return;
|
|
351
354
|
}
|
|
352
355
|
setStatus("loading");
|
|
@@ -363,9 +366,10 @@ var useQuery = (_a) => {
|
|
|
363
366
|
)
|
|
364
367
|
] : []
|
|
365
368
|
];
|
|
366
|
-
promiseResolutionInProgress.current = true;
|
|
367
369
|
const res = yield Promise.race(promises);
|
|
368
|
-
|
|
370
|
+
if (requestIdRef.current !== requestId) {
|
|
371
|
+
return;
|
|
372
|
+
}
|
|
369
373
|
if (res === "timeout") {
|
|
370
374
|
throw new Error("timeout");
|
|
371
375
|
}
|
|
@@ -393,36 +397,17 @@ var useQuery = (_a) => {
|
|
|
393
397
|
refetch();
|
|
394
398
|
}, [refetch]);
|
|
395
399
|
useEffect(() => {
|
|
396
|
-
|
|
397
|
-
refetch();
|
|
398
|
-
}
|
|
399
|
-
}, [enabled, refetch, queryKey]);
|
|
400
|
-
useEffect(() => {
|
|
401
|
-
if (lastQueryKey.current === void 0) {
|
|
402
|
-
lastQueryKey.current = queryKey;
|
|
403
|
-
refetch();
|
|
404
|
-
return;
|
|
405
|
-
}
|
|
406
|
-
if (queryKey !== lastQueryKey.current) {
|
|
407
|
-
lastQueryKey.current = queryKey;
|
|
408
|
-
refetch();
|
|
409
|
-
}
|
|
410
|
-
}, [queryKey, refetch, enabled]);
|
|
411
|
-
useEffect(() => {
|
|
400
|
+
const cleanups = [];
|
|
412
401
|
if (refetchOnWindowFocus) {
|
|
413
402
|
const queryHandleFocus = () => {
|
|
414
|
-
|
|
415
|
-
refetch();
|
|
416
|
-
}
|
|
403
|
+
refetch();
|
|
417
404
|
};
|
|
418
405
|
window.addEventListener("focus", queryHandleFocus);
|
|
419
|
-
|
|
406
|
+
cleanups.push(() => {
|
|
420
407
|
window.removeEventListener("focus", queryHandleFocus);
|
|
421
|
-
};
|
|
408
|
+
});
|
|
422
409
|
}
|
|
423
|
-
|
|
424
|
-
useEffect(() => {
|
|
425
|
-
if (refetchInterval !== 0) {
|
|
410
|
+
if (refetchInterval > 0) {
|
|
426
411
|
const interval = setInterval(
|
|
427
412
|
() => {
|
|
428
413
|
if (shouldRefetch()) {
|
|
@@ -431,11 +416,25 @@ var useQuery = (_a) => {
|
|
|
431
416
|
},
|
|
432
417
|
Math.max(refetchInterval, 1e3)
|
|
433
418
|
);
|
|
434
|
-
|
|
419
|
+
cleanups.push(() => {
|
|
435
420
|
clearInterval(interval);
|
|
436
|
-
};
|
|
421
|
+
});
|
|
422
|
+
}
|
|
423
|
+
if (lastQueryKey.current === void 0 || queryKey !== lastQueryKey.current) {
|
|
424
|
+
lastQueryKey.current = queryKey;
|
|
425
|
+
refetch();
|
|
437
426
|
}
|
|
438
|
-
|
|
427
|
+
return () => {
|
|
428
|
+
cleanups.forEach((cleanup) => cleanup());
|
|
429
|
+
};
|
|
430
|
+
}, [
|
|
431
|
+
queryKey,
|
|
432
|
+
enabled,
|
|
433
|
+
refetchOnWindowFocus,
|
|
434
|
+
refetchInterval,
|
|
435
|
+
shouldRefetch,
|
|
436
|
+
refetch
|
|
437
|
+
]);
|
|
439
438
|
return {
|
|
440
439
|
data: select && data !== void 0 ? select(data) : data,
|
|
441
440
|
error,
|
|
@@ -446,7 +445,7 @@ var useQuery = (_a) => {
|
|
|
446
445
|
};
|
|
447
446
|
|
|
448
447
|
// src/factories.ts
|
|
449
|
-
var useCallMutationBase = (
|
|
448
|
+
var useCallMutationBase = (promiseFunc, functionName, service, options) => {
|
|
450
449
|
const _a = useMemo(() => options != null ? options : {}, [options]), {
|
|
451
450
|
avoidTimeout = true,
|
|
452
451
|
customTimeout = void 0
|
|
@@ -457,7 +456,7 @@ var useCallMutationBase = (clientProxy, functionName, service, options) => {
|
|
|
457
456
|
const mutationFn = useCallback3(
|
|
458
457
|
(parameters) => __async(null, null, function* () {
|
|
459
458
|
try {
|
|
460
|
-
|
|
459
|
+
return yield promiseFunc({
|
|
461
460
|
service,
|
|
462
461
|
functionName: functionName.toString(),
|
|
463
462
|
parameters,
|
|
@@ -466,7 +465,6 @@ var useCallMutationBase = (clientProxy, functionName, service, options) => {
|
|
|
466
465
|
customTimeout
|
|
467
466
|
}
|
|
468
467
|
});
|
|
469
|
-
return res.message.Result;
|
|
470
468
|
} catch (e) {
|
|
471
469
|
throw e.message;
|
|
472
470
|
}
|
|
@@ -477,13 +475,13 @@ var useCallMutationBase = (clientProxy, functionName, service, options) => {
|
|
|
477
475
|
mutationFn
|
|
478
476
|
}, rest));
|
|
479
477
|
};
|
|
480
|
-
function buildMutationHook(
|
|
478
|
+
function buildMutationHook(promiseFunc, genTargetService) {
|
|
481
479
|
return function useCallMutationBuilder(func, options) {
|
|
482
480
|
const targetService = genTargetService(options == null ? void 0 : options.machine);
|
|
483
|
-
return useCallMutationBase(
|
|
481
|
+
return useCallMutationBase(promiseFunc, func, targetService, options);
|
|
484
482
|
};
|
|
485
483
|
}
|
|
486
|
-
var useCallQueryBase = (
|
|
484
|
+
var useCallQueryBase = (promiseFunction, functionName, params, service, options) => {
|
|
487
485
|
const _a = useMemo(() => options != null ? options : {}, [options]), {
|
|
488
486
|
avoidTimeout = true,
|
|
489
487
|
customTimeout = void 0
|
|
@@ -494,7 +492,7 @@ var useCallQueryBase = (clientProxy, functionName, params, service, options) =>
|
|
|
494
492
|
const queryFn = useCallback3(
|
|
495
493
|
(refetchParams, overrideTargetMachine) => __async(null, null, function* () {
|
|
496
494
|
try {
|
|
497
|
-
|
|
495
|
+
return yield promiseFunction({
|
|
498
496
|
service: overrideTargetMachine != null ? overrideTargetMachine : service,
|
|
499
497
|
functionName: functionName.toString(),
|
|
500
498
|
parameters: refetchParams != null ? refetchParams : params,
|
|
@@ -503,7 +501,6 @@ var useCallQueryBase = (clientProxy, functionName, params, service, options) =>
|
|
|
503
501
|
customTimeout
|
|
504
502
|
}
|
|
505
503
|
});
|
|
506
|
-
return res.message.Result;
|
|
507
504
|
} catch (e) {
|
|
508
505
|
if (e instanceof Error) {
|
|
509
506
|
throw e;
|
|
@@ -517,10 +514,10 @@ var useCallQueryBase = (clientProxy, functionName, params, service, options) =>
|
|
|
517
514
|
queryFn
|
|
518
515
|
}, rest));
|
|
519
516
|
};
|
|
520
|
-
function buildQueryHook(
|
|
517
|
+
function buildQueryHook(promiseFunction, getTargetService) {
|
|
521
518
|
return function useCallQueryBuilder(func, params, options) {
|
|
522
519
|
const targetService = getTargetService(options == null ? void 0 : options.machine);
|
|
523
|
-
return useCallQueryBase(
|
|
520
|
+
return useCallQueryBase(promiseFunction, func, params, targetService, options);
|
|
524
521
|
};
|
|
525
522
|
}
|
|
526
523
|
|
|
@@ -660,15 +657,34 @@ function buildServiceSubscriptionHook(subscriptionHandler, targetMachine) {
|
|
|
660
657
|
);
|
|
661
658
|
};
|
|
662
659
|
}
|
|
660
|
+
|
|
661
|
+
// src/proxyBinders.ts
|
|
663
662
|
function bindClientSocketProxy(socketClientProxy) {
|
|
664
|
-
return (...args) =>
|
|
663
|
+
return (...args) => {
|
|
664
|
+
const _a = args[0], { callback: cbHook } = _a, rest = __objRest(_a, ["callback"]);
|
|
665
|
+
return socketClientProxy.mirror(__spreadProps(__spreadValues({}, rest), {
|
|
666
|
+
callback: (message) => {
|
|
667
|
+
cbHook(message.message);
|
|
668
|
+
}
|
|
669
|
+
}))[0];
|
|
670
|
+
};
|
|
671
|
+
}
|
|
672
|
+
function bindPromiseFunction(clientSocketProxy) {
|
|
673
|
+
return (...args) => __async(null, null, function* () {
|
|
674
|
+
const res = yield clientSocketProxy.callAsync(...args);
|
|
675
|
+
return res.message.Result;
|
|
676
|
+
});
|
|
665
677
|
}
|
|
666
678
|
export {
|
|
679
|
+
ComparisonFailError,
|
|
667
680
|
bindClientSocketProxy,
|
|
681
|
+
bindPromiseFunction,
|
|
668
682
|
buildMutationHook,
|
|
669
683
|
buildQueryHook,
|
|
670
684
|
buildServiceSubscriptionHook,
|
|
685
|
+
useMutation,
|
|
671
686
|
useQuery,
|
|
672
|
-
useSubscription
|
|
687
|
+
useSubscription,
|
|
688
|
+
useSubscriptionCore
|
|
673
689
|
};
|
|
674
690
|
//# sourceMappingURL=index.js.map
|