@brandup/ui-ajax 1.0.44 → 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cjs/index.js CHANGED
@@ -1,425 +1,14 @@
1
1
  'use strict';
2
2
 
3
- const createQuery = (query) => {
4
- const urlParams = new URLSearchParams();
5
- if (!query)
6
- return urlParams;
7
- if (query instanceof FormData) {
8
- query.forEach((value, key) => {
9
- if (!key)
10
- return;
11
- urlParams.append(key, value.toString());
12
- });
13
- }
14
- else {
15
- for (const key in query) {
16
- const val = query[key];
17
- if (val === null)
18
- continue;
19
- if (Array.isArray(val))
20
- val.forEach(v => urlParams.append(key, v));
21
- else
22
- urlParams.append(key, val);
23
- }
24
- }
25
- return urlParams;
26
- };
27
- const addQuery = (url, query) => {
28
- if (query) {
29
- const urlParams = createQuery(query);
30
- if (urlParams.size) {
31
- if (url.indexOf("?") === -1)
32
- url += "?";
33
- else
34
- url += "&";
35
- url += urlParams.toString();
36
- }
37
- }
38
- return url;
39
- };
40
- const encodeForm = (data) => {
41
- const query = createQuery(data);
42
- return query.toString();
43
- };
3
+ var request = require('./request.js');
4
+ var ajaxRequest = require('./ajax-request.js');
5
+ var queue = require('./queue.js');
6
+ var helpers = require('./helpers.js');
44
7
 
45
- var helpers = /*#__PURE__*/Object.freeze({
46
- __proto__: null,
47
- addQuery: addQuery,
48
- createQuery: createQuery,
49
- encodeForm: encodeForm
50
- });
51
8
 
52
- const DEFAULT_TIMEOUT = 30000;
53
- const FORM_URL = "application/x-www-form-urlencoded";
54
- const FORM_DATA = "multipart/form-data";
55
- const detectRequestType = (options) => {
56
- if (!options.type && options.data) {
57
- const body = options.data;
58
- if (body instanceof Blob)
59
- options.type = "BLOB";
60
- else if (body instanceof FormData)
61
- options.type = null;
62
- else if (body instanceof HTMLFormElement)
63
- options.type = "FORM";
64
- else if (body instanceof Object)
65
- options.type = "JSON";
66
- else if (typeof body === "string")
67
- options.type = "TEXT";
68
- }
69
- };
70
- const prepareRequest = (options, body) => {
71
- const headers = {};
72
- if (options.headers) {
73
- for (const key in options.headers) {
74
- const value = options.headers[key];
75
- if (!value)
76
- continue;
77
- headers[key] = value;
78
- }
79
- }
80
- if (options.type) {
81
- let contentType = null;
82
- let accept = null;
83
- switch (options.type) {
84
- case "XML":
85
- contentType = "application/xml; charset=utf-8";
86
- accept = "application/xml, text/xml, */*; q=0.01";
87
- break;
88
- case "JSON":
89
- contentType = "application/json; charset=utf-8";
90
- accept = "application/json, text/json, */*; q=0.01";
91
- body = JSON.stringify(body);
92
- break;
93
- case "FORM":
94
- if (body instanceof HTMLFormElement) {
95
- body = new FormData(body);
96
- }
97
- else if (body instanceof FormData)
98
- contentType = FORM_URL;
99
- if (contentType == FORM_URL)
100
- body = encodeForm(body);
101
- else if (contentType == FORM_DATA)
102
- contentType = null;
103
- break;
104
- case "FORMDATA":
105
- break;
106
- case "TEXT":
107
- contentType = "text/plain";
108
- break;
109
- }
110
- if (accept)
111
- headers["Accept"] = accept;
112
- if (contentType)
113
- headers['Content-Type'] = contentType;
114
- }
115
- return {
116
- headers,
117
- body
118
- };
119
- };
120
- var internals = {
121
- DEFAULT_TIMEOUT,
122
- FORM_URL,
123
- FORM_DATA,
124
- detectRequestType,
125
- prepareRequest
126
- };
127
9
 
