@hyper-fetch/core 6.0.1 → 7.0.1
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/.eslintrc.json +8 -3
- package/README.md +5 -5
- package/dist/browser/index.js +3714 -0
- package/dist/browser/index.js.map +7 -0
- package/dist/index.d.ts +1669 -1091
- package/dist/index.js +3952 -0
- package/dist/index.js.map +7 -0
- package/package.json +16 -18
- package/src/adapter/adapter.bindings.ts +261 -165
- package/src/adapter/adapter.ts +480 -8
- package/src/adapter/adapter.types.ts +111 -139
- package/src/adapter/adapter.utils.ts +71 -25
- package/src/adapter/index.ts +1 -2
- package/src/cache/cache.events.ts +58 -14
- package/src/cache/cache.ts +158 -91
- package/src/cache/cache.types.ts +15 -25
- package/src/cache/cache.utils.ts +17 -10
- package/src/client/client.create.ts +17 -0
- package/src/client/client.ts +247 -266
- package/src/client/client.types.ts +43 -21
- package/src/client/client.utils.ts +7 -168
- package/src/client/index.ts +1 -0
- package/src/constants/http.constants.ts +6 -6
- package/src/constants/time.constants.ts +10 -10
- package/src/dispatcher/dispatcher.constants.ts +5 -5
- package/src/dispatcher/dispatcher.events.ts +85 -20
- package/src/dispatcher/dispatcher.ts +265 -199
- package/src/dispatcher/dispatcher.types.ts +6 -7
- package/src/dispatcher/dispatcher.utils.ts +26 -10
- package/src/http-adapter/http-adapter.browser.ts +101 -0
- package/src/http-adapter/http-adapter.constants.ts +8 -0
- package/src/http-adapter/http-adapter.server.ts +134 -0
- package/src/http-adapter/http-adapter.ts +11 -0
- package/src/http-adapter/http-adapter.types.ts +86 -0
- package/src/http-adapter/http-adapter.utils.ts +177 -0
- package/src/http-adapter/index.ts +4 -0
- package/src/index.ts +2 -1
- package/src/managers/app/app.manager.constants.ts +4 -4
- package/src/managers/app/app.manager.events.ts +12 -12
- package/src/managers/app/app.manager.ts +10 -7
- package/src/managers/app/app.manager.utils.ts +12 -2
- package/src/managers/logger/logger.manager.constants.ts +13 -18
- package/src/managers/logger/logger.manager.ts +37 -49
- package/src/managers/logger/logger.manager.types.ts +32 -21
- package/src/managers/logger/logger.manager.utils.ts +23 -17
- package/src/managers/request/request.manager.events.ts +222 -109
- package/src/managers/request/request.manager.ts +5 -2
- package/src/managers/request/request.manager.types.ts +35 -7
- package/src/managers/request/request.manager.utils.ts +29 -17
- package/src/mocker/mocker.ts +115 -81
- package/src/mocker/mocker.types.ts +29 -25
- package/src/plugin/index.ts +2 -0
- package/src/plugin/plugin.ts +159 -0
- package/src/plugin/plugin.types.ts +181 -0
- package/src/request/request.ts +366 -323
- package/src/request/request.types.ts +144 -148
- package/src/request/request.utils.ts +35 -32
- package/src/types/adapter.types.ts +63 -0
- package/src/types/client.types.ts +3 -3
- package/src/types/helpers.types.ts +16 -3
- package/src/types/hydration.types.ts +16 -0
- package/src/types/index.ts +3 -1
- package/src/types/request.types.ts +73 -0
- package/src/utils/emitter.utils.ts +52 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/uuid.utils.ts +4 -2
- package/tsconfig.base.json +4 -5
- package/tsconfig.json +5 -4
- package/dist/browser/index.cjs.js +0 -2420
- package/dist/browser/index.cjs.js.map +0 -7
- package/dist/browser/index.esm.js +0 -2395
- package/dist/browser/index.esm.js.map +0 -7
- package/dist/index.cjs.js +0 -2579
- package/dist/index.cjs.js.map +0 -7
- package/dist/index.esm.js +0 -2474
- package/dist/index.esm.js.map +0 -7
- package/src/adapter/adapter.browser.ts +0 -100
- package/src/adapter/adapter.constants.ts +0 -8
- package/src/adapter/adapter.server.ts +0 -93
- package/src/effect/index.ts +0 -2
- package/src/effect/request.effect.ts +0 -31
- package/src/effect/request.effect.types.ts +0 -44
- package/src/types/fetch.types.ts +0 -50
|
@@ -1,27 +1,26 @@
|
|
|
1
|
-
import EventEmitter from "events";
|
|
2
|
-
|
|
3
1
|
import {
|
|
4
2
|
QueueDataType,
|
|
5
3
|
getRequestType,
|
|
6
4
|
canRetryRequest,
|
|
7
5
|
getDispatcherEvents,
|
|
8
|
-
|
|
6
|
+
DispatcherMode,
|
|
9
7
|
DispatcherOptionsType,
|
|
10
8
|
DispatcherStorageType,
|
|
11
|
-
|
|
9
|
+
QueueItemType,
|
|
12
10
|
RunningRequestValueType,
|
|
13
11
|
} from "dispatcher";
|
|
14
12
|
import { ClientInstance } from "client";
|
|
15
|
-
import {
|
|
16
|
-
import { ResponseDetailsType,
|
|
13
|
+
import { EventEmitter } from "utils";
|
|
14
|
+
import { ResponseDetailsType, LoggerMethods } from "managers";
|
|
17
15
|
import { RequestInstance } from "request";
|
|
18
|
-
import { getErrorMessage } from "adapter";
|
|
16
|
+
import { AdapterInstance, getErrorMessage, RequestResponseType } from "adapter";
|
|
19
17
|
|
|
20
18
|
/**
|
|
21
|
-
* Dispatcher
|
|
22
|
-
*
|
|
19
|
+
* Dispatcher controls and manages the requests that are going to be executed with adapter. It manages them based on the options provided with request.
|
|
20
|
+
* This class can also run them with different modes like deduplication, cancelation, queueing or run-all-at-once mode. With it's help we can pause,
|
|
21
|
+
* stop, start and cancel requests.
|
|
23
22
|
*/
|
|
24
|
-
export class Dispatcher {
|
|
23
|
+
export class Dispatcher<Adapter extends AdapterInstance> {
|
|
25
24
|
public emitter = new EventEmitter();
|
|
26
25
|
public events = getDispatcherEvents(this.emitter);
|
|
27
26
|
public storage: DispatcherStorageType = new Map<string, QueueDataType<any>>();
|
|
@@ -29,68 +28,77 @@ export class Dispatcher {
|
|
|
29
28
|
private requestCount = new Map<string, number>();
|
|
30
29
|
private runningRequests = new Map<string, RunningRequestValueType[]>();
|
|
31
30
|
|
|
32
|
-
private logger:
|
|
31
|
+
private logger: LoggerMethods;
|
|
32
|
+
private client: ClientInstance<{ adapter: Adapter }>;
|
|
33
33
|
|
|
34
|
-
constructor(public
|
|
35
|
-
this.
|
|
34
|
+
constructor(public options?: DispatcherOptionsType) {
|
|
35
|
+
this.emitter?.setMaxListeners(1000);
|
|
36
36
|
|
|
37
37
|
if (this.options?.storage) {
|
|
38
38
|
this.storage = this.options.storage;
|
|
39
39
|
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
initialize = (client: ClientInstance<{ adapter: Adapter }>) => {
|
|
43
|
+
this.client = client;
|
|
44
|
+
this.logger = client.loggerManager.initialize(client, "Dispatcher");
|
|
40
45
|
|
|
41
46
|
// Going back from offline should re-trigger all requests
|
|
42
47
|
this.client.appManager.events.onOnline(() => {
|
|
43
48
|
this.flush();
|
|
44
49
|
});
|
|
45
50
|
|
|
46
|
-
this
|
|
47
|
-
}
|
|
51
|
+
return this;
|
|
52
|
+
};
|
|
48
53
|
|
|
49
|
-
//
|
|
50
|
-
//
|
|
54
|
+
// *********************************************************************
|
|
55
|
+
// *********************************************************************
|
|
51
56
|
// Queue
|
|
52
|
-
//
|
|
53
|
-
//
|
|
57
|
+
// *********************************************************************
|
|
58
|
+
// *********************************************************************
|
|
54
59
|
|
|
55
60
|
/**
|
|
56
|
-
* Start request handling by
|
|
61
|
+
* Start request handling by queryKey
|
|
57
62
|
*/
|
|
58
|
-
start = (
|
|
63
|
+
start = (queryKey: string) => {
|
|
59
64
|
// Change status to running
|
|
60
|
-
const queue = this.getQueue(
|
|
65
|
+
const queue = this.getQueue(queryKey);
|
|
61
66
|
|
|
62
67
|
// Start the queue when its stopped
|
|
63
68
|
queue.stopped = false;
|
|
64
|
-
this.setQueue(
|
|
65
|
-
this.flushQueue(
|
|
66
|
-
this.
|
|
69
|
+
this.setQueue(queryKey, queue);
|
|
70
|
+
this.flushQueue(queryKey);
|
|
71
|
+
this.client.triggerPlugins("onDispatcherQueueRunning", { dispatcher: this, queue, status: "running" });
|
|
72
|
+
this.events.emitQueueStatusChanged(queue);
|
|
67
73
|
};
|
|
68
74
|
|
|
69
75
|
/**
|
|
70
|
-
* Pause request queue, but not cancel already started requests
|
|
76
|
+
* Pause request queue, but do not cancel already started requests
|
|
71
77
|
*/
|
|
72
|
-
pause = (
|
|
78
|
+
pause = (queryKey: string) => {
|
|
73
79
|
// Change state to stopped
|
|
74
|
-
const queue = this.getQueue(
|
|
80
|
+
const queue = this.getQueue(queryKey);
|
|
75
81
|
|
|
76
82
|
queue.stopped = true;
|
|
77
|
-
this.setQueue(
|
|
78
|
-
this.
|
|
83
|
+
this.setQueue(queryKey, queue);
|
|
84
|
+
this.client.triggerPlugins("onDispatcherQueueRunning", { dispatcher: this, queue, status: "paused" });
|
|
85
|
+
this.events.emitQueueStatusChanged(queue);
|
|
79
86
|
};
|
|
80
87
|
|
|
81
88
|
/**
|
|
82
89
|
* Stop request queue and cancel all started requests - those will be treated like not started
|
|
83
90
|
*/
|
|
84
|
-
stop = (
|
|
91
|
+
stop = (queryKey: string) => {
|
|
85
92
|
// Change state to stopped
|
|
86
|
-
const queue = this.getQueue(
|
|
93
|
+
const queue = this.getQueue(queryKey);
|
|
87
94
|
|
|
88
95
|
queue.stopped = true;
|
|
89
|
-
this.setQueue(
|
|
96
|
+
this.setQueue(queryKey, queue);
|
|
90
97
|
|
|
91
98
|
// Cancel running requests
|
|
92
|
-
this.cancelRunningRequests(
|
|
93
|
-
this.
|
|
99
|
+
this.cancelRunningRequests(queryKey);
|
|
100
|
+
this.client.triggerPlugins("onDispatcherQueueRunning", { dispatcher: this, queue, status: "stopped" });
|
|
101
|
+
this.events.emitQueueStatusChanged(queue);
|
|
94
102
|
};
|
|
95
103
|
|
|
96
104
|
/**
|
|
@@ -103,9 +111,9 @@ export class Dispatcher {
|
|
|
103
111
|
/**
|
|
104
112
|
* Return queue state object
|
|
105
113
|
*/
|
|
106
|
-
getQueue = <Request extends RequestInstance = RequestInstance>(
|
|
107
|
-
const initialQueueState: QueueDataType<Request> = { requests: [], stopped: false };
|
|
108
|
-
const storedEntity = this.storage.get<Request>(
|
|
114
|
+
getQueue = <Request extends RequestInstance = RequestInstance>(queryKey: string) => {
|
|
115
|
+
const initialQueueState: QueueDataType<Request> = { queryKey, requests: [], stopped: false };
|
|
116
|
+
const storedEntity = this.storage.get<Request>(queryKey);
|
|
109
117
|
|
|
110
118
|
return storedEntity || initialQueueState;
|
|
111
119
|
};
|
|
@@ -113,9 +121,9 @@ export class Dispatcher {
|
|
|
113
121
|
/**
|
|
114
122
|
* Return request from queue state
|
|
115
123
|
*/
|
|
116
|
-
getRequest = <Request extends RequestInstance = RequestInstance>(
|
|
117
|
-
const initialQueueState: QueueDataType<Request> = { requests: [], stopped: false };
|
|
118
|
-
const storedEntity = this.storage.get<Request>(
|
|
124
|
+
getRequest = <Request extends RequestInstance = RequestInstance>(queryKey: string, requestId: string) => {
|
|
125
|
+
const initialQueueState: QueueDataType<Request> = { queryKey, requests: [], stopped: false };
|
|
126
|
+
const storedEntity = this.storage.get<Request>(queryKey) || initialQueueState;
|
|
119
127
|
|
|
120
128
|
return storedEntity.requests.find((req) => req.requestId === requestId);
|
|
121
129
|
};
|
|
@@ -123,8 +131,8 @@ export class Dispatcher {
|
|
|
123
131
|
/**
|
|
124
132
|
* Get value of the active queue status based on the stopped status
|
|
125
133
|
*/
|
|
126
|
-
getIsActiveQueue = (
|
|
127
|
-
const queue = this.getQueue(
|
|
134
|
+
getIsActiveQueue = (queryKey: string) => {
|
|
135
|
+
const queue = this.getQueue(queryKey);
|
|
128
136
|
const hasAvailableRequests = queue.requests.some((req) => !req.stopped);
|
|
129
137
|
const isRunningQueue = !queue.stopped;
|
|
130
138
|
return hasAvailableRequests && isRunningQueue;
|
|
@@ -133,24 +141,27 @@ export class Dispatcher {
|
|
|
133
141
|
/**
|
|
134
142
|
* Add new element to storage
|
|
135
143
|
*/
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
144
|
+
addQueueItem = <Request extends RequestInstance = RequestInstance>(
|
|
145
|
+
queryKey: string,
|
|
146
|
+
element: QueueItemType<Request>,
|
|
139
147
|
) => {
|
|
140
|
-
const queue = this.getQueue<Request>(
|
|
141
|
-
queue.requests.push(
|
|
142
|
-
|
|
148
|
+
const queue = this.getQueue<Request>(queryKey);
|
|
149
|
+
queue.requests.push(element);
|
|
150
|
+
|
|
151
|
+
this.client.triggerPlugins("onDispatcherItemAdded", { dispatcher: this, queue, queueItem: element });
|
|
152
|
+
|
|
153
|
+
this.setQueue<Request>(queryKey, queue);
|
|
143
154
|
};
|
|
144
155
|
|
|
145
156
|
/**
|
|
146
157
|
* Set new queue storage value
|
|
147
158
|
*/
|
|
148
|
-
setQueue = <Request extends RequestInstance = RequestInstance>(
|
|
149
|
-
this.storage.set<Request>(
|
|
159
|
+
setQueue = <Request extends RequestInstance = RequestInstance>(queryKey: string, queue: QueueDataType<Request>) => {
|
|
160
|
+
this.storage.set<Request>(queryKey, queue);
|
|
150
161
|
|
|
151
162
|
// Emit Queue Changes
|
|
152
|
-
this.
|
|
153
|
-
this.events.
|
|
163
|
+
this.client.triggerPlugins("onDispatcherQueueCreated", { dispatcher: this, queue });
|
|
164
|
+
this.events.emitQueueChanged(queue);
|
|
154
165
|
|
|
155
166
|
return queue;
|
|
156
167
|
};
|
|
@@ -158,14 +169,15 @@ export class Dispatcher {
|
|
|
158
169
|
/**
|
|
159
170
|
* Clear requests from queue cache
|
|
160
171
|
*/
|
|
161
|
-
clearQueue = (
|
|
162
|
-
const queue = this.getQueue(
|
|
163
|
-
const newQueue = { requests: [], stopped: queue.stopped };
|
|
164
|
-
this.storage.set(
|
|
172
|
+
clearQueue = (queryKey: string) => {
|
|
173
|
+
const queue = this.getQueue(queryKey);
|
|
174
|
+
const newQueue = { queryKey, requests: [], stopped: queue.stopped };
|
|
175
|
+
this.storage.set(queryKey, newQueue);
|
|
165
176
|
|
|
166
177
|
// Emit Queue Changes
|
|
167
|
-
this.
|
|
168
|
-
this.events.
|
|
178
|
+
this.client.triggerPlugins("onDispatcherQueueCleared", { dispatcher: this, queue: newQueue });
|
|
179
|
+
this.events.emitQueueChanged(newQueue);
|
|
180
|
+
this.events.emitDrained(newQueue);
|
|
169
181
|
|
|
170
182
|
return newQueue;
|
|
171
183
|
};
|
|
@@ -173,30 +185,30 @@ export class Dispatcher {
|
|
|
173
185
|
/**
|
|
174
186
|
* Method used to flush the queue requests
|
|
175
187
|
*/
|
|
176
|
-
flushQueue = async (
|
|
177
|
-
const queue = this.getQueue(
|
|
178
|
-
const runningRequests = this.getRunningRequests(
|
|
179
|
-
const
|
|
188
|
+
flushQueue = async (queryKey: string) => {
|
|
189
|
+
const queue = this.getQueue(queryKey);
|
|
190
|
+
const runningRequests = this.getRunningRequests(queryKey);
|
|
191
|
+
const queueItem = queue.requests.find((request) => !request.stopped);
|
|
180
192
|
|
|
181
193
|
const isStopped = queue && queue.stopped;
|
|
182
194
|
const isOffline = !this.client.appManager.isOnline;
|
|
183
|
-
const isConcurrent = !
|
|
195
|
+
const isConcurrent = !queueItem?.request.queued;
|
|
184
196
|
const isInactive = !runningRequests.length;
|
|
185
|
-
const isEmpty = !
|
|
197
|
+
const isEmpty = !queueItem;
|
|
186
198
|
|
|
187
199
|
// When there are no requests to flush, when its stopped, there is running request
|
|
188
200
|
// or there is no request to trigger - we don't want to perform actions
|
|
189
201
|
if (isStopped || isOffline || isEmpty) {
|
|
190
|
-
this.logger.debug("Skipping queue trigger", { isStopped, isOffline, isEmpty });
|
|
202
|
+
this.logger.debug({ title: "Skipping queue trigger", type: "system", extra: { isStopped, isOffline, isEmpty } });
|
|
191
203
|
} else if (isConcurrent) {
|
|
192
204
|
queue.requests.forEach((element) => {
|
|
193
|
-
if (!this.hasRunningRequest(
|
|
205
|
+
if (!this.hasRunningRequest(queryKey, element.requestId)) {
|
|
194
206
|
this.performRequest(element);
|
|
195
207
|
}
|
|
196
208
|
});
|
|
197
209
|
} else if (isInactive) {
|
|
198
|
-
await this.performRequest(
|
|
199
|
-
this.flushQueue(
|
|
210
|
+
await this.performRequest(queueItem);
|
|
211
|
+
this.flushQueue(queryKey);
|
|
200
212
|
}
|
|
201
213
|
};
|
|
202
214
|
|
|
@@ -208,9 +220,9 @@ export class Dispatcher {
|
|
|
208
220
|
|
|
209
221
|
// eslint-disable-next-line no-restricted-syntax
|
|
210
222
|
for (const key of keys) {
|
|
211
|
-
const
|
|
223
|
+
const storageItem = this.getQueue(key);
|
|
212
224
|
|
|
213
|
-
if (
|
|
225
|
+
if (storageItem) {
|
|
214
226
|
this.flushQueue(key);
|
|
215
227
|
}
|
|
216
228
|
}
|
|
@@ -221,114 +233,116 @@ export class Dispatcher {
|
|
|
221
233
|
*/
|
|
222
234
|
clear = () => {
|
|
223
235
|
const keys = this.getQueuesKeys();
|
|
224
|
-
keys.forEach((
|
|
236
|
+
keys.forEach((queryKey) => this.cancelRunningRequests(queryKey));
|
|
225
237
|
|
|
226
238
|
this.runningRequests.clear();
|
|
227
239
|
this.storage.clear();
|
|
228
|
-
this.
|
|
240
|
+
this.client.triggerPlugins("onDispatcherCleared", { dispatcher: this });
|
|
229
241
|
};
|
|
230
242
|
|
|
231
|
-
//
|
|
232
|
-
//
|
|
243
|
+
// *********************************************************************
|
|
244
|
+
// *********************************************************************
|
|
233
245
|
// Requests
|
|
234
|
-
//
|
|
235
|
-
//
|
|
246
|
+
// *********************************************************************
|
|
247
|
+
// *********************************************************************
|
|
236
248
|
|
|
237
249
|
/**
|
|
238
250
|
* Start particular request
|
|
239
251
|
*/
|
|
240
|
-
startRequest = (
|
|
252
|
+
startRequest = (queryKey: string, requestId: string) => {
|
|
241
253
|
// Change status to running
|
|
242
|
-
const queue = this.getQueue(
|
|
254
|
+
const queue = this.getQueue(queryKey);
|
|
243
255
|
const request = queue.requests.find((element) => element.requestId === requestId);
|
|
244
256
|
|
|
245
257
|
// Start the queue when its stopped
|
|
246
258
|
if (request) {
|
|
247
259
|
request.stopped = false;
|
|
248
|
-
this.setQueue(
|
|
249
|
-
this.flushQueue(
|
|
250
|
-
this.events.
|
|
260
|
+
this.setQueue(queryKey, queue);
|
|
261
|
+
this.flushQueue(queryKey);
|
|
262
|
+
this.events.emitQueueStatusChanged(queue);
|
|
251
263
|
}
|
|
252
264
|
};
|
|
253
265
|
|
|
254
266
|
/**
|
|
255
267
|
* Stop particular request
|
|
256
268
|
*/
|
|
257
|
-
stopRequest = (
|
|
269
|
+
stopRequest = (queryKey: string, requestId: string) => {
|
|
258
270
|
// Change state to stopped
|
|
259
|
-
const queue = this.getQueue(
|
|
271
|
+
const queue = this.getQueue(queryKey);
|
|
260
272
|
const request = queue.requests.find((element) => element.requestId === requestId);
|
|
261
273
|
|
|
262
274
|
if (request) {
|
|
263
275
|
request.stopped = true;
|
|
264
|
-
this.setQueue(
|
|
276
|
+
this.setQueue(queryKey, queue);
|
|
265
277
|
|
|
266
278
|
// Cancel running requests
|
|
267
|
-
this.cancelRunningRequest(
|
|
268
|
-
this.events.
|
|
279
|
+
this.cancelRunningRequest(queryKey, requestId);
|
|
280
|
+
this.events.emitQueueStatusChanged(queue);
|
|
269
281
|
}
|
|
270
282
|
};
|
|
271
283
|
|
|
272
284
|
/**
|
|
273
|
-
* Get currently running requests from all
|
|
285
|
+
* Get currently running requests from all queryKeys
|
|
274
286
|
*/
|
|
275
|
-
|
|
287
|
+
getAllRunningRequests = () => {
|
|
276
288
|
return Array.from(this.runningRequests.values()).flat();
|
|
277
289
|
};
|
|
278
290
|
|
|
279
291
|
/**
|
|
280
292
|
* Get currently running requests
|
|
281
293
|
*/
|
|
282
|
-
getRunningRequests = (
|
|
283
|
-
return this.runningRequests.get(
|
|
294
|
+
getRunningRequests = (queryKey: string) => {
|
|
295
|
+
return this.runningRequests.get(queryKey) || [];
|
|
284
296
|
};
|
|
285
297
|
|
|
286
298
|
/**
|
|
287
299
|
* Get running request by id
|
|
288
300
|
*/
|
|
289
|
-
getRunningRequest = (
|
|
290
|
-
const runningRequests = this.getRunningRequests(
|
|
301
|
+
getRunningRequest = (queryKey: string, requestId: string) => {
|
|
302
|
+
const runningRequests = this.getRunningRequests(queryKey);
|
|
291
303
|
return runningRequests.find((req) => req.requestId === requestId);
|
|
292
304
|
};
|
|
293
305
|
|
|
294
306
|
/**
|
|
295
307
|
* Add request to the running requests list
|
|
296
308
|
*/
|
|
297
|
-
addRunningRequest = (
|
|
298
|
-
const
|
|
299
|
-
runningRequests
|
|
300
|
-
|
|
309
|
+
addRunningRequest = (queryKey: string, requestId: string, request: RequestInstance): RunningRequestValueType => {
|
|
310
|
+
const newRunningRequest = { requestId, request, timestamp: Date.now() };
|
|
311
|
+
const runningRequests = this.getRunningRequests(queryKey);
|
|
312
|
+
runningRequests.push(newRunningRequest);
|
|
313
|
+
this.runningRequests.set(queryKey, runningRequests);
|
|
314
|
+
return newRunningRequest;
|
|
301
315
|
};
|
|
302
316
|
|
|
303
317
|
/**
|
|
304
318
|
* Get the value based on the currently running requests
|
|
305
319
|
*/
|
|
306
|
-
hasRunningRequests = (
|
|
307
|
-
return !!this.getRunningRequests(
|
|
320
|
+
hasRunningRequests = (queryKey: string) => {
|
|
321
|
+
return !!this.getRunningRequests(queryKey).length;
|
|
308
322
|
};
|
|
309
323
|
|
|
310
324
|
/**
|
|
311
325
|
* Check if request is currently processing
|
|
312
326
|
*/
|
|
313
|
-
hasRunningRequest = (
|
|
314
|
-
const runningRequests = this.getRunningRequests(
|
|
327
|
+
hasRunningRequest = (queryKey: string, requestId: string) => {
|
|
328
|
+
const runningRequests = this.getRunningRequests(queryKey);
|
|
315
329
|
return !!runningRequests.find((req) => req.requestId === requestId);
|
|
316
330
|
};
|
|
317
331
|
|
|
318
332
|
/**
|
|
319
333
|
* Cancel all started requests, but do NOT remove it from main storage
|
|
320
334
|
*/
|
|
321
|
-
cancelRunningRequests = (
|
|
322
|
-
this.runningRequests.get(
|
|
335
|
+
cancelRunningRequests = (queryKey: string) => {
|
|
336
|
+
this.runningRequests.get(queryKey)?.forEach((request) => {
|
|
323
337
|
this.client.requestManager.abortByRequestId(request.request.abortKey, request.requestId);
|
|
324
338
|
});
|
|
325
|
-
this.deleteRunningRequests(
|
|
339
|
+
this.deleteRunningRequests(queryKey);
|
|
326
340
|
};
|
|
327
341
|
/**
|
|
328
342
|
* Cancel started request, but do NOT remove it from main storage
|
|
329
343
|
*/
|
|
330
|
-
cancelRunningRequest = (
|
|
331
|
-
const requests = this.getRunningRequests(
|
|
344
|
+
cancelRunningRequest = (queryKey: string, requestId: string) => {
|
|
345
|
+
const requests = this.getRunningRequests(queryKey).filter((request) => {
|
|
332
346
|
if (request.requestId === requestId) {
|
|
333
347
|
this.client.requestManager.abortByRequestId(request.request.abortKey, request.requestId);
|
|
334
348
|
return false;
|
|
@@ -336,103 +350,109 @@ export class Dispatcher {
|
|
|
336
350
|
return true;
|
|
337
351
|
});
|
|
338
352
|
|
|
339
|
-
this.runningRequests.set(
|
|
353
|
+
this.runningRequests.set(queryKey, requests);
|
|
340
354
|
};
|
|
341
355
|
|
|
342
356
|
/**
|
|
343
357
|
* Delete all started requests, but do NOT clear it from queue and do NOT cancel them
|
|
344
358
|
*/
|
|
345
|
-
deleteRunningRequests = (
|
|
346
|
-
this.runningRequests.set(
|
|
359
|
+
deleteRunningRequests = (queryKey: string) => {
|
|
360
|
+
this.runningRequests.set(queryKey, []);
|
|
347
361
|
};
|
|
348
362
|
|
|
349
363
|
/**
|
|
350
364
|
* Delete request by id, but do NOT clear it from queue and do NOT cancel them
|
|
351
365
|
*/
|
|
352
|
-
deleteRunningRequest = (
|
|
353
|
-
const runningRequests = this.getRunningRequests(
|
|
366
|
+
deleteRunningRequest = (queryKey: string, requestId: string) => {
|
|
367
|
+
const runningRequests = this.getRunningRequests(queryKey);
|
|
354
368
|
this.runningRequests.set(
|
|
355
|
-
|
|
369
|
+
queryKey,
|
|
356
370
|
runningRequests.filter((req) => req.requestId !== requestId),
|
|
357
371
|
);
|
|
358
372
|
};
|
|
359
373
|
|
|
360
374
|
/**
|
|
361
|
-
* Get count of requests from the same
|
|
375
|
+
* Get count of requests from the same queryKey
|
|
362
376
|
*/
|
|
363
|
-
getQueueRequestCount = (
|
|
364
|
-
return this.requestCount.get(
|
|
377
|
+
getQueueRequestCount = (queryKey: string) => {
|
|
378
|
+
return this.requestCount.get(queryKey) || 0;
|
|
365
379
|
};
|
|
366
380
|
|
|
367
381
|
/**
|
|
368
|
-
* Add request count to the
|
|
382
|
+
* Add request count to the queryKey
|
|
369
383
|
*/
|
|
370
|
-
incrementQueueRequestCount = (
|
|
371
|
-
const count = this.requestCount.get(
|
|
372
|
-
this.requestCount.set(
|
|
384
|
+
incrementQueueRequestCount = (queryKey: string) => {
|
|
385
|
+
const count = this.requestCount.get(queryKey) || 0;
|
|
386
|
+
this.requestCount.set(queryKey, count + 1);
|
|
373
387
|
};
|
|
374
388
|
|
|
375
389
|
/**
|
|
376
390
|
* Create storage element from request
|
|
377
391
|
*/
|
|
378
392
|
// eslint-disable-next-line class-methods-use-this
|
|
379
|
-
|
|
380
|
-
const requestId =
|
|
381
|
-
const
|
|
393
|
+
createStorageItem = <Request extends RequestInstance>(request: Request) => {
|
|
394
|
+
const requestId = this.client.unstable_requestIdMapper(request);
|
|
395
|
+
const storageItem: QueueItemType<Request> = {
|
|
382
396
|
requestId,
|
|
383
397
|
timestamp: +new Date(),
|
|
384
398
|
request,
|
|
385
399
|
retries: 0,
|
|
386
400
|
stopped: false,
|
|
401
|
+
resolved: false,
|
|
387
402
|
};
|
|
388
|
-
return
|
|
403
|
+
return storageItem;
|
|
389
404
|
};
|
|
390
405
|
|
|
391
|
-
//
|
|
392
|
-
//
|
|
406
|
+
// *********************************************************************
|
|
407
|
+
// *********************************************************************
|
|
393
408
|
// Dispatching
|
|
394
|
-
//
|
|
395
|
-
//
|
|
409
|
+
// *********************************************************************
|
|
410
|
+
// *********************************************************************
|
|
396
411
|
|
|
397
412
|
/**
|
|
398
413
|
* Add request to the dispatcher handler
|
|
399
414
|
*/
|
|
400
415
|
add = (request: RequestInstance) => {
|
|
401
|
-
const {
|
|
416
|
+
const { queryKey } = request;
|
|
402
417
|
|
|
403
418
|
// Create dump of the request to allow storing it in localStorage, AsyncStorage or any other
|
|
404
419
|
// This way we don't save the Class but the instruction of the request to be done
|
|
405
|
-
const
|
|
406
|
-
const { requestId } =
|
|
420
|
+
const storageItem = this.createStorageItem(request);
|
|
421
|
+
const { requestId } = storageItem;
|
|
407
422
|
|
|
408
|
-
const queue = this.getQueue(
|
|
423
|
+
const queue = this.getQueue(queryKey);
|
|
409
424
|
const [latestRequest] = queue.requests.slice(-1);
|
|
410
425
|
const requestType = getRequestType(request, latestRequest);
|
|
411
426
|
|
|
412
|
-
this.logger.debug("Adding request to queue", { requestType, request, requestId });
|
|
427
|
+
this.logger.debug({ title: "Adding request to queue", type: "system", extra: { requestType, request, requestId } });
|
|
413
428
|
|
|
414
429
|
switch (requestType) {
|
|
415
|
-
case
|
|
430
|
+
case DispatcherMode.ONE_BY_ONE: {
|
|
416
431
|
// Requests will go one by one
|
|
417
|
-
this.
|
|
418
|
-
this.flushQueue(
|
|
432
|
+
this.addQueueItem(queryKey, storageItem);
|
|
433
|
+
this.flushQueue(queryKey);
|
|
419
434
|
return requestId;
|
|
420
435
|
}
|
|
421
|
-
case
|
|
436
|
+
case DispatcherMode.PREVIOUS_CANCELED: {
|
|
422
437
|
// Cancel all previous on-going requests
|
|
423
|
-
this.cancelRunningRequests(
|
|
424
|
-
this.clearQueue(
|
|
425
|
-
this.
|
|
426
|
-
this.flushQueue(
|
|
438
|
+
this.cancelRunningRequests(queryKey);
|
|
439
|
+
this.clearQueue(queryKey);
|
|
440
|
+
this.addQueueItem(queryKey, storageItem);
|
|
441
|
+
this.flushQueue(queryKey);
|
|
427
442
|
return requestId;
|
|
428
443
|
}
|
|
429
|
-
case
|
|
444
|
+
case DispatcherMode.DEDUPLICATED: {
|
|
445
|
+
this.client.requestManager.events.emitDeduplicated({
|
|
446
|
+
request: latestRequest.request,
|
|
447
|
+
requestId,
|
|
448
|
+
deduplicatedRequest: request,
|
|
449
|
+
});
|
|
430
450
|
// Return the running requestId to fulfill the events
|
|
431
|
-
return
|
|
451
|
+
return latestRequest.requestId;
|
|
432
452
|
}
|
|
433
453
|
default: {
|
|
434
|
-
this.
|
|
435
|
-
this.flushQueue(
|
|
454
|
+
this.addQueueItem(queryKey, storageItem);
|
|
455
|
+
this.flushQueue(queryKey);
|
|
436
456
|
return requestId;
|
|
437
457
|
}
|
|
438
458
|
}
|
|
@@ -441,29 +461,35 @@ export class Dispatcher {
|
|
|
441
461
|
/**
|
|
442
462
|
* Delete from the storage and cancel request
|
|
443
463
|
*/
|
|
444
|
-
delete = (
|
|
445
|
-
this.logger.debug("Deleting request", {
|
|
446
|
-
const queue = this.getQueue(
|
|
447
|
-
const
|
|
464
|
+
delete = (queryKey: string, requestId: string, abortKey: string) => {
|
|
465
|
+
this.logger.debug({ title: "Deleting request", type: "system", extra: { queryKey, requestId, abortKey } });
|
|
466
|
+
const queue = this.getQueue(queryKey);
|
|
467
|
+
const queueItem = queue.requests.find((req) => req.requestId === requestId);
|
|
448
468
|
|
|
449
|
-
if (!
|
|
469
|
+
if (!queueItem) return;
|
|
450
470
|
|
|
451
471
|
queue.requests = queue.requests.filter((req) => req.requestId !== requestId);
|
|
452
|
-
this.storage.set(
|
|
472
|
+
this.storage.set(queryKey, queue);
|
|
453
473
|
|
|
454
474
|
// Clean controllers
|
|
455
|
-
if (this.hasRunningRequest(
|
|
456
|
-
this.deleteRunningRequest(
|
|
475
|
+
if (this.hasRunningRequest(queryKey, requestId)) {
|
|
476
|
+
this.deleteRunningRequest(queryKey, requestId);
|
|
457
477
|
this.client.requestManager.abortByRequestId(abortKey, requestId);
|
|
458
478
|
}
|
|
459
479
|
|
|
460
480
|
// Emit Queue Changes
|
|
461
|
-
this.
|
|
462
|
-
|
|
463
|
-
this.
|
|
481
|
+
this.client.triggerPlugins("onDispatcherItemDeleted", { queue, dispatcher: this, queueItem });
|
|
482
|
+
|
|
483
|
+
this.events.emitQueueChanged(queue);
|
|
484
|
+
this.client.requestManager.events.emitRemove({
|
|
485
|
+
requestId,
|
|
486
|
+
request: queueItem.request,
|
|
487
|
+
resolved: queueItem.resolved,
|
|
488
|
+
});
|
|
464
489
|
|
|
465
490
|
if (!queue.requests.length) {
|
|
466
|
-
this.
|
|
491
|
+
this.client.triggerPlugins("onDispatcherQueueDrained", { queue, dispatcher: this });
|
|
492
|
+
this.events.emitDrained(queue);
|
|
467
493
|
}
|
|
468
494
|
|
|
469
495
|
return queue;
|
|
@@ -473,43 +499,50 @@ export class Dispatcher {
|
|
|
473
499
|
* Request can run for some time, once it's done, we have to check if it's successful or if it was aborted
|
|
474
500
|
* It can be different once the previous call was set as cancelled and removed from queue before this request got resolved
|
|
475
501
|
*/
|
|
476
|
-
performRequest = async (
|
|
477
|
-
const { request, requestId } =
|
|
478
|
-
this.logger.
|
|
502
|
+
performRequest = async (storageItem: QueueItemType) => {
|
|
503
|
+
const { request, requestId } = storageItem;
|
|
504
|
+
this.logger.debug({ title: "Performing request", type: "system", extra: { request, requestId } });
|
|
479
505
|
|
|
480
|
-
const { retry, retryTime,
|
|
506
|
+
const { retry, retryTime, queryKey, abortKey, offline } = request;
|
|
481
507
|
const { adapter, requestManager, cache, appManager } = this.client;
|
|
482
508
|
|
|
483
|
-
const canRetry = canRetryRequest(
|
|
509
|
+
const canRetry = canRetryRequest(storageItem.retries, retry);
|
|
484
510
|
// When offline not perform any request
|
|
485
511
|
const isOffline = !appManager.isOnline && offline;
|
|
486
512
|
// When request with this id was triggered again
|
|
487
|
-
const isAlreadyRunning = this.hasRunningRequest(
|
|
488
|
-
const isStopped =
|
|
513
|
+
const isAlreadyRunning = this.hasRunningRequest(queryKey, requestId);
|
|
514
|
+
const isStopped = storageItem.stopped;
|
|
489
515
|
|
|
490
516
|
if (isOffline || isAlreadyRunning || isStopped) {
|
|
491
|
-
return this.logger.warning(
|
|
517
|
+
return this.logger.warning({
|
|
518
|
+
title: "Unable to perform request",
|
|
519
|
+
type: "system",
|
|
520
|
+
extra: { isOffline, isAlreadyRunning, isStopped },
|
|
521
|
+
});
|
|
492
522
|
}
|
|
493
523
|
|
|
494
524
|
// Additionally keep the running request to possibly abort it later
|
|
495
|
-
this.addRunningRequest(
|
|
525
|
+
const runningRequest = this.addRunningRequest(queryKey, requestId, request);
|
|
496
526
|
|
|
497
527
|
// Propagate the loading to all connected hooks
|
|
498
|
-
requestManager.events.emitLoading(
|
|
499
|
-
|
|
528
|
+
requestManager.events.emitLoading({
|
|
529
|
+
request,
|
|
500
530
|
requestId,
|
|
501
531
|
loading: true,
|
|
502
|
-
isRetry: !!
|
|
532
|
+
isRetry: !!storageItem.retries,
|
|
503
533
|
isOffline,
|
|
504
534
|
});
|
|
505
535
|
|
|
506
536
|
// Trigger Request
|
|
507
|
-
this.incrementQueueRequestCount(
|
|
537
|
+
this.incrementQueueRequestCount(queryKey);
|
|
508
538
|
// Listen for aborting
|
|
509
539
|
requestManager.addAbortController(abortKey, requestId);
|
|
510
540
|
|
|
511
|
-
const response = await adapter(request, requestId);
|
|
541
|
+
const response: RequestResponseType<any> = await adapter.fetch(request, requestId);
|
|
512
542
|
|
|
543
|
+
// TODO: improve this
|
|
544
|
+
// eslint-disable-next-line no-param-reassign
|
|
545
|
+
storageItem.resolved = true;
|
|
513
546
|
// Stop listening for aborting
|
|
514
547
|
requestManager.removeAbortController(abortKey, requestId);
|
|
515
548
|
// Do not continue the request handling when it got stopped and request was unsuccessful
|
|
@@ -517,80 +550,113 @@ export class Dispatcher {
|
|
|
517
550
|
const isOfflineResponseStatus = !appManager.isOnline;
|
|
518
551
|
// If there is no running request with this id, it means it was cancelled and removed during send
|
|
519
552
|
const isCancelMessage = getErrorMessage("abort").message === response.error?.message;
|
|
520
|
-
const isCanceled = !this.hasRunningRequest(
|
|
553
|
+
const isCanceled = !this.hasRunningRequest(queryKey, requestId) || isCancelMessage;
|
|
521
554
|
|
|
522
555
|
// Remove running request, must be called after isCancelled
|
|
523
|
-
this.deleteRunningRequest(
|
|
556
|
+
this.deleteRunningRequest(queryKey, requestId);
|
|
524
557
|
|
|
525
558
|
const requestDetails: ResponseDetailsType = {
|
|
526
559
|
isCanceled,
|
|
527
560
|
isOffline: isOfflineResponseStatus,
|
|
528
|
-
retries:
|
|
529
|
-
|
|
561
|
+
retries: storageItem.retries,
|
|
562
|
+
addedTimestamp: storageItem.timestamp,
|
|
563
|
+
triggerTimestamp: runningRequest.timestamp,
|
|
564
|
+
requestTimestamp: response.requestTimestamp,
|
|
565
|
+
responseTimestamp: response.responseTimestamp,
|
|
530
566
|
};
|
|
531
567
|
|
|
532
568
|
// Global response emitter to handle request execution
|
|
533
|
-
requestManager.events.emitResponse(
|
|
569
|
+
requestManager.events.emitResponse({ request, requestId, response, details: requestDetails });
|
|
534
570
|
|
|
535
571
|
// Turn off loading
|
|
536
|
-
requestManager.events.emitLoading(
|
|
537
|
-
|
|
572
|
+
requestManager.events.emitLoading({
|
|
573
|
+
request,
|
|
538
574
|
requestId,
|
|
539
575
|
loading: false,
|
|
540
|
-
isRetry: !!
|
|
576
|
+
isRetry: !!storageItem.retries,
|
|
541
577
|
isOffline,
|
|
542
578
|
});
|
|
543
579
|
|
|
544
580
|
// Cache event to emit the data inside and store it
|
|
545
581
|
cache.set(request, { ...response, ...requestDetails });
|
|
546
|
-
this.logger.
|
|
582
|
+
this.logger.debug({
|
|
583
|
+
title: "Dispatcher processing response",
|
|
584
|
+
type: "system",
|
|
585
|
+
extra: { requestId, request, response, details: requestDetails },
|
|
586
|
+
});
|
|
547
587
|
|
|
548
588
|
// On cancelled
|
|
549
589
|
if (isCanceled) {
|
|
550
|
-
const queue = this.getQueue(
|
|
551
|
-
const
|
|
590
|
+
const queue = this.getQueue(queryKey);
|
|
591
|
+
const queueItem = queue.requests.find((req) => req.requestId === requestId);
|
|
552
592
|
|
|
553
593
|
// do not remove cancelled request as it may be result of manual queue pause
|
|
554
594
|
// if abort was done without stop action we can remove request
|
|
555
|
-
if (!queue.stopped && !
|
|
556
|
-
this.logger.debug("Request paused", { response, requestDetails, request });
|
|
557
|
-
return this.delete(
|
|
595
|
+
if (!queue.stopped && !queueItem?.stopped) {
|
|
596
|
+
this.logger.debug({ title: "Request paused", type: "system", extra: { response, requestDetails, request } });
|
|
597
|
+
return this.delete(queryKey, requestId, abortKey);
|
|
558
598
|
}
|
|
559
|
-
return this.logger.debug(
|
|
599
|
+
return this.logger.debug({
|
|
600
|
+
title: "Request canceled",
|
|
601
|
+
type: "system",
|
|
602
|
+
extra: { response, requestDetails, request },
|
|
603
|
+
});
|
|
560
604
|
}
|
|
561
605
|
// On offline
|
|
562
606
|
if (!response.success && isOfflineResponseStatus) {
|
|
563
607
|
// if we don't want to keep offline request - just delete them
|
|
564
608
|
if (!offline) {
|
|
565
|
-
this.logger.warning(
|
|
566
|
-
|
|
609
|
+
this.logger.warning({
|
|
610
|
+
title: "Removing non-offline request",
|
|
611
|
+
type: "system",
|
|
612
|
+
extra: { response, requestDetails, request },
|
|
613
|
+
});
|
|
614
|
+
return this.delete(queryKey, requestId, abortKey);
|
|
567
615
|
}
|
|
568
616
|
// do not remove request from store as we want to re-send it later
|
|
569
|
-
return this.logger.debug(
|
|
617
|
+
return this.logger.debug({
|
|
618
|
+
title: "Awaiting for network restoration",
|
|
619
|
+
type: "system",
|
|
620
|
+
extra: { response, requestDetails, request },
|
|
621
|
+
});
|
|
570
622
|
}
|
|
571
623
|
// On success
|
|
572
624
|
if (response.success) {
|
|
573
|
-
this.delete(
|
|
574
|
-
return this.logger.debug(
|
|
575
|
-
response,
|
|
576
|
-
|
|
577
|
-
|
|
625
|
+
this.delete(queryKey, requestId, abortKey);
|
|
626
|
+
return this.logger.debug({
|
|
627
|
+
title: "Successful response, removing request from queue.",
|
|
628
|
+
type: "system",
|
|
629
|
+
extra: {
|
|
630
|
+
response,
|
|
631
|
+
requestDetails,
|
|
632
|
+
request,
|
|
633
|
+
},
|
|
578
634
|
});
|
|
579
635
|
}
|
|
580
636
|
// On retry
|
|
581
637
|
if (!response.success && canRetry) {
|
|
582
|
-
this.logger.debug("Waiting for retry", { response, requestDetails, request });
|
|
638
|
+
this.logger.debug({ title: "Waiting for retry", type: "system", extra: { response, requestDetails, request } });
|
|
583
639
|
// Perform retry once request is failed
|
|
584
640
|
setTimeout(() => {
|
|
585
|
-
this.logger.warning(
|
|
641
|
+
this.logger.warning({
|
|
642
|
+
title: "Error response, performing retry",
|
|
643
|
+
type: "request",
|
|
644
|
+
extra: { response, requestDetails, request, requestId },
|
|
645
|
+
});
|
|
586
646
|
this.performRequest({
|
|
587
|
-
...
|
|
588
|
-
retries:
|
|
647
|
+
...storageItem,
|
|
648
|
+
retries: storageItem.retries + 1,
|
|
589
649
|
});
|
|
590
650
|
}, retryTime || 0);
|
|
591
651
|
} else {
|
|
592
|
-
|
|
593
|
-
|
|
652
|
+
if (request.retry) {
|
|
653
|
+
this.logger.error({
|
|
654
|
+
title: "All retries have been used. Removing request from queue.",
|
|
655
|
+
type: "request",
|
|
656
|
+
extra: { response, requestDetails, request, requestId },
|
|
657
|
+
});
|
|
658
|
+
}
|
|
659
|
+
this.delete(queryKey, requestId, abortKey);
|
|
594
660
|
}
|
|
595
661
|
};
|
|
596
662
|
}
|