@brandup/ui-ajax 2.0.1 → 2.0.3

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.
@@ -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
@@ -0,0 +1 @@
1
+ {"version":3,"file":"queue.js","sources":["../../../source/queue.ts"],"sourcesContent":[null],"names":["request"],"mappings":";;;;AAGA;MACa,SAAS,CAAA;AACb,IAAA,QAAQ;IACR,SAAS,GAAuB,EAAE;IAClC,QAAQ,GAAuB,IAAI;IACnC,UAAU,GAAG,KAAK;;AAG1B,IAAA,WAAA,CAAY,OAA0B,EAAA;AACrC,QAAA,IAAI,CAAC,QAAQ,GAAG,OAAO,IAAI,EAAE;IAC9B;;IAGA,IAAI,MAAM,GAAA,EAAa,OAAO,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC;;AAErD,IAAA,IAAI,MAAM,GAAA,EAAc,OAAO,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;;IAEzE,IAAI,OAAO,GAAA,EAAc,OAAO,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC;AAExD;;;;;;AAMG;IACH,IAAI,CAAC,OAAoB,EAAE,WAAyB,EAAA;QACnD,IAAI,IAAI,CAAC,UAAU;AAClB,YAAA,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC;AAE3C,QAAA,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,CAAC;QAErD,IAAI,CAAC,IAAI,CAAC,QAAQ;YACjB,IAAI,CAAC,SAAS,EAAE;IAClB;AAEA;;;;;;;;;AASG;IACH,OAAO,CAAkB,OAAoB,EAAE,WAAyB,EAAA;AACvE,QAAA,MAAM,EAAE,OAAO,EAAE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AC,cAAc;oBAC/B,IAAI,CAAC,QAAQ,CAAC,cAAc,CAAC,IAAI,CAAC,OAAO,EAAE,QAAQ,CAAC;AACtD,YAAA,CAAC;iBACA,KAAK,CAAC,MAAM,IAAG;gBACf,IAAI,IAAI,CAAC,UAAU;oBAClB;AAED,gBAAA,IAAI,IAAI,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,CAAC,IAAI,CAAC,CAAC;QACnC;IACD;;;;AAKQ,IAAA,MAAM,CAAC,aAA0B,EAAA;QACxC,IAAI,IAAI,CAAC,UAAU;YAClB;AACD,QAAA,IAAI,IAAI,CAAC,QAAQ,KAAK,aAAa;YAClC;AAED,QAAA,IAAI,CAAC,QAAQ,GAAG,IAAI;QACpB,IAAI,CAAC,SAAS,EAAE;IACjB;AACA;;;;"}
@@ -0,0 +1,108 @@
1
+ 'use strict';
2
+
3
+ var helpers = require('./helpers.js');
4
+ var internals = require('./internals.js');
5
+
6
+ /**
7
+ * Performs an AJAX request using the Fetch API.
8
+ *
9
+ * The response body is parsed according to its `Content-Type`: JSON, `text/html`,
10
+ * `text/plain`, otherwise a `Blob`. `disableCache` maps to `cache: "no-store"`.
11
+ * The request is aborted when its `timeout` elapses (defaults to 30000 ms), or when
12
+ * `options.abort` or the `abortSignal` argument signals.
13
+ *
14
+ * @param options Request options. `GET`/`HEAD` requests must not carry `data`.
15
+ * @param abortSignal Optional additional signal used to cancel the request.
16
+ * @returns The parsed response. `options.success` is also invoked before resolving.
17
+ * @throws On network/abort/timeout errors, or for unsupported (opaque) response types; `options.error` is invoked before re-throwing.
18
+ */
19
+ async function request(options, abortSignal) {
20
+ let { mode, credentials = "include" } = options;
21
+ let url = options.url || location.href;
22
+ url = helpers.addQuery(url, options.query);
23
+ const method = options.method ? options.method.toUpperCase() : "GET";
24
+ let body = options.data;
25
+ if (body && (method === "GET" || method === "HEAD"))
26
+ throw new Error(`${method} method does not support a request body.`);
27
+ internals.default.detectRequestType(options);
28
+ const prepared = internals.default.prepareRequest(options, body);
29
+ const abortSignals = [AbortSignal.timeout(options.timeout ?? internals.default.DEFAULT_TIMEOUT)];
30
+ if (options.abort)
31
+ abortSignals.push(options.abort);
32
+ if (abortSignal)
33
+ abortSignals.push(abortSignal);
34
+ try {
35
+ const response = await fetch(url, {
36
+ method,
37
+ headers: new Headers(prepared.headers),
38
+ cache: options.disableCache ? "no-store" : "default",
39
+ mode,
40
+ credentials,
41
+ redirect: "follow",
42
+ signal: AbortSignal.any(abortSignals),
43
+ body: prepared.body
44
+ });
45
+ let result;
46
+ switch (response.type) {
47
+ case "basic":
48
+ case "default":
49
+ case "cors": {
50
+ let responseData = null;
51
+ let responseType = "none";
52
+ let contentType = response.headers.get("content-type");
53
+ if (!response.redirected && response.body) {
54
+ if (contentType) {
55
+ const ctSplitIndex = contentType.indexOf(";");
56
+ if (ctSplitIndex > 0)
57
+ contentType = contentType.substring(0, ctSplitIndex);
58
+ if (contentType.includes("json")) {
59
+ responseType = "json";
60
+ responseData = await response.json();
61
+ }
62
+ else if (contentType.includes("text/html")) {
63
+ responseType = "html";
64
+ responseData = await response.text();
65
+ }
66
+ else if (contentType.includes("text/plain")) {
67
+ responseType = "text";
68
+ responseData = await response.text();
69
+ }
70
+ else {
71
+ responseType = "blob";
72
+ responseData = await response.blob();
73
+ }
74
+ }
75
+ }
76
+ result = {
77
+ status: response.status,
78
+ url: response.url,
79
+ redirected: response.redirected,
80
+ type: responseType,
81
+ contentType,
82
+ headers: response.headers,
83
+ data: responseData,
84
+ state: options.state
85
+ };
86
+ break;
87
+ }
88
+ case "opaqueredirect":
89
+ case "opaque":
90
+ throw new Error(`Not supported response type: ${response.type}`);
91
+ case "error":
92
+ throw new Error("Response error.");
93
+ default:
94
+ throw new Error(`Unknown response type: ${response.type}`);
95
+ }
96
+ if (options.success)
97
+ options.success(result);
98
+ return result;
99
+ }
100
+ catch (error) {
101
+ if (options.error)
102
+ options.error(options, error);
103
+ throw error;
104
+ }
105
+ }
106
+
107
+ exports.request = request;
108
+ //# sourceMappingURL=request.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"request.js","sources":["../../../source/request.ts"],"sourcesContent":[null],"names":["helpers.addQuery","internals"],"mappings":";;;;;AAIA;;;;;;;;;;;;AAYG;AACI,eAAe,OAAO,CAA4B,OAA4B,EAAE,WAAyB,EAAA;IAC/G,IAAI,EAAE,IAAI,EAAE,WAAW,GAAG,SAAS,EAAE,GAAG,OAAO;IAC/C,IAAI,GAAG,GAAG,OAAO,CAAC,GAAG,IAAI,QAAQ,CAAC,IAAI;IACtC,GAAG,GAAGA,gBAAgB,CAAC,GAAG,EAAE,OAAO,CAAC,KAAK,CAAC;AAE1C,IAAA,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,WAAW,EAAE,GAAG,KAAK;AAEpE,IAAA,IAAI,IAAI,GAAQ,OAAO,CAAC,IAAI;IAC5B,IAAI,IAAI,KAAK,MAAM,KAAK,KAAK,IAAI,MAAM,KAAK,MAAM,CAAC;AAClD,QAAA,MAAM,IAAI,KAAK,CAAC,GAAG,MAAM,CAAA,wCAAA,CAA0C,CAAC;AAErE,IAAAC,iBAAS,CAAC,iBAAiB,CAAC,OAAO,CAAC;IACpC,MAAM,QAAQ,GAAGA,iBAAS,CAAC,cAAc,CAAC,OAAO,EAAE,IAAI,CAAC;AAExD,IAAA,MAAM,YAAY,GAAG,CAAC,WAAW,CAAC,OAAO,CAAC,OAAO,CAAC,OAAO,IAAIA,iBAAS,CAAC,eAAe,CAAC,CAAC;IACxF,IAAI,OAAO,CAAC,KAAK;AAChB,QAAA,YAAY,CAAC,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC;AACjC,IAAA,IAAI,WAAW;AACd,QAAA,YAAY,CAAC,IAAI,CAAC,W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XMLHttpRequest`, always with credentials.
6
+ *
7
+ * The response body is parsed by `Content-Type` into JSON, `text/plain` or `text/html`;
8
+ * unlike the fetch-based {@link request}, there is no `blob` response type. `disableCache`
9
+ * appends a `_=<timestamp>` cache-busting query parameter (rather than `cache: "no-store"`).
10
+ * Results and errors are delivered through `options.success` / `options.error`; this
11
+ * function does not return a promise.
12
+ *
13
+ * @param options Request options. `GET` requests must not carry `data`. `timeout` of `0` disables the timeout.
14
+ * @returns The underlying `XMLHttpRequest`, which can be used to `abort()` the request.
15
+ */
16
+ const ajaxRequest = (options) => {
17
+ let url = options.url || location.href;
18
+ let { query } = options;
19
+ if (options.disableCache) {
20
+ if (!query)
21
+ query = {};
22
+ query["_"] = Date.now().toString();
23
+ }
24
+ url = addQuery(url, query);
25
+ const method = options.method ? options.method.toUpperCase() : "GET";
26
+ if (options.data && (method === "GET" || method === "HEAD"))
27
+ throw new Error(`${method} method does not support a request body.`);
28
+ internals.detectRequestType(options);
29
+ const prepared = internals.prepareRequest(options, options.data);
30
+ const xhr = new XMLHttpRequest();
31
+ xhr.withCredentials = true;
32
+ if (options.timeout === 0 || options.timeout)
33
+ xhr.timeout = options.timeout;
34
+ xhr.onreadystatechange = (_e) => {
35
+ if (xhr.readyState !== XMLHttpRequest.DONE)
36
+ return;
37
+ if (options.success) {
38
+ let responseData = null;
39
+ let responseType = "none";
40
+ const contentType = xhr.getResponseHeader("Content-Type");
41
+ if (xhr.response && contentType) {
42
+ if (contentType.includes("json")) {
43
+ responseType = "json";
44
+ try {
45
+ responseData = JSON.parse(xhr.responseText);
46
+ }
47
+ catch (e) {
48
+ if (options.error)
49
+ options.error(options, e);
50
+ return;
51
+ }
52
+ }
53
+ else if (contentType.includes("text/plain")) {
54
+ responseType = "text";
55
+ responseData = xhr.responseText;
56
+ }
57
+ else if (contentType.includes("text/html")) {
58
+ responseType = "html";
59
+ responseData = xhr.responseText;
60
+ }
61
+ }
62
+ const xhrRef = xhr;
63
+ const headers = {
64
+ get(name) {
65
+ return xhrRef.getResponseHeader(name);
66
+ },
67
+ has(name) {
68
+ return !!xhrRef.getResponseHeader(name);
69
+ },
70
+ forEach(callbackfn, thisArg) {
71
+ xhrRef.getAllResponseHeaders()
72
+ .trim()
73
+ .split(/[\r\n]+/)
74
+ .filter(Boolean)
75
+ .forEach(line => {
76
+ const idx = line.indexOf(": ");
77
+ const key = idx >= 0 ? line.substring(0, idx).toLowerCase() : line.toLowerCase();
78
+ const value = idx >= 0 ? line.substring(idx + 2) : "";
79
+ callbackfn.call(thisArg, value, key, {});
80
+ });
81
+ }
82
+ };
83
+ options.success({
84
+ status: xhr.status,
85
+ url: xhr.responseURL,
86
+ redirected: false,
87
+ type: responseType,
88
+ contentType,
89
+ headers,
90
+ data: responseData,
91
+ state: options.state
92
+ });
93
+ }
94
+ };
95
+ xhr.onabort = (_e) => {
96
+ if (options.error)
97
+ options.error(options, new Error("Request aborted"));
98
+ };
99
+ xhr.onerror = (_e) => {
100
+ if (options.error)
101
+ options.error(options, new Error("Request network error"));
102
+ };
103
+ xhr.ontimeout = (_e) => {
104
+ if (options.error)
105
+ options.error(options, new Error("Request timeout"));
106
+ };
107
+ xhr.open(method, url, true);
108
+ xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
109
+ for (const key in prepared.headers) {
110
+ const value = prepared.headers[key];
111
+ if (!value)
112
+ continue;
113
+ xhr.setRequestHeader(key, value);
114
+ }
115
+ if (method === "GET")
116
+ xhr.send();
117
+ else
118
+ xhr.send(prepared.body);
119
+ return xhr;
120
+ };
121
+
122
+ export { ajaxRequest };
123
+ //# sourceMappingURL=ajax-request.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ajax-request.js","sources":["../../../source/ajax-request.ts"],"sourcesContent":[null],"names":["helpers.addQuery"],"mappings":";;;AAIA;;;;;;;;;;;AAWG;AACI,MAAM,WAAW,GAAG,CAAC,OAAoB,KAAI;IACnD,IAAI,GAAG,GAAG,OAAO,CAAC,GAAG,IAAI,QAAQ,CA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CAAC,kBAAkB,EAAE,gBAAgB,CAAC;AAE1D,IAAA,KAAK,MAAM,GAAG,IAAI,QAAQ,CAAC,OAAO,EAAE;QACnC,MAAM,KAAK,GAAG,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC;AACnC,QAAA,IAAI,CAAC,KAAK;YACT;AACD,QAAA,GAAG,CAAC,gBAAgB,CAAC,GAAG,EAAE,KAAK,CAAC;IACjC;IAEA,IAAI,MAAM,KAAK,KAAK;QACnB,GAAG,CAAC,IAAI,EAAE;;AAEV,QAAA,GAAG,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC;AAExB,IAAA,OAAO,GAAG;AACX;;;;"}
@@ -0,0 +1,65 @@
1
+ /**
2
+ * Builds a `URLSearchParams` from query data or a `FormData`.
3
+ *
4
+ * Array values produce repeated keys; `null` values are skipped.
5
+ *
6
+ * @param query Source parameters.
7
+ * @returns The populated `URLSearchParams` (empty when `query` is nullish).
8
+ */
9
+ const createQuery = (query) => {
10
+ const urlParams = new URLSearchParams();
11
+ if (!query)
12
+ return urlParams;
13
+ if (query instanceof FormData) {
14
+ query.forEach((value, key) => {
15
+ if (!key)
16
+ return;
17
+ urlParams.append(key, value.toString());
18
+ });
19
+ }
20
+ else {
21
+ for (const key in query) {
22
+ const val = query[key];
23
+ if (val === null)
24
+ continue;
25
+ if (Array.isArray(val))
26
+ val.forEach(v => urlParams.append(key, v));
27
+ else
28
+ urlParams.append(key, val);
29
+ }
30
+ }
31
+ return urlParams;
32
+ };
33
+ /**
34
+ * Appends query parameters to a URL, choosing `?` or `&` as appropriate.
35
+ *
36
+ * @param url Base URL.
37
+ * @param query Parameters to append; nothing is added when empty.
38
+ * @returns The resulting URL.
39
+ */
40
+ const addQuery = (url, query) => {
41
+ if (query) {
42
+ const urlParams = createQuery(query);
43
+ if (urlParams.size) {
44
+ if (url.indexOf("?") === -1)
45
+ url += "?";
46
+ else
47
+ url += "&";
48
+ url += urlParams.toString();
49
+ }
50
+ }
51
+ return url;
52
+ };
53
+ /**
54
+ * Encodes a `FormData` as an `application/x-www-form-urlencoded` string.
55
+ *
56
+ * @param data Form data to encode.
57
+ * @returns The URL-encoded form string.
58
+ */
59
+ const encodeForm = (data) => {
60
+ const query = createQuery(data);
61
+ return query.toString();
62
+ };
63
+
64
+ export { addQuery, createQuery, encodeForm };
65
+ //# sourceMappingURL=helpers.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"helpers.js","sources":["../../../source/helpers.ts"],"sourcesContent":[null],"names":[],"mappings":"AAEA;;;;;;;AAOG;AACH,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,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC;;AAE1C,gBAAA,SAAS,CAAC,MAAM,CAAC,GAAG,EAAE,GAAG,CAAC;QAC5B;IACD;AAEA,IAAA,OAAO,SAAS;AACjB;AAEA;;;;;;AAMG;AACH,MAAM,QAAQ,GAAG,CAAC,GAAW,EAAE,KAAmC,KAAI;IACrE,IAAI,KAAK,EAA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