@pixotope/query 0.6.0 → 0.8.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 +11 -7
- package/CHANGELOG.md +13 -0
- package/dist/index.cjs +68 -71
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +50 -91
- package/dist/index.d.ts +50 -91
- package/dist/index.js +68 -71
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
- package/src/factories.test.ts +65 -0
- package/src/factories.ts +48 -31
- package/src/proxyBinders.test.ts +42 -0
- package/src/proxyBinders.ts +8 -5
- package/src/useMutation.test.ts +106 -0
- package/src/useMutation.ts +87 -116
- package/src/useQuery.test.ts +77 -139
- package/src/useQuery.ts +5 -8
- package/src/useSubscription.test.ts +79 -0
- package/src/useSubscription.ts +23 -13
- package/src/useSubscriptionCore.ts +6 -3
- package/src/mock/useQuery.ts +0 -48
package/src/useQuery.test.ts
CHANGED
|
@@ -1,210 +1,162 @@
|
|
|
1
1
|
import { fireEvent } from "@testing-library/dom";
|
|
2
2
|
import { act, renderHook, waitFor } from "@testing-library/react";
|
|
3
|
-
import { vi, describe,
|
|
3
|
+
import { vi, describe, it, expect, test } from "vitest";
|
|
4
4
|
|
|
5
5
|
import { useQuery } from "./useQuery";
|
|
6
|
-
import { MockUseQueryUtils } from "./mock/useQuery";
|
|
7
6
|
const mockQueryKey = ["dummyQK"];
|
|
8
7
|
|
|
9
|
-
function until(condition: boolean) {
|
|
10
|
-
return new Promise((resolve, reject) => {
|
|
11
|
-
if (condition) {
|
|
12
|
-
resolve(null);
|
|
13
|
-
} else {
|
|
14
|
-
reject();
|
|
15
|
-
}
|
|
16
|
-
});
|
|
17
|
-
}
|
|
18
|
-
|
|
19
8
|
describe("useQuery", () => {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
useQuery({
|
|
30
|
-
queryFn: mockFunction,
|
|
31
|
-
key: mockQueryKey,
|
|
32
|
-
})
|
|
9
|
+
it("should call queryFn by on mount by default", async () => {
|
|
10
|
+
const mockFunction = vi.fn();
|
|
11
|
+
await act(() =>
|
|
12
|
+
renderHook(() =>
|
|
13
|
+
useQuery({
|
|
14
|
+
queryFn: mockFunction,
|
|
15
|
+
key: mockQueryKey,
|
|
16
|
+
})
|
|
17
|
+
)
|
|
33
18
|
);
|
|
34
|
-
|
|
35
19
|
expect(mockFunction).toBeCalledTimes(1);
|
|
36
20
|
});
|
|
37
|
-
it("should be in loading state on mount", () => {
|
|
21
|
+
it("should be in loading state on mount", async () => {
|
|
38
22
|
const { result } = renderHook(() =>
|
|
39
23
|
useQuery({
|
|
40
|
-
queryFn:
|
|
24
|
+
queryFn: vi.fn(),
|
|
41
25
|
key: mockQueryKey,
|
|
42
26
|
})
|
|
43
27
|
);
|
|
44
28
|
|
|
45
|
-
expect(result.current.status).toBe("loading");
|
|
29
|
+
await waitFor(() => expect(result.current.status).toBe("loading"));
|
|
46
30
|
});
|
|
47
31
|
it("should be in success state after queryFn resolves", async () => {
|
|
32
|
+
const mockFunction = vi.fn();
|
|
48
33
|
const { result } = renderHook(() =>
|
|
49
34
|
useQuery({
|
|
50
|
-
queryFn:
|
|
35
|
+
queryFn: mockFunction,
|
|
51
36
|
key: mockQueryKey,
|
|
52
37
|
})
|
|
53
38
|
);
|
|
54
39
|
act(() => {
|
|
55
|
-
|
|
56
|
-
});
|
|
57
|
-
await waitFor(() => until(result.current.status === "success"), {
|
|
58
|
-
timeout: 1000,
|
|
40
|
+
mockFunction.mockResolvedValue("test");
|
|
59
41
|
});
|
|
42
|
+
await waitFor(() => expect(result.current.status).toBe("success"));
|
|
60
43
|
});
|
|
61
44
|
it("should be in error state after queryFn rejects", async () => {
|
|
45
|
+
const mockFunction = vi.fn().mockRejectedValue("test");
|
|
62
46
|
const { result } = renderHook(() =>
|
|
63
47
|
useQuery({
|
|
64
|
-
queryFn:
|
|
48
|
+
queryFn: mockFunction,
|
|
65
49
|
key: mockQueryKey,
|
|
66
50
|
})
|
|
67
51
|
);
|
|
68
|
-
|
|
69
|
-
await waitFor(() => until(result.current.status === "error"), {
|
|
70
|
-
timeout: 1000,
|
|
71
|
-
});
|
|
72
|
-
expect(result.current.status).toBe("error");
|
|
52
|
+
await waitFor(() => expect(result.current.status).toBe("error"));
|
|
73
53
|
});
|
|
74
54
|
it("should be in timeout state after queryFn rejects with timeout", async () => {
|
|
55
|
+
const mockFunction = vi.fn(
|
|
56
|
+
() => new Promise((_, reject) => setTimeout(() => reject("test"), 10))
|
|
57
|
+
);
|
|
75
58
|
const { result } = renderHook(() =>
|
|
76
59
|
useQuery({
|
|
77
|
-
queryFn:
|
|
60
|
+
queryFn: mockFunction,
|
|
78
61
|
key: mockQueryKey,
|
|
79
62
|
timeout: 0.01,
|
|
80
63
|
})
|
|
81
64
|
);
|
|
82
|
-
await waitFor(() =>
|
|
83
|
-
timeout: 1000,
|
|
84
|
-
});
|
|
85
|
-
expect(result.current.status === "error");
|
|
65
|
+
await waitFor(() => expect(result.current.status).toBe("error"));
|
|
86
66
|
});
|
|
87
67
|
it("should be in success state after queryFn resolves with initialData", async () => {
|
|
88
68
|
const { result } = renderHook(() =>
|
|
89
69
|
useQuery({
|
|
90
|
-
queryFn: ()
|
|
70
|
+
queryFn: vi.fn().mockResolvedValue("test-test-2"),
|
|
91
71
|
key: mockQueryKey,
|
|
92
72
|
initialData: "test-test",
|
|
93
73
|
})
|
|
94
74
|
);
|
|
95
|
-
|
|
96
|
-
await waitFor(() => until(result.current.status === "success"), {
|
|
97
|
-
timeout: 1000,
|
|
98
|
-
});
|
|
75
|
+
await waitFor(() => expect(result.current.status).toBe("success"));
|
|
99
76
|
});
|
|
100
77
|
it("should be in idle state if enabled is false", async () => {
|
|
101
78
|
const { result } = renderHook(() =>
|
|
102
79
|
useQuery({
|
|
103
|
-
queryFn:
|
|
80
|
+
queryFn: vi.fn(),
|
|
104
81
|
key: mockQueryKey,
|
|
105
82
|
enabled: false,
|
|
106
83
|
})
|
|
107
84
|
);
|
|
108
|
-
expect(result.current.status).toBe("idle");
|
|
85
|
+
await waitFor(() => expect(result.current.status).toBe("idle"));
|
|
109
86
|
});
|
|
110
87
|
test("refetch should trigger query function", async () => {
|
|
111
|
-
const resolver = vi.fn(
|
|
112
|
-
const { result } =
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
})
|
|
118
|
-
)
|
|
88
|
+
const resolver = vi.fn().mockResolvedValue("test");
|
|
89
|
+
const { result } = renderHook(() =>
|
|
90
|
+
useQuery({
|
|
91
|
+
queryFn: resolver,
|
|
92
|
+
key: mockQueryKey,
|
|
93
|
+
})
|
|
119
94
|
);
|
|
120
|
-
await
|
|
121
|
-
await
|
|
122
|
-
timeout: 1000,
|
|
123
|
-
});
|
|
124
|
-
await act(() => result.current.refetch());
|
|
95
|
+
await waitFor(() => expect(result.current.status).toBe("success"));
|
|
96
|
+
await act(() => result.current.refetch({}));
|
|
125
97
|
expect(resolver).toBeCalledTimes(2);
|
|
126
98
|
});
|
|
127
|
-
test("select should return selected
|
|
128
|
-
const
|
|
129
|
-
foo: "bar",
|
|
130
|
-
baz: "qux",
|
|
131
|
-
});
|
|
99
|
+
test("select should return correctly selected data", async () => {
|
|
100
|
+
const resolver = vi.fn().mockResolvedValue({ foo: "bar" });
|
|
132
101
|
const { result } = renderHook(() =>
|
|
133
102
|
useQuery({
|
|
134
|
-
queryFn:
|
|
103
|
+
queryFn: resolver,
|
|
135
104
|
key: mockQueryKey,
|
|
136
|
-
select: (data) => data?.foo,
|
|
105
|
+
select: (data: any) => data?.foo,
|
|
137
106
|
})
|
|
138
107
|
);
|
|
139
|
-
expect(result.current.data).
|
|
140
|
-
queryMock.resolve();
|
|
141
|
-
await waitFor(() => until(result.current.data !== undefined), {
|
|
142
|
-
timeout: 1000,
|
|
143
|
-
});
|
|
144
|
-
expect(result.current.data).toBe("bar");
|
|
108
|
+
await waitFor(() => expect(result.current.data).toBe("bar"));
|
|
145
109
|
});
|
|
146
110
|
test("onSuccess should be called after queryFn resolves", async () => {
|
|
147
111
|
const onSuccess = vi.fn();
|
|
148
112
|
renderHook(() =>
|
|
149
113
|
useQuery({
|
|
150
|
-
queryFn: ()
|
|
114
|
+
queryFn: vi.fn().mockResolvedValue("test"),
|
|
151
115
|
key: mockQueryKey,
|
|
152
116
|
onSuccess,
|
|
153
117
|
})
|
|
154
118
|
);
|
|
155
|
-
await
|
|
156
|
-
|
|
157
|
-
timeout: 1000,
|
|
158
|
-
});
|
|
159
|
-
expect(onSuccess).toBeCalledTimes(1);
|
|
119
|
+
await waitFor(() => expect(onSuccess).toBeCalledTimes(1));
|
|
120
|
+
expect(onSuccess).toBeCalledWith("test");
|
|
160
121
|
});
|
|
161
122
|
test("onError should be called after queryFn rejects", async () => {
|
|
162
123
|
const onError = vi.fn();
|
|
163
124
|
renderHook(() =>
|
|
164
125
|
useQuery({
|
|
165
|
-
queryFn: ()
|
|
126
|
+
queryFn: vi.fn().mockRejectedValue("test"),
|
|
166
127
|
key: mockQueryKey,
|
|
167
128
|
onError,
|
|
168
129
|
})
|
|
169
130
|
);
|
|
170
|
-
await
|
|
171
|
-
|
|
172
|
-
timeout: 1000,
|
|
173
|
-
});
|
|
174
|
-
expect(onError).toBeCalledTimes(1);
|
|
131
|
+
await waitFor(() => expect(onError).toBeCalledTimes(1));
|
|
132
|
+
expect(onError).toBeCalledWith("test");
|
|
175
133
|
});
|
|
176
134
|
test("onSettled should be called after queryFn resolves", async () => {
|
|
177
135
|
const onSettled = vi.fn();
|
|
178
136
|
renderHook(() =>
|
|
179
137
|
useQuery({
|
|
180
|
-
queryFn: ()
|
|
138
|
+
queryFn: vi.fn().mockResolvedValue("test"),
|
|
181
139
|
key: mockQueryKey,
|
|
182
140
|
onSettled,
|
|
183
141
|
})
|
|
184
142
|
);
|
|
185
|
-
await
|
|
186
|
-
|
|
187
|
-
timeout: 1000,
|
|
188
|
-
});
|
|
189
|
-
expect(onSettled).toBeCalledTimes(1);
|
|
143
|
+
await waitFor(() => expect(onSettled).toBeCalledTimes(1));
|
|
144
|
+
expect(onSettled).toBeCalledWith("test", undefined);
|
|
190
145
|
});
|
|
191
146
|
test("onSettled should be called after queryFn rejects", async () => {
|
|
192
147
|
const onSettled = vi.fn();
|
|
193
148
|
renderHook(() =>
|
|
194
149
|
useQuery({
|
|
195
|
-
queryFn: ()
|
|
150
|
+
queryFn: vi.fn().mockRejectedValue("test"),
|
|
196
151
|
key: mockQueryKey,
|
|
197
152
|
onSettled,
|
|
198
153
|
})
|
|
199
154
|
);
|
|
200
|
-
await
|
|
201
|
-
|
|
202
|
-
timeout: 1000,
|
|
203
|
-
});
|
|
204
|
-
expect(onSettled).toBeCalledTimes(1);
|
|
155
|
+
await waitFor(() => expect(onSettled).toBeCalledTimes(1));
|
|
156
|
+
expect(onSettled).toBeCalledWith(undefined, "test");
|
|
205
157
|
});
|
|
206
158
|
test("refetch is triggered on window focus", async () => {
|
|
207
|
-
const resolver = vi.fn(
|
|
159
|
+
const resolver = vi.fn().mockResolvedValue("test");
|
|
208
160
|
const { result } = await act(() =>
|
|
209
161
|
renderHook(() =>
|
|
210
162
|
useQuery({
|
|
@@ -214,15 +166,12 @@ describe("useQuery", () => {
|
|
|
214
166
|
})
|
|
215
167
|
)
|
|
216
168
|
);
|
|
217
|
-
await
|
|
218
|
-
await waitFor(() => until(result.current.status === "success"), {
|
|
219
|
-
timeout: 1000,
|
|
220
|
-
});
|
|
169
|
+
await waitFor(() => expect(result.current.status).toBe("success"));
|
|
221
170
|
await act(() => fireEvent(window, new FocusEvent("focus")));
|
|
222
171
|
expect(resolver).toBeCalledTimes(2);
|
|
223
172
|
});
|
|
224
173
|
test("refetch interval should work", async () => {
|
|
225
|
-
const resolver = vi.fn(
|
|
174
|
+
const resolver = vi.fn().mockResolvedValue("test");
|
|
226
175
|
const { result } = renderHook(() =>
|
|
227
176
|
useQuery({
|
|
228
177
|
queryFn: resolver,
|
|
@@ -230,38 +179,31 @@ describe("useQuery", () => {
|
|
|
230
179
|
key: mockQueryKey,
|
|
231
180
|
})
|
|
232
181
|
);
|
|
233
|
-
await
|
|
234
|
-
await waitFor(() =>
|
|
235
|
-
timeout: 1000,
|
|
236
|
-
});
|
|
237
|
-
waitFor(() => until(resolver.call.length > 1), { timeout: 1000 });
|
|
182
|
+
await waitFor(() => expect(result.current.status).toBe("success"));
|
|
183
|
+
await waitFor(() => expect(resolver.mock.calls.length).toBeGreaterThan(1));
|
|
238
184
|
});
|
|
239
185
|
test("refetch should automatically trigger if queryKey is changed", async () => {
|
|
240
|
-
const resolver = vi.fn(
|
|
241
|
-
const { result, rerender } =
|
|
242
|
-
|
|
243
|
-
(
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
{
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
);
|
|
255
|
-
await act(() => defaultQueryMock.resolve());
|
|
256
|
-
await waitFor(() => until(result.current.status === "success"), {
|
|
257
|
-
timeout: 1000,
|
|
258
|
-
});
|
|
186
|
+
const resolver = vi.fn().mockResolvedValue("test");
|
|
187
|
+
const { result, rerender } = renderHook(
|
|
188
|
+
(props) =>
|
|
189
|
+
useQuery({
|
|
190
|
+
queryFn: resolver,
|
|
191
|
+
key: props.key,
|
|
192
|
+
}),
|
|
193
|
+
{
|
|
194
|
+
initialProps: {
|
|
195
|
+
key: ["dummyQK1"],
|
|
196
|
+
},
|
|
197
|
+
}
|
|
198
|
+
);
|
|
199
|
+
await waitFor(() => expect(result.current.status).toBe("success"));
|
|
200
|
+
await waitFor(() => expect(resolver).toBeCalledTimes(1));
|
|
259
201
|
rerender({ key: ["dummyQK2"] });
|
|
260
202
|
rerender({ key: ["dummyQK3"] });
|
|
261
|
-
expect(resolver).toBeCalledTimes(3);
|
|
203
|
+
await waitFor(() => expect(resolver).toBeCalledTimes(3));
|
|
262
204
|
});
|
|
263
205
|
test("stateless mode", async () => {
|
|
264
|
-
const resolver = vi.fn(
|
|
206
|
+
const resolver = vi.fn().mockResolvedValue("test");
|
|
265
207
|
const { result } = renderHook(() =>
|
|
266
208
|
useQuery({
|
|
267
209
|
queryFn: resolver,
|
|
@@ -269,11 +211,7 @@ describe("useQuery", () => {
|
|
|
269
211
|
stateless: true,
|
|
270
212
|
})
|
|
271
213
|
);
|
|
272
|
-
await
|
|
273
|
-
await waitFor(() => until(result.current.status === "success"), {
|
|
274
|
-
timeout: 1000,
|
|
275
|
-
});
|
|
276
|
-
|
|
214
|
+
await waitFor(() => expect(result.current.status).toBe("success"));
|
|
277
215
|
// Here ts should kick in and complain about but for testing purposes, all good
|
|
278
216
|
// @ts-ignore
|
|
279
217
|
expect(result.current.data).toBeUndefined();
|
package/src/useQuery.ts
CHANGED
|
@@ -259,14 +259,11 @@ export const useQuery = <
|
|
|
259
259
|
|
|
260
260
|
// If the query should be refetched on an interval, add an interval
|
|
261
261
|
if (refetchInterval > 0) {
|
|
262
|
-
const interval = setInterval(
|
|
263
|
-
()
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
},
|
|
268
|
-
Math.max(refetchInterval, 1000)
|
|
269
|
-
);
|
|
262
|
+
const interval = setInterval(() => {
|
|
263
|
+
if (shouldRefetch()) {
|
|
264
|
+
refetch();
|
|
265
|
+
}
|
|
266
|
+
}, refetchInterval);
|
|
270
267
|
|
|
271
268
|
cleanups.push(() => {
|
|
272
269
|
clearInterval(interval);
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { renderHook } from "@testing-library/react";
|
|
2
|
+
import { vi, describe, expect, it } from "vitest";
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
buildServiceSubscriptionHook,
|
|
6
|
+
useSubscription,
|
|
7
|
+
} from "./useSubscription";
|
|
8
|
+
|
|
9
|
+
describe("useSubscription", () => {
|
|
10
|
+
it("should call subscription handler", () => {
|
|
11
|
+
const subscriptionHandler = vi.fn();
|
|
12
|
+
renderHook(() =>
|
|
13
|
+
useSubscription(subscriptionHandler, {
|
|
14
|
+
topic: "test",
|
|
15
|
+
service: "test",
|
|
16
|
+
})
|
|
17
|
+
);
|
|
18
|
+
expect(subscriptionHandler).toBeCalledTimes(1);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it("should correctly pass options to subscription handler", () => {
|
|
22
|
+
const subscriptionHandler = vi.fn();
|
|
23
|
+
renderHook(() =>
|
|
24
|
+
useSubscription(subscriptionHandler, {
|
|
25
|
+
topic: "test",
|
|
26
|
+
service: "test",
|
|
27
|
+
meta: { foo: "bar" },
|
|
28
|
+
maxDownwardDepth: 100,
|
|
29
|
+
})
|
|
30
|
+
);
|
|
31
|
+
expect(subscriptionHandler).toBeCalledTimes(1);
|
|
32
|
+
expect(subscriptionHandler).toHaveBeenCalledWith({
|
|
33
|
+
service: "test",
|
|
34
|
+
name: "test",
|
|
35
|
+
callback: expect.any(Function),
|
|
36
|
+
maxDownwardDepth: 100,
|
|
37
|
+
meta: { foo: "bar" },
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
describe("buildServiceSubscriptionHook", () => {
|
|
43
|
+
it("should correctly pass options to subscription handler", () => {
|
|
44
|
+
const subscriptionHandler = vi.fn();
|
|
45
|
+
const useHook = buildServiceSubscriptionHook(
|
|
46
|
+
subscriptionHandler,
|
|
47
|
+
() => "test"
|
|
48
|
+
);
|
|
49
|
+
renderHook(() => useHook({ topic: "test", meta: { foo: "bar" } }));
|
|
50
|
+
expect(subscriptionHandler).toHaveBeenCalledTimes(1);
|
|
51
|
+
expect(subscriptionHandler).toHaveBeenCalledWith(
|
|
52
|
+
expect.objectContaining({
|
|
53
|
+
service: "test",
|
|
54
|
+
name: "test",
|
|
55
|
+
meta: { foo: "bar" },
|
|
56
|
+
})
|
|
57
|
+
);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("should correctly handle overridden machine", () => {
|
|
61
|
+
const subscriptionHandler = vi.fn();
|
|
62
|
+
const useHook = buildServiceSubscriptionHook(
|
|
63
|
+
subscriptionHandler,
|
|
64
|
+
(machine) => machine || "test"
|
|
65
|
+
);
|
|
66
|
+
renderHook(() =>
|
|
67
|
+
useHook({
|
|
68
|
+
topic: "test",
|
|
69
|
+
machine: "myCustomMachineName",
|
|
70
|
+
})
|
|
71
|
+
);
|
|
72
|
+
expect(subscriptionHandler).toHaveBeenCalledWith(
|
|
73
|
+
expect.objectContaining({
|
|
74
|
+
service: "myCustomMachineName",
|
|
75
|
+
name: "test",
|
|
76
|
+
})
|
|
77
|
+
);
|
|
78
|
+
});
|
|
79
|
+
});
|
package/src/useSubscription.ts
CHANGED
|
@@ -7,6 +7,7 @@ export type SubscriptionOptions<
|
|
|
7
7
|
TData,
|
|
8
8
|
TMessage = unknown,
|
|
9
9
|
Stateless extends true | undefined = undefined,
|
|
10
|
+
TMetadata extends {} = {},
|
|
10
11
|
> = Omit<
|
|
11
12
|
SubscriptionCoreOptions<TData, TMessage, Stateless>,
|
|
12
13
|
"subscriberFn"
|
|
@@ -14,27 +15,35 @@ export type SubscriptionOptions<
|
|
|
14
15
|
service: string;
|
|
15
16
|
topic: string;
|
|
16
17
|
maxDownwardDepth?: number;
|
|
18
|
+
} & {
|
|
19
|
+
meta?: TMetadata;
|
|
17
20
|
};
|
|
18
21
|
|
|
19
|
-
export type TSubscriptionHandlerFunction<
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
22
|
+
export type TSubscriptionHandlerFunction<
|
|
23
|
+
TMessage,
|
|
24
|
+
TMetadata extends {} = {},
|
|
25
|
+
> = (
|
|
26
|
+
opts: {
|
|
27
|
+
service: string;
|
|
28
|
+
name: string;
|
|
29
|
+
callback: (message: TMessage) => void;
|
|
30
|
+
maxDownwardDepth?: number;
|
|
31
|
+
} & { meta?: TMetadata }
|
|
32
|
+
) => () => void;
|
|
25
33
|
|
|
26
34
|
export function useSubscription<
|
|
27
35
|
TData = unknown,
|
|
28
36
|
TMessage = TData,
|
|
29
37
|
Stateless extends true | undefined = undefined,
|
|
38
|
+
TMetadata extends {} = {},
|
|
30
39
|
>(
|
|
31
|
-
subscriptionHandler: TSubscriptionHandlerFunction<TMessage>,
|
|
32
|
-
options: SubscriptionOptions<TData, TMessage, Stateless>
|
|
40
|
+
subscriptionHandler: TSubscriptionHandlerFunction<TMessage, TMetadata>,
|
|
41
|
+
options: SubscriptionOptions<TData, TMessage, Stateless, TMetadata>
|
|
33
42
|
) {
|
|
34
|
-
const { topic, service, maxDownwardDepth = 1000 } = options;
|
|
43
|
+
const { topic, service, maxDownwardDepth = 1000, meta, ...rest } = options;
|
|
35
44
|
|
|
36
45
|
return useSubscriptionCore<TData, TMessage, Stateless>({
|
|
37
|
-
...
|
|
46
|
+
...rest,
|
|
38
47
|
subscriberFn: (handler) => {
|
|
39
48
|
const unsubscribe = subscriptionHandler({
|
|
40
49
|
service,
|
|
@@ -43,6 +52,7 @@ export function useSubscription<
|
|
|
43
52
|
handler(message);
|
|
44
53
|
},
|
|
45
54
|
maxDownwardDepth,
|
|
55
|
+
meta,
|
|
46
56
|
});
|
|
47
57
|
|
|
48
58
|
return unsubscribe;
|
|
@@ -50,8 +60,8 @@ export function useSubscription<
|
|
|
50
60
|
});
|
|
51
61
|
}
|
|
52
62
|
|
|
53
|
-
export function buildServiceSubscriptionHook(
|
|
54
|
-
subscriptionHandler: TSubscriptionHandlerFunction<unknown>,
|
|
63
|
+
export function buildServiceSubscriptionHook<TMetadata extends {} = {}>(
|
|
64
|
+
subscriptionHandler: TSubscriptionHandlerFunction<unknown, TMetadata>,
|
|
55
65
|
targetMachine: (machine: string | undefined) => string
|
|
56
66
|
) {
|
|
57
67
|
return function useSubscriptionBuilder<
|
|
@@ -60,7 +70,7 @@ export function buildServiceSubscriptionHook(
|
|
|
60
70
|
Stateless extends true | undefined = undefined,
|
|
61
71
|
>(
|
|
62
72
|
options: Omit<
|
|
63
|
-
SubscriptionOptions<TData, TMessage, Stateless>,
|
|
73
|
+
SubscriptionOptions<TData, TMessage, Stateless, TMetadata>,
|
|
64
74
|
"service"
|
|
65
75
|
> & {
|
|
66
76
|
machine?: string;
|
|
@@ -8,7 +8,8 @@
|
|
|
8
8
|
import { shallowEqual } from "@pixotope/utils/comparison";
|
|
9
9
|
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
10
10
|
|
|
11
|
-
type SubscriptionState = "idle" | "subscribed" | "subscribing";
|
|
11
|
+
type SubscriptionState = "idle" | "subscribed" | "subscribing" | "error";
|
|
12
|
+
|
|
12
13
|
export type MessageHandler<TMessage> = (message: TMessage) => void;
|
|
13
14
|
export type Connection = {
|
|
14
15
|
acknowledge: () => void;
|
|
@@ -36,8 +37,9 @@ export type SubscriptionCoreOptions<
|
|
|
36
37
|
*/
|
|
37
38
|
enabled?: boolean;
|
|
38
39
|
/**
|
|
39
|
-
* @description Function that will be called
|
|
40
|
-
*
|
|
40
|
+
* @description Function that will be called before the message is processed.
|
|
41
|
+
* Returning `true` will cause the message to not be set to state and call {@link onError} if provided.
|
|
42
|
+
* Status will be set to `error`.
|
|
41
43
|
*/
|
|
42
44
|
compareOrThrow?: (data: TMessage) => boolean;
|
|
43
45
|
/**
|
|
@@ -165,6 +167,7 @@ export function useSubscriptionCore<
|
|
|
165
167
|
const handler = useCallback(
|
|
166
168
|
async (message: TMessage) => {
|
|
167
169
|
if (internalCallbacksRef.current.compareOrThrow?.(message)) {
|
|
170
|
+
updateStatus("error");
|
|
168
171
|
internalCallbacksRef.current.onError?.(message);
|
|
169
172
|
|
|
170
173
|
return;
|
package/src/mock/useQuery.ts
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
export class MockPromise<TData = any> {
|
|
2
|
-
private _data: TData;
|
|
3
|
-
private _resolve!: (value: TData) => void;
|
|
4
|
-
private _reject!: (reason?: any) => void;
|
|
5
|
-
public promise: Promise<TData>;
|
|
6
|
-
|
|
7
|
-
public constructor(data: TData) {
|
|
8
|
-
this._data = data;
|
|
9
|
-
this.promise = new Promise((resolve, reject) => {
|
|
10
|
-
this._resolve = resolve;
|
|
11
|
-
this._reject = reject;
|
|
12
|
-
});
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
public async resolve() {
|
|
16
|
-
this._resolve(this._data);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
public async reject() {
|
|
20
|
-
this._reject();
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export class MockUseQueryUtils<TData = any> {
|
|
25
|
-
private _data: TData;
|
|
26
|
-
private _mockPromise: MockPromise<TData>;
|
|
27
|
-
|
|
28
|
-
public constructor(data: TData) {
|
|
29
|
-
this._data = data;
|
|
30
|
-
this._mockPromise = new MockPromise(data);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
public getMockedQueryFn() {
|
|
34
|
-
return this._mockPromise.promise;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
public resolve() {
|
|
38
|
-
return this._mockPromise.resolve();
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
public reject() {
|
|
42
|
-
this._mockPromise.reject();
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
public getData() {
|
|
46
|
-
return this._data;
|
|
47
|
-
}
|
|
48
|
-
}
|