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