128
- /** Request with fetch. */
129
- async function request(options, abortSignal) {
130
- let { mode, credentials = "include" } = options;
131
- let url = options.url || location.href;
132
- url = addQuery(url, options.query);
133
- const method = options.method ? options.method.toUpperCase() : "GET";
134
- let body = options.data;
135
- if (body && (method === "GET" || method === "HEAD"))
136
- throw new Error("GET method is not support request with data.");
137
- internals.detectRequestType(options);
138
- const prepared = internals.prepareRequest(options, body);
139
- const abortSignals = [AbortSignal.timeout(options.timeout ?? internals.DEFAULT_TIMEOUT)];
140
- if (options.abort)
141
- abortSignals.push(options.abort);
142
- if (abortSignal)
143
- abortSignals.push(abortSignal);
144
- try {
145
- const response = await fetch(url, {
146
- method,
147
- headers: new Headers(prepared.headers),
148
- cache: options.disableCache ? "no-cache" : "default",
149
- mode,
150
- credentials,
151
- redirect: "follow",
152
- signal: AbortSignal.any(abortSignals),
153
- body: prepared.body
154
- });
155
- let result;
156
- switch (response.type) {
157
- case "basic":
158
- case "default":
159
- case "cors": {
160
- let responseData = null;
161
- let responseType = "none";
162
- let contentType = response.headers.get("content-type");
163
- if (!response.redirected && response.body) {
164
- if (contentType) {
165
- const ctSplitIndex = contentType.indexOf(";");
166
- if (ctSplitIndex > 0)
167
- contentType = contentType.substring(0, ctSplitIndex);
168
- if (contentType.includes("json")) {
169
- responseType = "json";
170
- responseData = await response.json();
171
- }
172
- else if (contentType.includes("text/html")) {
173
- responseType = "html";
174
- responseData = await response.text();
175
- }
176
- else if (contentType.includes("text/plain")) {
177
- responseType = "text";
178
- responseData = await response.text();
179
- }
180
- else {
181
- responseType = "blob";
182
- responseData = await response.blob();
183
- }
184
- }
185
- }
186
- result = {
187
- status: response.status,
188
- url: response.url,
189
- redirected: response.redirected,
190
- type: responseType,
191
- contentType,
192
- headers: response.headers,
193
- data: responseData,
194
- state: options.state
195
- };
196
- break;
197
- }
198
- case "opaqueredirect":
199
- case "opaque":
200
- throw new Error(`Not supported response type: ${response.type}`);
201
- case "error":
202
- throw new Error("Response error.");
203
- default:
204
- throw new Error(`Unknown response type: ${response.type}`);
205
- }
206
- if (options.success)
207
- options.success(result);
208
- return result;
209
- }
210
- catch (error) {
211
- if (options.error)
212
- options.error(options, error);
213
- throw error;
214
- }
215
- }
216
-
217
- /** Request with XMLHttpRequest. */
218
- const ajaxRequest = (options) => {
219
- let url = options.url || location.href;
220
- let { query } = options;
221
- if (options.disableCache) {
222
- if (!query)
223
- query = {};
224
- query["_"] = Date.now().toString();
225
- }
226
- url = addQuery(url, query);
227
- const method = options.method ? options.method.toUpperCase() : "GET";
228
- if (options.data && method === "GET")
229
- throw new Error("GET method is not support request with data.");
230
- internals.detectRequestType(options);
231
- const prepared = internals.prepareRequest(options, options.data);
232
- const xhr = new XMLHttpRequest();
233
- xhr.withCredentials = true;
234
- if (options.timeout === 0 || options.timeout)
235
- xhr.timeout = options.timeout;
236
- xhr.onreadystatechange = (_e) => {
237
- switch (xhr.readyState) {
238
- case XMLHttpRequest.DONE: {
239
- if (options.success) {
240
- let responseData = null;
241
- let responseType = "none";
242
- const contentType = xhr.getResponseHeader("Content-Type");
243
- if (xhr.response) {
244
- if (contentType) {
245
- if (contentType.includes("json")) {
246
- responseType = "json";
247
- try {
248
- responseData = JSON.parse(xhr.responseText);
249
- }
250
- catch (e) {
251
- if (options.error)
252
- options.error(options, e);
253
- break;
254
- }
255
- }
256
- else if (contentType.includes("text/plain")) {
257
- responseType = "text";
258
- responseData = xhr.responseText;
259
- }
260
- else if (contentType.includes("text/html")) {
261
- responseType = "html";
262
- responseData = xhr.responseText;
263
- }
264
- }
265
- }
266
- const headers = {
267
- get(name) {
268
- return xhr.getResponseHeader(name);
269
- },
270
- has(name) {
271
- return !!xhr.getResponseHeader(name);
272
- },
273
- forEach(callbackfn, thisArg) {
274
- const headers = xhr.getAllResponseHeaders();
275
- // Convert the header string into an array
276
- // of individual headers
277
- const arr = headers.trim().split(/[\r\n]+/);
278
- // Create a map of header names to values
279
- arr.forEach((line) => {
280
- const parts = line.split(": ");
281
- const header = parts.shift() || "";
282
- const value = parts.join(": ");
283
- callbackfn.call(thisArg, value, header.toLowerCase(), {});
284
- });
285
- }
286
- };
287
- options.success({
288
- status: xhr.status,
289
- url: xhr.responseURL,
290
- redirected: false,
291
- type: responseType,
292
- contentType,
293
- headers,
294
- data: responseData,
295
- state: options.state
296
- });
297
- }
298
- break;
299
- }
300
- }
301
- };
302
- xhr.onabort = (_e) => {
303
- if (options.error)
304
- options.error(options, "Request aborted");
305
- };
306
- xhr.onerror = (_e) => {
307
- if (options.error)
308
- options.error(options, "Request network error");
309
- };
310
- xhr.ontimeout = (_e) => {
311
- if (options.error)
312
- options.error(options, "Request timeout");
313
- };
314
- xhr.open(method, url, true);
315
- xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
316
- for (const key in prepared.headers) {
317
- const value = prepared.headers[key];
318
- if (!value)
319
- continue;
320
- xhr.setRequestHeader(key, value);
321
- }
322
- if (method === "GET")
323
- xhr.send();
324
- else
325
- xhr.send(prepared.body);
326
- return xhr;
327
- };
328
-
329
- class AjaxQueue {
330
- _options;
331
- _requests = [];
332
- _curent = null;
333
- _destroyed = false;
334
- constructor(options) {
335
- this._options = options ?? {};
336
- }
337
- get length() { return this._requests.length; }
338
- get isFree() { return !this._requests.length && !this._curent; }
339
- get isEmpty() { return !this._requests.length; }
340
- push(request, abortSignal) {
341
- if (this._destroyed)
342
- throw new Error("AjaxQueue is destroyed.");
343
- this._requests.push({ request, cancel: abortSignal, abort: new AbortController() });
344
- if (!this._curent)
345
- this.__execute();
346
- }
347
- enque(request, abortSignal) {
348
- const { success, error } = request;
349
- return new Promise((resolve, reject) => {
350
- request.success = (response) => {
351
- if (success)
352
- success(response);
353
- resolve(response);
354
- };
355
- request.error = (request, reason) => {
356
- if (error)
357
- error(request, reason);
358
- reject(reason);
359
- };
360
- this.push(request, abortSignal);
361
- });
362
- }
363
- reset(cancelCurrentRequest = false) {
364
- this._requests = [];
365
- const current = this._curent;
366
- this._curent = null;
367
- if (cancelCurrentRequest && current)
368
- current.abort?.abort("ResetAjaxQueue");
369
- }
370
- destroy() {
371
- if (this._destroyed)
372
- return;
373
- this._destroyed = true;
374
- this._requests = [];
375
- if (this._curent) {
376
- this._curent.abort?.abort("DestroyAjaxQueue");
377
- this._curent = null;
378
- }
379
- }
380
- __execute() {
381
- if (this._destroyed)
382
- return;
383
- if (this._curent)
384
- throw new Error("AjaxQueue currently is executing.");
385
- const task = this._curent = this._requests.shift() ?? null;
386
- if (task) {
387
- if (this._options.canRequest && this._options.canRequest(task.request) === false) {
388
- this.__next();
389
- return;
390
- }
391
- if (task.request.abort?.aborted || task.cancel?.aborted)
392
- task.result = Promise.reject("cancelled");
393
- else {
394
- task.abort = new AbortController();
395
- task.result = request(task.request, task.cancel ? AbortSignal.any([task.abort.signal, task.cancel]) : task.abort.signal);
396
- }
397
- task.result
398
- .then(response => {
399
- if (this._destroyed)
400
- return;
401
- if (this._options.successRequest)
402
- this._options.successRequest(task.request, response);
403
- })
404
- .catch(reason => {
405
- if (this._destroyed)
406
- return;
407
- if (this._options.errorRequest)
408
- this._options.errorRequest(task.request, reason);
409
- })
410
- .finally(() => this.__next());
411
- }
412
- }
413
- __next() {
414
- if (this._destroyed)
415
- return;
416
- this._curent = null;
417
- this.__execute();
418
- }
419
- }
420
-
421
- exports.AjaxQueue = AjaxQueue;
10
+ exports.request = request.request;
11
+ exports.ajaxRequest = ajaxRequest.ajaxRequest;
12
+ exports.AjaxQueue = queue.AjaxQueue;
422
13
  exports.RequestHelper = helpers;
