@hyper-fetch/core 9.6.1 → 9.6.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/__tests__/features/adapter/adapter.bindings.progress.spec.ts +120 -0
- package/__tests__/features/adapter/adapter.bindings.spec.ts +4 -6
- package/__tests__/features/adapter/adapter.defaults.spec.ts +87 -0
- package/__tests__/features/adapter/adapter.mapping.spec.ts +257 -0
- package/__tests__/features/cache/cache.lazy-storage.clear.spec.ts +145 -0
- package/__tests__/features/cache/cache.lazy-storage.resilience.spec.ts +667 -0
- package/__tests__/features/cache/cache.scoped-events.spec.ts +183 -0
- package/__tests__/features/cache/cache.semantics.spec.ts +785 -0
- package/__tests__/features/cache/cache.structural-sharing.spec.ts +565 -0
- package/__tests__/features/cache/cache.timing.spec.ts +448 -0
- package/__tests__/features/client/client.clear.spec.ts +313 -0
- package/__tests__/features/client/client.configuration.spec.ts +416 -0
- package/__tests__/features/client/client.hydration.round-trip.spec.ts +324 -0
- package/__tests__/features/client/client.interceptor.spec.ts +54 -45
- package/__tests__/features/client/client.interceptors.pipeline.spec.ts +599 -0
- package/__tests__/features/client/client.user-errors.spec.ts +220 -0
- package/__tests__/features/dispatcher/dispatcher.cleanup.spec.ts +534 -0
- package/__tests__/features/dispatcher/dispatcher.control.spec.ts +381 -0
- package/__tests__/features/dispatcher/dispatcher.modes.spec.ts +527 -0
- package/__tests__/features/dispatcher/dispatcher.offline.spec.ts +278 -0
- package/__tests__/features/dispatcher/dispatcher.queue.spec.ts +35 -12
- package/__tests__/features/dispatcher/dispatcher.reentrancy.spec.ts +309 -0
- package/__tests__/features/dispatcher/dispatcher.response-order.spec.ts +241 -0
- package/__tests__/features/dispatcher/dispatcher.retry-flow.spec.ts +554 -0
- package/__tests__/features/dispatcher/dispatcher.retry-removal.spec.ts +196 -0
- package/__tests__/features/dispatcher/dispatcher.retry.spec.ts +4 -3
- package/__tests__/features/http-adapter/http-adapter.e2e.spec.ts +917 -0
- package/__tests__/features/managers/app/app.manager.events.spec.ts +434 -0
- package/__tests__/features/managers/logger/logger.levels.spec.ts +199 -0
- package/__tests__/features/managers/request/request.manager.abort.spec.ts +449 -0
- package/__tests__/features/mocker/mocker.base.spec.ts +22 -3
- package/__tests__/features/mocker/mocker.errors.spec.ts +99 -0
- package/__tests__/features/mocker/mocker.outcomes.spec.ts +308 -0
- package/__tests__/features/plugin/plugin.lifecycle.spec.ts +457 -0
- package/__tests__/features/request/request.async-mapper-removal.spec.ts +138 -0
- package/__tests__/features/request/request.immutability.spec.ts +259 -0
- package/__tests__/features/request/request.inference.spec.ts +269 -0
- package/__tests__/features/request/request.lifecycle.spec.ts +682 -0
- package/__tests__/features/request/request.optimistic-settlement.spec.ts +35 -17
- package/__tests__/features/request/request.values.spec.ts +408 -0
- package/__tests__/features/sdk/sdk.builder.spec.ts +304 -0
- package/__tests__/features/utils/emitter.listeners.spec.ts +202 -0
- package/__tests__/utils/controlled-adapter.utils.ts +119 -0
- package/__tests__/utils/fixtures.utils.ts +193 -0
- package/dist/adapter/adapter.bindings.d.ts.map +1 -1
- package/dist/adapter/adapter.utils.d.ts.map +1 -1
- package/dist/cache/cache.d.ts +12 -1
- package/dist/cache/cache.d.ts.map +1 -1
- package/dist/cache/cache.events.d.ts +20 -6
- package/dist/cache/cache.events.d.ts.map +1 -1
- package/dist/cache/cache.utils.d.ts +1 -0
- package/dist/cache/cache.utils.d.ts.map +1 -1
- package/dist/client/client.d.ts +3 -3
- package/dist/client/client.d.ts.map +1 -1
- package/dist/client/client.utils.d.ts.map +1 -1
- package/dist/dispatcher/dispatcher.d.ts +26 -5
- package/dist/dispatcher/dispatcher.d.ts.map +1 -1
- package/dist/http-adapter/http-adapter.fetch.d.ts.map +1 -1
- package/dist/index.mjs +2127 -1741
- package/dist/index.mjs.map +1 -1
- package/dist/managers/app/app.manager.constants.d.ts.map +1 -1
- package/dist/managers/app/app.manager.d.ts +9 -0
- package/dist/managers/app/app.manager.d.ts.map +1 -1
- package/dist/managers/app/app.manager.utils.d.ts.map +1 -1
- package/dist/managers/logger/logger.manager.d.ts.map +1 -1
- package/dist/managers/request/request.manager.d.ts.map +1 -1
- package/dist/managers/request/request.manager.types.d.ts +2 -0
- package/dist/managers/request/request.manager.types.d.ts.map +1 -1
- package/dist/mocker/mocker.d.ts +1 -1
- package/dist/mocker/mocker.d.ts.map +1 -1
- package/dist/plugin/plugin.d.ts +2 -0
- package/dist/plugin/plugin.d.ts.map +1 -1
- package/dist/request/request.d.ts +1 -1
- package/dist/request/request.d.ts.map +1 -1
- package/dist/request/request.utils.d.ts.map +1 -1
- package/dist/utils/emitter.utils.d.ts.map +1 -1
- package/dist/utils/error.utils.d.ts +17 -0
- package/dist/utils/error.utils.d.ts.map +1 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/adapter/adapter.bindings.ts +46 -18
- package/src/adapter/adapter.utils.ts +47 -7
- package/src/cache/cache.events.ts +24 -8
- package/src/cache/cache.ts +109 -21
- package/src/cache/cache.utils.ts +33 -10
- package/src/client/client.ts +99 -7
- package/src/client/client.utils.ts +10 -4
- package/src/dispatcher/dispatcher.ts +273 -133
- package/src/http-adapter/http-adapter.fetch.ts +15 -3
- package/src/managers/app/app.manager.constants.ts +12 -1
- package/src/managers/app/app.manager.ts +43 -23
- package/src/managers/app/app.manager.utils.ts +8 -4
- package/src/managers/logger/logger.manager.ts +21 -8
- package/src/managers/request/request.manager.ts +7 -1
- package/src/managers/request/request.manager.types.ts +2 -0
- package/src/mocker/mocker.ts +14 -11
- package/src/plugin/plugin.ts +6 -0
- package/src/request/request.ts +31 -22
- package/src/request/request.utils.ts +23 -15
- package/src/utils/emitter.utils.ts +9 -2
- package/src/utils/error.utils.ts +29 -0
- package/src/utils/index.ts +1 -0
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
import { createControlledClient, flushPromises, isPending } from "../../utils/controlled-adapter.utils";
|
|
2
|
+
|
|
3
|
+
describe("Dispatcher [ Offline ]", () => {
|
|
4
|
+
const setup = () => {
|
|
5
|
+
const { client, calls, getPending } = createControlledClient();
|
|
6
|
+
const createRequest = (options: { queued?: boolean; offline?: boolean; retry?: number; endpoint?: string } = {}) =>
|
|
7
|
+
client.createRequest<{ response: unknown; payload: any }>()({
|
|
8
|
+
endpoint: "/offline",
|
|
9
|
+
method: "POST",
|
|
10
|
+
...options,
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
return { client, calls, getPending, createRequest, dispatcher: client.submitDispatcher };
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
beforeEach(() => {
|
|
17
|
+
vi.useFakeTimers();
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
afterEach(() => {
|
|
21
|
+
vi.useRealTimers();
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
describe("When requests are sent without a connection", () => {
|
|
25
|
+
it("should keep them in the queue instead of sending", async () => {
|
|
26
|
+
const { client, createRequest, calls, dispatcher } = setup();
|
|
27
|
+
const request = createRequest();
|
|
28
|
+
client.appManager.setOnline(false);
|
|
29
|
+
|
|
30
|
+
const sent = request.send();
|
|
31
|
+
await vi.advanceTimersByTimeAsync(60_000);
|
|
32
|
+
|
|
33
|
+
expect(calls).toHaveLength(0);
|
|
34
|
+
expect(dispatcher.getQueue(request.queryKey).requests).toHaveLength(1);
|
|
35
|
+
expect(await isPending(sent)).toBe(true);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("should send them when the connection is back", async () => {
|
|
39
|
+
const { client, createRequest, calls } = setup();
|
|
40
|
+
const request = createRequest();
|
|
41
|
+
client.appManager.setOnline(false);
|
|
42
|
+
|
|
43
|
+
const sent = request.send();
|
|
44
|
+
await flushPromises();
|
|
45
|
+
client.appManager.setOnline(true);
|
|
46
|
+
await flushPromises();
|
|
47
|
+
await calls[0].succeed("synced");
|
|
48
|
+
|
|
49
|
+
await expect(sent).resolves.toHaveProperty("data", "synced");
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it("should send all the waiting requests of all queues", async () => {
|
|
53
|
+
const { client, createRequest, calls } = setup();
|
|
54
|
+
client.appManager.setOnline(false);
|
|
55
|
+
|
|
56
|
+
createRequest({ endpoint: "/a" }).send();
|
|
57
|
+
createRequest({ endpoint: "/b" }).send();
|
|
58
|
+
createRequest({ endpoint: "/b" }).send();
|
|
59
|
+
await flushPromises();
|
|
60
|
+
client.appManager.setOnline(true);
|
|
61
|
+
await flushPromises();
|
|
62
|
+
|
|
63
|
+
expect(calls.map((call) => call.request.endpoint)).toEqual(["/a", "/b", "/b"]);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("should send queued requests one by one in their order", async () => {
|
|
67
|
+
const { client, createRequest, calls } = setup();
|
|
68
|
+
const request = createRequest({ queued: true });
|
|
69
|
+
client.appManager.setOnline(false);
|
|
70
|
+
|
|
71
|
+
const sent = [1, 2, 3].map((payload) => request.send({ payload }));
|
|
72
|
+
await flushPromises();
|
|
73
|
+
client.appManager.setOnline(true);
|
|
74
|
+
for (let index = 0; index < 3; index += 1) {
|
|
75
|
+
// eslint-disable-next-line no-await-in-loop
|
|
76
|
+
await flushPromises();
|
|
77
|
+
expect(calls).toHaveLength(index + 1);
|
|
78
|
+
// eslint-disable-next-line no-await-in-loop
|
|
79
|
+
await calls[index].succeed();
|
|
80
|
+
}
|
|
81
|
+
await Promise.all(sent);
|
|
82
|
+
|
|
83
|
+
expect(calls.map((call) => call.request.payload)).toEqual([1, 2, 3]);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it("should not send anything twice when the connection comes back many times", async () => {
|
|
87
|
+
const { client, createRequest, calls } = setup();
|
|
88
|
+
const request = createRequest();
|
|
89
|
+
client.appManager.setOnline(false);
|
|
90
|
+
|
|
91
|
+
request.send();
|
|
92
|
+
await flushPromises();
|
|
93
|
+
client.appManager.setOnline(true);
|
|
94
|
+
client.appManager.setOnline(false);
|
|
95
|
+
client.appManager.setOnline(true);
|
|
96
|
+
client.appManager.setOnline(true);
|
|
97
|
+
await flushPromises();
|
|
98
|
+
|
|
99
|
+
expect(calls).toHaveLength(1);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it("should mark the loading events of a request sent after reconnecting as online", async () => {
|
|
103
|
+
const { client, createRequest } = setup();
|
|
104
|
+
const request = createRequest();
|
|
105
|
+
const offline: boolean[] = [];
|
|
106
|
+
client.requestManager.events.onLoading((event) => offline.push(event.isOffline));
|
|
107
|
+
client.appManager.setOnline(false);
|
|
108
|
+
|
|
109
|
+
request.send();
|
|
110
|
+
await flushPromises();
|
|
111
|
+
expect(offline).toEqual([]);
|
|
112
|
+
client.appManager.setOnline(true);
|
|
113
|
+
await flushPromises();
|
|
114
|
+
|
|
115
|
+
expect(offline).toEqual([false]);
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
describe("When the connection is lost during a request", () => {
|
|
120
|
+
it("should keep a failed request and send it again after reconnecting", async () => {
|
|
121
|
+
const { client, createRequest, calls, dispatcher } = setup();
|
|
122
|
+
const request = createRequest();
|
|
123
|
+
const onResponse = vi.fn();
|
|
124
|
+
|
|
125
|
+
const sent = request.send({ onResponse });
|
|
126
|
+
await flushPromises();
|
|
127
|
+
client.appManager.setOnline(false);
|
|
128
|
+
await calls[0].fail({ message: "Network error" }, 0);
|
|
129
|
+
|
|
130
|
+
expect(await isPending(sent)).toBe(true);
|
|
131
|
+
expect(onResponse).not.toHaveBeenCalled();
|
|
132
|
+
expect(dispatcher.getQueue(request.queryKey).requests).toHaveLength(1);
|
|
133
|
+
|
|
134
|
+
client.appManager.setOnline(true);
|
|
135
|
+
await flushPromises();
|
|
136
|
+
await calls[1].succeed("synced");
|
|
137
|
+
|
|
138
|
+
await expect(sent).resolves.toHaveProperty("data", "synced");
|
|
139
|
+
expect(onResponse).toHaveBeenCalledTimes(1);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it("should use the response which arrived successfully despite going offline", async () => {
|
|
143
|
+
const { client, createRequest, calls, dispatcher } = setup();
|
|
144
|
+
const request = createRequest();
|
|
145
|
+
|
|
146
|
+
const sent = request.send();
|
|
147
|
+
await flushPromises();
|
|
148
|
+
client.appManager.setOnline(false);
|
|
149
|
+
await calls[0].succeed("made it");
|
|
150
|
+
|
|
151
|
+
await expect(sent).resolves.toHaveProperty("data", "made it");
|
|
152
|
+
expect(dispatcher.getQueue(request.queryKey).requests).toHaveLength(0);
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it("should mark the response as received offline", async () => {
|
|
156
|
+
const { client, createRequest, calls } = setup();
|
|
157
|
+
const request = createRequest();
|
|
158
|
+
const offline: boolean[] = [];
|
|
159
|
+
client.requestManager.events.onResponse(({ details }) => offline.push(details.isOffline));
|
|
160
|
+
|
|
161
|
+
request.send();
|
|
162
|
+
await flushPromises();
|
|
163
|
+
client.appManager.setOnline(false);
|
|
164
|
+
await calls[0].fail();
|
|
165
|
+
|
|
166
|
+
expect(offline).toEqual([true]);
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
it("should hold the rest of a queue until the connection is back", async () => {
|
|
170
|
+
const { client, createRequest, calls } = setup();
|
|
171
|
+
const request = createRequest({ queued: true });
|
|
172
|
+
|
|
173
|
+
request.send({ payload: 1 });
|
|
174
|
+
request.send({ payload: 2 });
|
|
175
|
+
await flushPromises();
|
|
176
|
+
client.appManager.setOnline(false);
|
|
177
|
+
await calls[0].fail({ message: "Network error" }, 0);
|
|
178
|
+
await vi.advanceTimersByTimeAsync(60_000);
|
|
179
|
+
expect(calls).toHaveLength(1);
|
|
180
|
+
|
|
181
|
+
client.appManager.setOnline(true);
|
|
182
|
+
await flushPromises();
|
|
183
|
+
|
|
184
|
+
// The interrupted request goes first again
|
|
185
|
+
expect(calls).toHaveLength(2);
|
|
186
|
+
expect(calls[1].request.payload).toBe(1);
|
|
187
|
+
});
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
describe("When the request is not kept for later with offline set to false", () => {
|
|
191
|
+
it("should send it even without a connection", async () => {
|
|
192
|
+
const { client, createRequest, calls } = setup();
|
|
193
|
+
const request = createRequest({ offline: false });
|
|
194
|
+
client.appManager.setOnline(false);
|
|
195
|
+
|
|
196
|
+
request.send();
|
|
197
|
+
await flushPromises();
|
|
198
|
+
|
|
199
|
+
expect(calls).toHaveLength(1);
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
it("should give the error right away instead of waiting for the connection", async () => {
|
|
203
|
+
const { client, createRequest, calls, dispatcher } = setup();
|
|
204
|
+
const request = createRequest({ offline: false });
|
|
205
|
+
client.appManager.setOnline(false);
|
|
206
|
+
|
|
207
|
+
const sent = request.send();
|
|
208
|
+
await flushPromises();
|
|
209
|
+
await calls[0].fail({ message: "Network error" }, 0);
|
|
210
|
+
const response = await sent;
|
|
211
|
+
client.appManager.setOnline(true);
|
|
212
|
+
await flushPromises();
|
|
213
|
+
|
|
214
|
+
expect(response.error).toEqual({ message: "Network error" });
|
|
215
|
+
expect(calls).toHaveLength(1);
|
|
216
|
+
expect(dispatcher.getQueue(request.queryKey).requests).toHaveLength(0);
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
it("should not retry it while there is no connection", async () => {
|
|
220
|
+
const { client, createRequest, calls } = setup();
|
|
221
|
+
const request = createRequest({ offline: false, retry: 3 });
|
|
222
|
+
client.appManager.setOnline(false);
|
|
223
|
+
|
|
224
|
+
const sent = request.send();
|
|
225
|
+
await flushPromises();
|
|
226
|
+
await calls[0].fail();
|
|
227
|
+
await sent;
|
|
228
|
+
await vi.advanceTimersByTimeAsync(60_000);
|
|
229
|
+
|
|
230
|
+
expect(calls).toHaveLength(1);
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
it("should give the error when the connection is lost during the request", async () => {
|
|
234
|
+
const { client, createRequest, calls } = setup();
|
|
235
|
+
const request = createRequest({ offline: false });
|
|
236
|
+
|
|
237
|
+
const sent = request.send();
|
|
238
|
+
await flushPromises();
|
|
239
|
+
client.appManager.setOnline(false);
|
|
240
|
+
await calls[0].fail({ message: "Network error" }, 0);
|
|
241
|
+
|
|
242
|
+
await expect(sent).resolves.toHaveProperty("error", { message: "Network error" });
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
it("should not block the queued requests behind it", async () => {
|
|
246
|
+
const { client, createRequest, calls } = setup();
|
|
247
|
+
const request = createRequest({ queued: true, offline: false });
|
|
248
|
+
client.appManager.setOnline(false);
|
|
249
|
+
|
|
250
|
+
const first = request.send({ payload: 1 });
|
|
251
|
+
const second = request.send({ payload: 2 });
|
|
252
|
+
await flushPromises();
|
|
253
|
+
await calls[0].fail();
|
|
254
|
+
await flushPromises();
|
|
255
|
+
await calls[1].fail();
|
|
256
|
+
const responses = await Promise.all([first, second]);
|
|
257
|
+
|
|
258
|
+
expect(responses.map((response) => response.success)).toEqual([false, false]);
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
it("should send only the requests which do not wait for the connection from a mixed queue", async () => {
|
|
262
|
+
const { client, createRequest, calls } = setup();
|
|
263
|
+
const kept = createRequest({ offline: true });
|
|
264
|
+
const immediate = createRequest({ offline: false });
|
|
265
|
+
client.appManager.setOnline(false);
|
|
266
|
+
|
|
267
|
+
kept.send({ payload: "kept" });
|
|
268
|
+
immediate.send({ payload: "immediate" });
|
|
269
|
+
await flushPromises();
|
|
270
|
+
expect(calls.map((call) => call.request.payload)).toEqual(["immediate"]);
|
|
271
|
+
|
|
272
|
+
client.appManager.setOnline(true);
|
|
273
|
+
await flushPromises();
|
|
274
|
+
|
|
275
|
+
expect(calls.map((call) => call.request.payload)).toEqual(["immediate", "kept"]);
|
|
276
|
+
});
|
|
277
|
+
});
|
|
278
|
+
});
|
|
@@ -115,10 +115,12 @@ describe("Dispatcher [ Queue ]", () => {
|
|
|
115
115
|
dispatcher.add(request);
|
|
116
116
|
}, 20);
|
|
117
117
|
|
|
118
|
-
|
|
118
|
+
// Wait for the responses themselves - a fixed sleep is too short on a busy machine
|
|
119
|
+
await waitFor(() => {
|
|
120
|
+
expect(spy2).toHaveBeenCalledTimes(3);
|
|
121
|
+
});
|
|
119
122
|
|
|
120
123
|
expect(spy1).toHaveBeenCalledTimes(3);
|
|
121
|
-
expect(spy2).toHaveBeenCalledTimes(3);
|
|
122
124
|
});
|
|
123
125
|
});
|
|
124
126
|
describe("When using dispatcher performRequest method", () => {
|
|
@@ -235,8 +237,16 @@ describe("Dispatcher [ Queue ]", () => {
|
|
|
235
237
|
});
|
|
236
238
|
describe("When flushing requests", () => {
|
|
237
239
|
it("should flush all queues request", async () => {
|
|
238
|
-
const firstRequest = client.createRequest()({
|
|
239
|
-
|
|
240
|
+
const firstRequest = client.createRequest()({
|
|
241
|
+
endpoint: "shared-base-endpoint",
|
|
242
|
+
queryKey: "1",
|
|
243
|
+
deduplicate: false,
|
|
244
|
+
});
|
|
245
|
+
const secondRequest = client.createRequest()({
|
|
246
|
+
endpoint: "shared-base-endpoint",
|
|
247
|
+
queryKey: "2",
|
|
248
|
+
deduplicate: false,
|
|
249
|
+
});
|
|
240
250
|
mockRequest(firstRequest);
|
|
241
251
|
mockRequest(secondRequest);
|
|
242
252
|
|
|
@@ -349,7 +359,8 @@ describe("Dispatcher [ Queue ]", () => {
|
|
|
349
359
|
});
|
|
350
360
|
it("should stop queue and cancel ongoing requests", async () => {
|
|
351
361
|
const request = client.createRequest()({ endpoint: "shared-base-endpoint", queryKey: "1", deduplicate: false });
|
|
352
|
-
|
|
362
|
+
// The responses come long after the stop - both requests are stopped while they are in flight
|
|
363
|
+
mockRequest(request, { delay: 1000 });
|
|
353
364
|
|
|
354
365
|
const spy = vi.spyOn(client.adapter, "fetch");
|
|
355
366
|
const firstSpy = vi.fn();
|
|
@@ -360,7 +371,9 @@ describe("Dispatcher [ Queue ]", () => {
|
|
|
360
371
|
client.requestManager.events.onAbortById(firstRequestId, firstSpy);
|
|
361
372
|
client.requestManager.events.onAbortById(secondRequestId, secondSpy);
|
|
362
373
|
|
|
363
|
-
await
|
|
374
|
+
await waitFor(() => {
|
|
375
|
+
expect(dispatcher.getRunningRequests(request.queryKey)).toHaveLength(2);
|
|
376
|
+
});
|
|
364
377
|
|
|
365
378
|
dispatcher.stop(request.queryKey);
|
|
366
379
|
|
|
@@ -412,23 +425,33 @@ describe("Dispatcher [ Queue ]", () => {
|
|
|
412
425
|
});
|
|
413
426
|
it("should not remove requests from queue storage on stop", async () => {
|
|
414
427
|
const request = client.createRequest()({ endpoint: "shared-base-endpoint" });
|
|
415
|
-
|
|
428
|
+
// The response comes long after the stop - the request is stopped while it is in flight
|
|
429
|
+
mockRequest(request, { delay: 1000 });
|
|
416
430
|
|
|
417
|
-
dispatcher.add(request);
|
|
418
|
-
await
|
|
431
|
+
const requestId = dispatcher.add(request);
|
|
432
|
+
await waitFor(() => {
|
|
433
|
+
expect(dispatcher.hasRunningRequest(request.queryKey, requestId)).toBeTrue();
|
|
434
|
+
});
|
|
419
435
|
dispatcher.stop(request.queryKey);
|
|
420
436
|
|
|
421
437
|
expect(dispatcher.getQueue(request.queryKey).requests).toHaveLength(1);
|
|
438
|
+
expect(dispatcher.getRunningRequests(request.queryKey)).toHaveLength(0);
|
|
422
439
|
});
|
|
423
440
|
it("should not remove request from queue storage on stopRequest", async () => {
|
|
424
441
|
const request = client.createRequest()({ endpoint: "shared-base-endpoint" });
|
|
425
|
-
|
|
442
|
+
// The response comes long after the stop - the request is stopped while it is in flight
|
|
443
|
+
mockRequest(request, { delay: 1000 });
|
|
426
444
|
|
|
427
445
|
const requestId = dispatcher.add(request);
|
|
428
|
-
await
|
|
446
|
+
await waitFor(() => {
|
|
447
|
+
expect(dispatcher.hasRunningRequest(request.queryKey, requestId)).toBeTrue();
|
|
448
|
+
});
|
|
429
449
|
dispatcher.stopRequest(request.queryKey, requestId);
|
|
430
450
|
|
|
431
|
-
|
|
451
|
+
const { requests } = dispatcher.getQueue(request.queryKey);
|
|
452
|
+
expect(requests).toHaveLength(1);
|
|
453
|
+
expect(requests[0].stopped).toBeTrue();
|
|
454
|
+
expect(dispatcher.hasRunningRequest(request.queryKey, requestId)).toBeFalse();
|
|
432
455
|
});
|
|
433
456
|
});
|
|
434
457
|
|
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
import { createControlledClient, flushPromises } from "../../utils/controlled-adapter.utils";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Response listeners run while the answered request is still in its queue. Sending another request from one of them
|
|
5
|
+
* (the next step of a flow, a refetch) must send only that request - not the answered one once again.
|
|
6
|
+
*/
|
|
7
|
+
describe("Dispatcher [ Sending from response callbacks ]", () => {
|
|
8
|
+
const setup = (options: { queued?: boolean; retry?: number; retryTime?: number } = {}) => {
|
|
9
|
+
const { client, calls } = createControlledClient();
|
|
10
|
+
const request = client.createRequest<{ response: unknown; payload: { step: number } }>()({
|
|
11
|
+
endpoint: "/steps",
|
|
12
|
+
method: "POST",
|
|
13
|
+
...options,
|
|
14
|
+
});
|
|
15
|
+
// Every attempt of sending is listed - also the ones canceled before they reach the adapter
|
|
16
|
+
const attempts: unknown[] = [];
|
|
17
|
+
const canceled: unknown[] = [];
|
|
18
|
+
client.requestManager.events.onLoading(({ loading, request: sent }) => {
|
|
19
|
+
if (loading) {
|
|
20
|
+
attempts.push(sent.payload);
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
client.requestManager.events.onResponse(({ details, request: answered }) => {
|
|
24
|
+
if (details.isCanceled) {
|
|
25
|
+
canceled.push(answered.payload);
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
const payloads = () => {
|
|
29
|
+
expect(canceled).toStrictEqual([]);
|
|
30
|
+
expect(calls.map((call) => call.request.payload)).toStrictEqual(attempts);
|
|
31
|
+
return attempts;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
return { client, calls, request, payloads };
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
beforeEach(() => {
|
|
38
|
+
vi.useFakeTimers();
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
afterEach(() => {
|
|
42
|
+
vi.useRealTimers();
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
describe.each([
|
|
46
|
+
["concurrent", false],
|
|
47
|
+
["queued", true],
|
|
48
|
+
])("When the requests are %s", (_name, queued) => {
|
|
49
|
+
it("should send only the next request from the onResponse option", async () => {
|
|
50
|
+
const { calls, request, payloads } = setup({ queued });
|
|
51
|
+
let next: Promise<unknown> | undefined;
|
|
52
|
+
|
|
53
|
+
const first = request.send({
|
|
54
|
+
payload: { step: 1 },
|
|
55
|
+
onResponse: () => {
|
|
56
|
+
next = request.send({ payload: { step: 2 } });
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
await flushPromises();
|
|
60
|
+
await calls[0].succeed();
|
|
61
|
+
await flushPromises();
|
|
62
|
+
|
|
63
|
+
expect(payloads()).toStrictEqual([{ step: 1 }, { step: 2 }]);
|
|
64
|
+
|
|
65
|
+
await calls[1].succeed();
|
|
66
|
+
await Promise.all([first, next]);
|
|
67
|
+
await vi.advanceTimersByTimeAsync(1000);
|
|
68
|
+
|
|
69
|
+
expect(payloads()).toStrictEqual([{ step: 1 }, { step: 2 }]);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("should send only the next request from a response event listener", async () => {
|
|
73
|
+
const { client, calls, request, payloads } = setup({ queued });
|
|
74
|
+
let sent = false;
|
|
75
|
+
client.requestManager.events.onResponse(() => {
|
|
76
|
+
if (!sent) {
|
|
77
|
+
sent = true;
|
|
78
|
+
request.send({ payload: { step: 2 } });
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
request.send({ payload: { step: 1 } });
|
|
83
|
+
await flushPromises();
|
|
84
|
+
await calls[0].succeed();
|
|
85
|
+
await flushPromises();
|
|
86
|
+
await calls[1]?.succeed();
|
|
87
|
+
await vi.advanceTimersByTimeAsync(1000);
|
|
88
|
+
|
|
89
|
+
expect(payloads()).toStrictEqual([{ step: 1 }, { step: 2 }]);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it("should send only the next request after a failed response", async () => {
|
|
93
|
+
const { calls, request, payloads } = setup({ queued });
|
|
94
|
+
|
|
95
|
+
request.send({
|
|
96
|
+
payload: { step: 1 },
|
|
97
|
+
onResponse: () => {
|
|
98
|
+
request.send({ payload: { step: 2 } });
|
|
99
|
+
},
|
|
100
|
+
});
|
|
101
|
+
await flushPromises();
|
|
102
|
+
await calls[0].fail();
|
|
103
|
+
await flushPromises();
|
|
104
|
+
await calls[1]?.succeed();
|
|
105
|
+
await vi.advanceTimersByTimeAsync(1000);
|
|
106
|
+
|
|
107
|
+
expect(payloads()).toStrictEqual([{ step: 1 }, { step: 2 }]);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it("should run a chain of requests, each sent from the response of the previous one", async () => {
|
|
111
|
+
const { calls, request, payloads } = setup({ queued });
|
|
112
|
+
const sendStep = (step: number): Promise<unknown> =>
|
|
113
|
+
request.send({
|
|
114
|
+
payload: { step },
|
|
115
|
+
onResponse: () => {
|
|
116
|
+
if (step < 5) {
|
|
117
|
+
sendStep(step + 1);
|
|
118
|
+
}
|
|
119
|
+
},
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
sendStep(1);
|
|
123
|
+
for (let index = 0; index < 5; index += 1) {
|
|
124
|
+
// eslint-disable-next-line no-await-in-loop
|
|
125
|
+
await flushPromises();
|
|
126
|
+
// eslint-disable-next-line no-await-in-loop
|
|
127
|
+
await calls[index].succeed();
|
|
128
|
+
}
|
|
129
|
+
await vi.advanceTimersByTimeAsync(1000);
|
|
130
|
+
|
|
131
|
+
expect(payloads()).toStrictEqual([1, 2, 3, 4, 5].map((step) => ({ step })));
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
describe.each([
|
|
136
|
+
["concurrent", false],
|
|
137
|
+
["queued", true],
|
|
138
|
+
])("When a %s request is added to the dispatcher directly from a response listener", (_name, queued) => {
|
|
139
|
+
it("should not send the answered request again after a success", async () => {
|
|
140
|
+
const { client, calls, request, payloads } = setup({ queued });
|
|
141
|
+
let added = false;
|
|
142
|
+
client.requestManager.events.onResponse(() => {
|
|
143
|
+
if (!added) {
|
|
144
|
+
added = true;
|
|
145
|
+
client.submitDispatcher.add(request.setPayload({ step: 2 }));
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
client.submitDispatcher.add(request.setPayload({ step: 1 }));
|
|
150
|
+
await flushPromises();
|
|
151
|
+
await calls[0].succeed();
|
|
152
|
+
await flushPromises();
|
|
153
|
+
await calls[1]?.succeed();
|
|
154
|
+
await vi.advanceTimersByTimeAsync(1000);
|
|
155
|
+
|
|
156
|
+
expect(payloads()).toStrictEqual([{ step: 1 }, { step: 2 }]);
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it("should not send the answered request again after a failure", async () => {
|
|
160
|
+
const { client, calls, request, payloads } = setup({ queued });
|
|
161
|
+
let added = false;
|
|
162
|
+
client.requestManager.events.onResponse(() => {
|
|
163
|
+
if (!added) {
|
|
164
|
+
added = true;
|
|
165
|
+
client.submitDispatcher.add(request.setPayload({ step: 2 }));
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
client.submitDispatcher.add(request.setPayload({ step: 1 }));
|
|
170
|
+
await flushPromises();
|
|
171
|
+
await calls[0].fail();
|
|
172
|
+
await flushPromises();
|
|
173
|
+
await calls[1]?.succeed();
|
|
174
|
+
await vi.advanceTimersByTimeAsync(1000);
|
|
175
|
+
|
|
176
|
+
expect(payloads()).toStrictEqual([{ step: 1 }, { step: 2 }]);
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
it("should not send the answered request again when added from a loading listener", async () => {
|
|
180
|
+
const { client, calls, request, payloads } = setup({ queued });
|
|
181
|
+
let added = false;
|
|
182
|
+
client.requestManager.events.onLoading(({ loading }) => {
|
|
183
|
+
if (!loading && !added) {
|
|
184
|
+
added = true;
|
|
185
|
+
client.submitDispatcher.add(request.setPayload({ step: 2 }));
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
client.submitDispatcher.add(request.setPayload({ step: 1 }));
|
|
190
|
+
await flushPromises();
|
|
191
|
+
await calls[0].succeed();
|
|
192
|
+
await flushPromises();
|
|
193
|
+
await calls[1]?.succeed();
|
|
194
|
+
await vi.advanceTimersByTimeAsync(1000);
|
|
195
|
+
|
|
196
|
+
expect(payloads()).toStrictEqual([{ step: 1 }, { step: 2 }]);
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it("should leave the queue empty once both requests are answered", async () => {
|
|
200
|
+
const { client, calls, request } = setup({ queued });
|
|
201
|
+
let added = false;
|
|
202
|
+
client.requestManager.events.onResponse(() => {
|
|
203
|
+
if (!added) {
|
|
204
|
+
added = true;
|
|
205
|
+
client.submitDispatcher.add(request.setPayload({ step: 2 }));
|
|
206
|
+
}
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
client.submitDispatcher.add(request.setPayload({ step: 1 }));
|
|
210
|
+
await flushPromises();
|
|
211
|
+
await calls[0].succeed();
|
|
212
|
+
await flushPromises();
|
|
213
|
+
await calls[1]?.succeed();
|
|
214
|
+
await vi.advanceTimersByTimeAsync(1000);
|
|
215
|
+
|
|
216
|
+
expect(client.submitDispatcher.getAllRunningRequests()).toHaveLength(0);
|
|
217
|
+
expect(client.submitDispatcher.getQueue(request.queryKey).requests).toHaveLength(0);
|
|
218
|
+
});
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
describe("When the answered request is going to be retried", () => {
|
|
222
|
+
it("should keep its retry time when another request is sent from its response listener", async () => {
|
|
223
|
+
const { client, calls, request, payloads } = setup({ retry: 1, retryTime: 500 });
|
|
224
|
+
let sent = false;
|
|
225
|
+
client.requestManager.events.onResponse(() => {
|
|
226
|
+
if (!sent) {
|
|
227
|
+
sent = true;
|
|
228
|
+
request.send({ payload: { step: 2 } });
|
|
229
|
+
}
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
request.send({ payload: { step: 1 } });
|
|
233
|
+
await flushPromises();
|
|
234
|
+
await calls[0].fail();
|
|
235
|
+
await flushPromises();
|
|
236
|
+
|
|
237
|
+
// Only the new request went out - the failed one waits for its retry time
|
|
238
|
+
expect(payloads()).toStrictEqual([{ step: 1 }, { step: 2 }]);
|
|
239
|
+
|
|
240
|
+
await calls[1].succeed();
|
|
241
|
+
await vi.advanceTimersByTimeAsync(500);
|
|
242
|
+
|
|
243
|
+
expect(payloads()).toStrictEqual([{ step: 1 }, { step: 2 }, { step: 1 }]);
|
|
244
|
+
});
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
describe("When the answered request stays in the queue", () => {
|
|
248
|
+
it("should send the request kept for the network when the connection is back in its response listener", async () => {
|
|
249
|
+
const { client, calls, request } = setup();
|
|
250
|
+
let restored = false;
|
|
251
|
+
client.requestManager.events.onResponse(() => {
|
|
252
|
+
if (!restored) {
|
|
253
|
+
restored = true;
|
|
254
|
+
client.appManager.setOnline(true);
|
|
255
|
+
}
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
const sent = request.send({ payload: { step: 1 } });
|
|
259
|
+
await flushPromises();
|
|
260
|
+
client.appManager.setOnline(false);
|
|
261
|
+
await calls[0].fail();
|
|
262
|
+
await flushPromises();
|
|
263
|
+
|
|
264
|
+
expect(calls).toHaveLength(2);
|
|
265
|
+
expect(calls[1].requestId).toBe(calls[0].requestId);
|
|
266
|
+
|
|
267
|
+
await calls[1].succeed({ done: true });
|
|
268
|
+
const response = await sent;
|
|
269
|
+
|
|
270
|
+
expect(response.data).toStrictEqual({ done: true });
|
|
271
|
+
expect(calls).toHaveLength(2);
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
it("should keep the request waiting for the network when the connection is still down", async () => {
|
|
275
|
+
const { client, calls, request } = setup();
|
|
276
|
+
client.requestManager.events.onResponse(() => {
|
|
277
|
+
request.send({ payload: { step: 2 } });
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
request.send({ payload: { step: 1 } });
|
|
281
|
+
await flushPromises();
|
|
282
|
+
client.appManager.setOnline(false);
|
|
283
|
+
await calls[0].fail();
|
|
284
|
+
await vi.advanceTimersByTimeAsync(1000);
|
|
285
|
+
|
|
286
|
+
expect(calls).toHaveLength(1);
|
|
287
|
+
|
|
288
|
+
client.appManager.setOnline(true);
|
|
289
|
+
await flushPromises();
|
|
290
|
+
|
|
291
|
+
expect(calls.map((call) => call.request.payload)).toStrictEqual([{ step: 1 }, { step: 1 }, { step: 2 }]);
|
|
292
|
+
});
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
describe("When nothing is sent from the listeners", () => {
|
|
296
|
+
it("should still send the requests added while another one is in flight", async () => {
|
|
297
|
+
const { calls, request, payloads } = setup();
|
|
298
|
+
|
|
299
|
+
request.send({ payload: { step: 1 } });
|
|
300
|
+
await flushPromises();
|
|
301
|
+
request.send({ payload: { step: 2 } });
|
|
302
|
+
await flushPromises();
|
|
303
|
+
await calls[0].succeed();
|
|
304
|
+
await calls[1].succeed();
|
|
305
|
+
|
|
306
|
+
expect(payloads()).toStrictEqual([{ step: 1 }, { step: 2 }]);
|
|
307
|
+
});
|
|
308
|
+
});
|
|
309
|
+
});
|