423
- exports.ajaxRequest = ajaxRequest;
424
- exports.request = request;
425
14
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../../source/helpers.ts","../../source/internals.ts","../../source/request.ts","../../source/ajax-request.ts","../../source/ajax-queue.ts"],"sourcesContent":[null,null,null,null,null],"names":["helpers.encodeForm","helpers.addQuery"],"mappings":";;AAEA,MAAM,WAAW,GAAG,CAAC,KAAmC,KAAI;AAC3D,IAAA,MAAM,SAAS,GAAG,IAAI,eAAe,EAAE;AAEvC,IAAA,IAAI,CAAC,KAAK;AACT,QAAA,OAAO,SAAS;AAEjB,IAAA,IAAI,KAAK,YAAY,QAAQ,EAAE;QAC9B,KAAK,CAAC,OAAO,CAAC,CAAC,KAAyB,EAAE,GAAW,KAAI;AACxD,YAAA,IAAI,CAAC,GAAG;gBACP;YACD,SAAS,CAAC,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,QAAQ,EAAE,CAAC;AACxC,QAAA,CAAC,CAAC;IACH;SACK;AACJ,QAAA,KAAK,MAAM,GAAG,IAAI,KAAK,EAAE;AACxB,YAAA,MAAM,GAAG,GAAG,KAAK,CAAC,GAAG,CAAC;YACt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AAI,CAAC,QAAQ,CAAC,YAAY;oBAC7B,IAAI,CAAC,QAAQ,CAAC,YAAY,CAAC,IAAI,CAAC,OAAO,EAAE,MAAM,CAAC;AAClD,YAAA,CAAC;iBACA,OAAO,CAAC,MAAM,IAAI,CAAC,MAAM,EAAE,CAAC;QAC/B;IACD;IAEQ,MAAM,GAAA;QACb,IAAI,IAAI,CAAC,UAAU;YAClB;AAED,QAAA,IAAI,CAAC,OAAO,GAAG,IAAI;QACnB,IAAI,CAAC,SAAS,EAAE;IACjB;AACA;;;;;;;"}
1
+ {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;"}
@@ -0,0 +1,82 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var helpers = require('./helpers.js');
6
+
7
+ const DEFAULT_TIMEOUT = 30000;
8
+ const FORM_URL = "application/x-www-form-urlencoded";
9
+ const FORM_DATA = "multipart/form-data";
10
+ const detectRequestType = (options) => {
11
+ if (!options.type && options.data) {
12
+ const body = options.data;
13
+ if (body instanceof Blob)
14
+ options.type = "BLOB";
15
+ else if (body instanceof FormData)
16
+ options.type = "FORMDATA";
17
+ else if (body instanceof HTMLFormElement)
18
+ options.type = "FORM";
19
+ else if (body instanceof Object)
20
+ options.type = "JSON";
21
+ else if (typeof body === "string")
22
+ options.type = "TEXT";
23
+ }
24
+ };
25
+ const prepareRequest = (options, body) => {
26
+ const headers = {};
27
+ if (options.headers) {
28
+ for (const key in options.headers) {
29
+ const value = options.headers[key];
30
+ if (!value)
31
+ continue;
32
+ headers[key] = value;
33
+ }
34
+ }
35
+ if (options.type) {
36
+ let contentType = null;
37
+ let accept = null;
38
+ switch (options.type) {
39
+ case "XML":
40
+ contentType = "application/xml; charset=utf-8";
41
+ accept = "application/xml, text/xml, */*; q=0.01";
42
+ break;
43
+ case "JSON":
44
+ contentType = "application/json; charset=utf-8";
45
+ accept = "application/json, text/json, */*; q=0.01";
46
+ body = JSON.stringify(body);
47
+ break;
48
+ case "FORM":
49
+ // Convert HTMLFormElement to FormData first, then URL-encode both paths
50
+ if (body instanceof HTMLFormElement)
51
+ body = new FormData(body);
52
+ if (body instanceof FormData) {
53
+ body = helpers.encodeForm(body);
54
+ contentType = FORM_URL;
55
+ }
56
+ break;
57
+ case "FORMDATA":
58
+ break;
59
+ case "TEXT":
60
+ contentType = "text/plain";
61
+ break;
62
+ }
63
+ if (accept)
64
+ headers["Accept"] = accept;
65
+ if (contentType)
66
+ headers['Content-Type'] = contentType;
67
+ }
68
+ return {
69
+ headers,
70
+ body
71
+ };
72
+ };
73
+ var internals = {
74
+ DEFAULT_TIMEOUT,
75
+ FORM_URL,
76
+ FORM_DATA,
77
+ detectRequestType,
78
+ prepareRequest
79
+ };
80
+
81
+ exports.default = internals;
82
+ //# sourceMappingURL=internals.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"internals.js","sources":["../../../source/internals.ts"],"sourcesContent":[null],"names":["helpers.encodeForm"],"mappings":";;;;;;AAGA,MAAM,eAAe,GAAG,KAAK;AAC7B,MAAM,QAAQ,GAAG,mCAAmC;AACpD,MAAM,SAAS,GAAG,qBAAqB;AAEvC,MAAM,iBAAiB,GAAG,CAAC,OAAoB,KAAI;IAClD,IAAI,CAAC,OAAO,CAAC,IAAI,IAAI,OAAO,CAAC,IAAI,EAAE;AAClC,QAAA,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI;QACzB,IAAI,IAAI,YAAY,IAAI;AACvB,YAAA,OAAO,CAAC,IAAI,GAAG,MAAM;aACjB,IAAI,IAAI,YAAY,QAAQ;AAChC,YAAA,OAAO,CAAC,IAAI,GAAG,UAAU;aACrB,IAAI,IAAI,YAAY,eAAe;AACvC,YAAA,OAAO,CAAC,IAAI,GAAG,MAAM;aACjB,IAAI,IAAI,YAAY,MAAM;AAC9B,YAAA,OAAO,CAAC,IAAI,GAAG,MAAM;aACjB,IAAI,OAAO,IAAI,KAAK,QAAQ;AAChC,YA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,cAAc,CAAC,GAAG,WAAW;IACvC;IAEA,OAAO;QACN,OAAO;QACP;KACA;AACF,CAAC;AAED,gBAAe;IACd,eAAe;IACf,QAAQ;IACR,SAAS;IACT,iBAAiB;IACjB;CACA;;;;"}
@@ -0,0 +1,138 @@
1
+ 'use strict';
2
+
3
+ var request = require('./request.js');
4
+
5
+ /** Queue that executes AJAX requests one at a time, in the order they were added. */
6
+ class AjaxQueue {
7
+ _options;
8
+ _requests = [];
9
+ _current = null;
10
+ _destroyed = false;
11
+ /** @param options Optional queue-wide hooks. */
12
+ constructor(options) {
13
+ this._options = options ?? {};
14
+ }
15
+ /** Number of requests waiting in the queue (excluding the one currently executing). */
16
+ get length() { return this._requests.length; }
17
+ /** `true` when nothing is queued and no request is currently executing. */
18
+ get isFree() { return !this._requests.length && !this._current; }
19
+ /** `true` when no requests are waiting in the queue (a request may still be executing). */
20
+ get isEmpty() { return !this._requests.length; }
21
+ /**
22
+ * Adds a request to the queue. Starts executing immediately if the queue is idle.
23
+ *
24
+ * @param request Request options; its `success`/`error` callbacks are invoked as usual.
25
+ * @param abortSignal Optional signal used to cancel this specific request.
26
+ * @throws If the queue has been destroyed.
27
+ */
28
+ push(request, abortSignal) {
29
+ if (this._destroyed)
30
+ throw new Error("AjaxQueue is destroyed.");
31
+ this._requests.push({ request, cancel: abortSignal });
32
+ if (!this._current)
33
+ this.__execute();
34
+ }
35
+ /**
36
+ * Adds a request to the queue and returns a promise for its response.
37
+ *
38
+ * Wraps {@link push}; the request's own `success`/`error` callbacks are still invoked,
39
+ * then the promise resolves with the response or rejects with the failure reason.
40
+ *
41
+ * @param request Request options.
42
+ * @param abortSignal Optional signal used to cancel this specific request.
43
+ * @returns A promise resolving with the {@link AjaxResponse}.
44
+ */
45
+ enqueue(request, abortSignal) {
46
+ const { success, error } = request;
47
+ return new Promise((resolve, reject) => {
48
+ request.success = (response) => {
49
+ if (success)
50
+ success(response);
51
+ resolve(response);
52
+ };
53
+ request.error = (request, reason) => {
54
+ if (error)
55
+ error(request, reason);
56
+ reject(reason);
57
+ };
58
+ this.push(request, abortSignal);
59
+ });
60
+ }
61
+ /** @deprecated Renamed to {@link enqueue}. */
62
+ enque(request, abortSignal) {
63
+ return this.enqueue(request, abortSignal);
64
+ }
65
+ /**
66
+ * Clears all queued (not-yet-started) requests.
67
+ *
68
+ * @param cancelCurrentRequest When `true`, also aborts the request currently executing.
69
+ */
70
+ reset(cancelCurrentRequest = false) {
71
+ this._requests = [];
72
+ const current = this._current;
73
+ this._current = null;
74
+ if (cancelCurrentRequest && current)
75
+ current.abort?.abort("ResetAjaxQueue");
76
+ }
77
+ /** Destroys the queue: clears pending requests and aborts the current one. Subsequent {@link push} calls throw. */
78
+ destroy() {
79
+ if (this._destroyed)
80
+ return;
81
+ this._destroyed = true;
82
+ this._requests = [];
83
+ if (this._current) {
84
+ this._current.abort?.abort("DestroyAjaxQueue");
85
+ this._current = null;
86
+ }
87
+ }
88
+ __execute() {
89
+ if (this._destroyed)
90
+ return;
91
+ if (this._current)
92
+ throw new Error("AjaxQueue currently is executing.");
93
+ const task = this._current = this._requests.shift() ?? null;
94
+ if (task) {
95
+ if (this._options.canRequest && this._options.canRequest(task.request) === false) {
96
+ this.__next(task);
97
+ return;
98
+ }
99
+ if (task.request.abort?.aborted || task.cancel?.aborted) {
100
+ const err = new Error("Request cancelled");
101
+ task.request.error?.(task.request, err);
102
+ task.result = Promise.reject(err);
103
+ }
104
+ else {
105
+ task.abort = new AbortController();
106
+ task.result = request.request(task.request, task.cancel ? AbortSignal.any([task.abort.signal, task.cancel]) : task.abort.signal);
107
+ }
108
+ task.result
109
+ .then(response => {
110
+ if (this._destroyed)
111
+ return;
112
+ if (this._options.successRequest)
113
+ this._options.successRequest(task.request, response);
114
+ })
115
+ .catch(reason => {
116
+ if (this._destroyed)
117
+ return;
118
+ if (this._options.errorRequest)
119
+ this._options.errorRequest(task.request, reason);
120
+ })
121
+ .finally(() => this.__next(task));
122
+ }
123
+ }
124
+ // completedTask is the task that finished — if _current already changed
125
+ // (e.g. reset(true) was called and a new push() started a new task), bail out
126
+ // to avoid clearing the new task's reference or double-executing the queue.
127
+ __next(completedTask) {
128
+ if (this._destroyed)
129
+ return;
130
+ if (this._current !== completedTask)
131
+ return;
132
+ this._current = null;
133
+ this.__execute();
134
+ }
135
+ }
136
+
137
+ exports.AjaxQueue = AjaxQueue;
138
+ //# sourceMappingURL=queue.js.map