@wcstack/fetch 4.23.0 → 4.24.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/README.ja.md +78 -16
- package/README.md +78 -16
- package/dist/auto.min.js +1 -1
- package/dist/auto.min.js.map +1 -1
- package/dist/index.d.ts +100 -3
- package/dist/index.esm.js +256 -21
- package/dist/index.esm.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -158,6 +158,18 @@ interface WcsFetchCoreValues<T = unknown> {
|
|
|
158
158
|
interface WcsFetchValues<T = unknown> extends WcsFetchCoreValues<T> {
|
|
159
159
|
trigger: boolean;
|
|
160
160
|
}
|
|
161
|
+
/**
|
|
162
|
+
* What `<wcs-fetch>`'s `fetch(url, init)` takes beside the url, for that request only — a command token carries
|
|
163
|
+
* it: `this.$command.load.emit("/api/users", { method: "POST", body: user })`.
|
|
164
|
+
*/
|
|
165
|
+
interface WcsFetchCommandInit {
|
|
166
|
+
/** The method, in place of the element's `method`. */
|
|
167
|
+
method?: string;
|
|
168
|
+
/** Headers added to the `<wcs-fetch-header>` ones (a same name replaces it). */
|
|
169
|
+
headers?: Record<string, string>;
|
|
170
|
+
/** The body, in place of the element's `body` — which is then kept for the next request. */
|
|
171
|
+
body?: unknown;
|
|
172
|
+
}
|
|
161
173
|
|
|
162
174
|
declare function bootstrapFetch(userConfig?: IWritableConfig, registry?: CustomElementRegistry): void;
|
|
163
175
|
|
|
@@ -370,18 +382,64 @@ declare class Fetch extends HTMLElement {
|
|
|
370
382
|
* change starts, which `[wcs/fetch-auto-unsafe]` reports.
|
|
371
383
|
*/
|
|
372
384
|
private _warnBodyGone;
|
|
373
|
-
|
|
385
|
+
/**
|
|
386
|
+
* Runs a request. With no argument it is the element's: its `url`, `method`, headers and `body`.
|
|
387
|
+
*
|
|
388
|
+
* `url` and `init` are for this request only — the element's attributes do not change. They are what a command
|
|
389
|
+
* token carries (`this.$command.load.emit("/api/users", { method: "GET" })` with `command.fetch: $command.load`):
|
|
390
|
+
* a command runs before the bindings of its update, so a `url` written in the same handler has not reached the
|
|
391
|
+
* element yet, and the request went to the one before (docs/seam-apps.md, town-archive). `init` takes `method`,
|
|
392
|
+
* `headers` (added to the `<wcs-fetch-header>` ones, replacing a same name) and `body` (in place of the
|
|
393
|
+
* element's, which is then kept for the next request). Anything that is not a string in `url`'s place — the
|
|
394
|
+
* event an `on…: $command.x` binding emits first — is ignored, and `init` with it.
|
|
395
|
+
*/
|
|
396
|
+
fetch(url?: unknown, init?: unknown): Promise<any>;
|
|
374
397
|
attributeChangedCallback(name: string, _oldValue: string | null, _newValue: string | null): void;
|
|
375
398
|
connectedCallback(): void;
|
|
376
399
|
connectedMoveCallback(): void;
|
|
377
400
|
disconnectedCallback(): void;
|
|
378
401
|
}
|
|
379
402
|
|
|
403
|
+
/**
|
|
404
|
+
* Runs a `<wcs-fetch>` when this element — a sentinel after a list — comes on screen.
|
|
405
|
+
*
|
|
406
|
+
* An IntersectionObserver reports a change of visibility and nothing else, and an element that only acted on that
|
|
407
|
+
* report did not load in three cases (docs/seam-apps.md, town-archive):
|
|
408
|
+
* - the sentinel reported before the page had written the target's `url` (state binds a moment after the elements
|
|
409
|
+
* are defined): the request was refused, and the sentinel, still on screen, never reported again;
|
|
410
|
+
* - a page arrived and did not push the sentinel out — a tall window, a zoomed-out page, a short page;
|
|
411
|
+
* - the `url` changed with the sentinel on screen — a filter, with the list emptied — at rest or while a request
|
|
412
|
+
* was in flight.
|
|
413
|
+
*
|
|
414
|
+
* So, with the sentinel on screen, the element also runs the target's request
|
|
415
|
+
* - when its `url` is written or changed while no request is in flight (the first page; a filter);
|
|
416
|
+
* - when a request ends and the `url` changed while it was in flight (a filter typed meanwhile);
|
|
417
|
+
* - when a request ends without an answer — aborted — and the `url` is another one (a page that aborts the request
|
|
418
|
+
* on its way when the filter changes);
|
|
419
|
+
* - when a request ends, the page has added rows (the sentinel moved) and moved the `url` on.
|
|
420
|
+
*
|
|
421
|
+
* It never runs a `url` again from there: a failed request waits for the user to scroll away and back, or for the
|
|
422
|
+
* page's retry. And a response that added nothing ends it, whatever the page does to the `url`: a page that counts
|
|
423
|
+
* its page number up on an empty answer does not ask for ever.
|
|
424
|
+
*/
|
|
380
425
|
declare class InfiniteScroll extends HTMLElement {
|
|
381
426
|
static get observedAttributes(): string[];
|
|
382
427
|
private _observer;
|
|
383
428
|
private _done;
|
|
384
429
|
private _warnedTarget;
|
|
430
|
+
private _warnedRoot;
|
|
431
|
+
private _warnedMargin;
|
|
432
|
+
private _rootElement;
|
|
433
|
+
private _inView;
|
|
434
|
+
private _fetch;
|
|
435
|
+
private _urlObserver;
|
|
436
|
+
private _asked;
|
|
437
|
+
private _startedAt;
|
|
438
|
+
private _changedInFlight;
|
|
439
|
+
private _answered;
|
|
440
|
+
private _settling;
|
|
441
|
+
private _onResponse;
|
|
442
|
+
private _onLoadingChanged;
|
|
385
443
|
get target(): string;
|
|
386
444
|
set target(value: string);
|
|
387
445
|
get root(): string | null;
|
|
@@ -399,8 +457,47 @@ declare class InfiniteScroll extends HTMLElement {
|
|
|
399
457
|
attributeChangedCallback(): void;
|
|
400
458
|
private _observe;
|
|
401
459
|
private _disconnectObserver;
|
|
460
|
+
/** The observer's report: the last entry is the sentinel's state now, and coming on screen runs the request. */
|
|
461
|
+
private _report;
|
|
462
|
+
/**
|
|
463
|
+
* Takes the reports the browser has made and not delivered yet, before the sentinel's state is read outside a
|
|
464
|
+
* report. While the user scrolls, the report that a page pushed the sentinel out arrives a frame later than
|
|
465
|
+
* otherwise — after the frames the end of a request waits (Chromium, Firefox): the sentinel read as still on
|
|
466
|
+
* screen, and a page the window did not need was asked for (docs/seam-apps.md, landing town-archive).
|
|
467
|
+
*/
|
|
468
|
+
private _sync;
|
|
469
|
+
/**
|
|
470
|
+
* The scroll container: the element with that id, or — as `<wcs-intersect root>` spells it — the first one the
|
|
471
|
+
* value matches as a selector. A root that is given and not found, or that does not scroll, is said once per
|
|
472
|
+
* value: the viewport took its place without a word, and a root that does not clip its content counts
|
|
473
|
+
* everything in it as on screen (`root="main"` loaded page after page).
|
|
474
|
+
*/
|
|
402
475
|
private _resolveRoot;
|
|
403
|
-
private
|
|
476
|
+
private _resolveTarget;
|
|
477
|
+
/** Follows `target`'s url and requests (or stops following, with null). */
|
|
478
|
+
private _follow;
|
|
479
|
+
/** Where the sentinel is in what scrolls: rows added before it move it, scrolling does not. */
|
|
480
|
+
private _position;
|
|
481
|
+
/** A request started on the target (this element's, or the page's own). */
|
|
482
|
+
private _started;
|
|
483
|
+
/**
|
|
484
|
+
* The request ended: once the page has had its frames to add rows and move the url on, the sentinel — if it is
|
|
485
|
+
* still on screen — runs the next request when the url changed while this one was in flight, when this one had no
|
|
486
|
+
* answer (aborted: a url written right after it is the page's own change, not its reaction to a response), or
|
|
487
|
+
* when this one added rows. A response that added nothing ends it there.
|
|
488
|
+
*/
|
|
489
|
+
private _ended;
|
|
490
|
+
/**
|
|
491
|
+
* The target's url was written. In flight, and while the page settles after a response, the end of that request
|
|
492
|
+
* decides (_ended). At rest it is the page's own change — the first url, a filter: with the sentinel on screen
|
|
493
|
+
* it is asked for, at once when nothing was asked yet, otherwise once the emptied list is laid out.
|
|
494
|
+
*/
|
|
495
|
+
private _urlChanged;
|
|
496
|
+
/**
|
|
497
|
+
* Runs the target's request. `edge`: the sentinel came on screen (a user who scrolls away and back asks again,
|
|
498
|
+
* also for the same url — the way a failed request is tried again); otherwise only a url not asked yet.
|
|
499
|
+
*/
|
|
500
|
+
private _run;
|
|
404
501
|
}
|
|
405
502
|
|
|
406
503
|
/**
|
|
@@ -459,4 +556,4 @@ declare global {
|
|
|
459
556
|
}
|
|
460
557
|
|
|
461
558
|
export { FetchCore, TRUSTED_TYPES_POLICY_SLOT, WCS_FETCH_ERROR_CODE, Fetch as WcsFetch, FetchBody as WcsFetchBody, FetchHeader as WcsFetchHeader, InfiniteScroll as WcsInfiniteScroll, bootstrapFetch, getConfig, getTrustedTypesPolicy, setTrustedTypesPolicy };
|
|
462
|
-
export type { FetchRequestOptions, IWcsTrustedTypesPolicy, IWritableConfig, IWritableTagNames, WcsFetchCoreValues, WcsFetchHttpError, WcsFetchValues, WcsIoErrorInfo, WcsIoErrorPhase };
|
|
559
|
+
export type { FetchRequestOptions, IWcsTrustedTypesPolicy, IWritableConfig, IWritableTagNames, WcsFetchCommandInit, WcsFetchCoreValues, WcsFetchHttpError, WcsFetchValues, WcsIoErrorInfo, WcsIoErrorPhase };
|
package/dist/index.esm.js
CHANGED
|
@@ -68,7 +68,7 @@ function setConfig(partialConfig) {
|
|
|
68
68
|
frozenConfig = null;
|
|
69
69
|
}
|
|
70
70
|
|
|
71
|
-
var pkg = {"name":"@wcstack/fetch","version":"4.
|
|
71
|
+
var pkg = {"name":"@wcstack/fetch","version":"4.24.1"};
|
|
72
72
|
|
|
73
73
|
// ===========================================================================
|
|
74
74
|
// AUTO-GENERATED FILE - DO NOT EDIT.
|
|
@@ -1529,20 +1529,38 @@ class Fetch extends HTMLElement {
|
|
|
1529
1529
|
"it wrote last, so the same string on a retry never arrives: write a new object each time " +
|
|
1530
1530
|
"(this.payload = { ...entry }), or null between two sends. (Said once until a body is sent again.)", this);
|
|
1531
1531
|
}
|
|
1532
|
-
|
|
1532
|
+
/**
|
|
1533
|
+
* Runs a request. With no argument it is the element's: its `url`, `method`, headers and `body`.
|
|
1534
|
+
*
|
|
1535
|
+
* `url` and `init` are for this request only — the element's attributes do not change. They are what a command
|
|
1536
|
+
* token carries (`this.$command.load.emit("/api/users", { method: "GET" })` with `command.fetch: $command.load`):
|
|
1537
|
+
* a command runs before the bindings of its update, so a `url` written in the same handler has not reached the
|
|
1538
|
+
* element yet, and the request went to the one before (docs/seam-apps.md, town-archive). `init` takes `method`,
|
|
1539
|
+
* `headers` (added to the `<wcs-fetch-header>` ones, replacing a same name) and `body` (in place of the
|
|
1540
|
+
* element's, which is then kept for the next request). Anything that is not a string in `url`'s place — the
|
|
1541
|
+
* event an `on…: $command.x` binding emits first — is ignored, and `init` with it.
|
|
1542
|
+
*/
|
|
1543
|
+
async fetch(url, init) {
|
|
1544
|
+
const given = typeof url === "string" && url !== "";
|
|
1545
|
+
const requestUrl = given ? url : this.url;
|
|
1546
|
+
const options = (given && init !== null && typeof init === "object" ? init : {});
|
|
1533
1547
|
// Record the url for the auto-fetch same-value guard. Every fetch (explicit
|
|
1534
1548
|
// included) updates it so a later auto-write of the same url is treated as a
|
|
1535
1549
|
// no-op rather than a duplicate request.
|
|
1536
|
-
this._lastFetchedUrl =
|
|
1550
|
+
this._lastFetchedUrl = requestUrl;
|
|
1537
1551
|
// ready again (a request starts): a trigger refused in the next outage is said again
|
|
1538
|
-
if (
|
|
1552
|
+
if (requestUrl)
|
|
1539
1553
|
this._toldTriggerWithoutUrl = false;
|
|
1540
|
-
const headers = this._collectHeaders();
|
|
1554
|
+
const headers = { ...this._collectHeaders(), ...(typeof options.headers === "object" ? options.headers : null) };
|
|
1541
1555
|
// Snapshot and reset `body` synchronously, before any await. The body is a
|
|
1542
1556
|
// one-shot input; resetting it after the await (when another caller may have
|
|
1543
1557
|
// already set a new body for the next request) would silently drop that value.
|
|
1544
|
-
|
|
1545
|
-
|
|
1558
|
+
// (A body the command brought is this request's: the element's waits for the next.)
|
|
1559
|
+
let bodySnapshot = options.body ?? null;
|
|
1560
|
+
if (bodySnapshot === null) {
|
|
1561
|
+
bodySnapshot = this._body;
|
|
1562
|
+
this._body = null;
|
|
1563
|
+
}
|
|
1546
1564
|
const { body, contentType } = this._collectBody(bodySnapshot);
|
|
1547
1565
|
const auto = this._auto;
|
|
1548
1566
|
this._auto = false;
|
|
@@ -1562,8 +1580,8 @@ class Fetch extends HTMLElement {
|
|
|
1562
1580
|
"This drops the multipart boundary the browser adds automatically; remove the " +
|
|
1563
1581
|
"Content-Type header (e.g. the <wcs-fetch-header>) to fix multipart uploads.");
|
|
1564
1582
|
}
|
|
1565
|
-
const result = await this._core.fetch(
|
|
1566
|
-
method: this.method,
|
|
1583
|
+
const result = await this._core.fetch(requestUrl, {
|
|
1584
|
+
method: typeof options.method === "string" && options.method ? options.method.toUpperCase() : this.method,
|
|
1567
1585
|
headers,
|
|
1568
1586
|
body,
|
|
1569
1587
|
contentType,
|
|
@@ -1702,6 +1720,30 @@ class FetchBody extends HTMLElement {
|
|
|
1702
1720
|
}
|
|
1703
1721
|
}
|
|
1704
1722
|
|
|
1723
|
+
/** Frames the page gets after a response to add its rows and move its url on, before the sentinel is looked at again. */
|
|
1724
|
+
const SETTLE_FRAMES = 3;
|
|
1725
|
+
/**
|
|
1726
|
+
* Runs a `<wcs-fetch>` when this element — a sentinel after a list — comes on screen.
|
|
1727
|
+
*
|
|
1728
|
+
* An IntersectionObserver reports a change of visibility and nothing else, and an element that only acted on that
|
|
1729
|
+
* report did not load in three cases (docs/seam-apps.md, town-archive):
|
|
1730
|
+
* - the sentinel reported before the page had written the target's `url` (state binds a moment after the elements
|
|
1731
|
+
* are defined): the request was refused, and the sentinel, still on screen, never reported again;
|
|
1732
|
+
* - a page arrived and did not push the sentinel out — a tall window, a zoomed-out page, a short page;
|
|
1733
|
+
* - the `url` changed with the sentinel on screen — a filter, with the list emptied — at rest or while a request
|
|
1734
|
+
* was in flight.
|
|
1735
|
+
*
|
|
1736
|
+
* So, with the sentinel on screen, the element also runs the target's request
|
|
1737
|
+
* - when its `url` is written or changed while no request is in flight (the first page; a filter);
|
|
1738
|
+
* - when a request ends and the `url` changed while it was in flight (a filter typed meanwhile);
|
|
1739
|
+
* - when a request ends without an answer — aborted — and the `url` is another one (a page that aborts the request
|
|
1740
|
+
* on its way when the filter changes);
|
|
1741
|
+
* - when a request ends, the page has added rows (the sentinel moved) and moved the `url` on.
|
|
1742
|
+
*
|
|
1743
|
+
* It never runs a `url` again from there: a failed request waits for the user to scroll away and back, or for the
|
|
1744
|
+
* page's retry. And a response that added nothing ends it, whatever the page does to the `url`: a page that counts
|
|
1745
|
+
* its page number up on an empty answer does not ask for ever.
|
|
1746
|
+
*/
|
|
1705
1747
|
class InfiniteScroll extends HTMLElement {
|
|
1706
1748
|
static get observedAttributes() {
|
|
1707
1749
|
return ["target", "root", "root-margin", "threshold", "disabled"];
|
|
@@ -1710,6 +1752,37 @@ class InfiniteScroll extends HTMLElement {
|
|
|
1710
1752
|
_done = false;
|
|
1711
1753
|
// the target last warned about (once per value)
|
|
1712
1754
|
_warnedTarget = null;
|
|
1755
|
+
// the root and the margin last warned about (once per value)
|
|
1756
|
+
_warnedRoot = null;
|
|
1757
|
+
_warnedMargin = null;
|
|
1758
|
+
// the scroll container in use (null: the viewport)
|
|
1759
|
+
_rootElement = null;
|
|
1760
|
+
// the sentinel's last report
|
|
1761
|
+
_inView = false;
|
|
1762
|
+
// the target whose url and requests it follows
|
|
1763
|
+
_fetch = null;
|
|
1764
|
+
_urlObserver = null;
|
|
1765
|
+
// the url of the request last started on the target (by this element or by the page)
|
|
1766
|
+
_asked = null;
|
|
1767
|
+
// where the sentinel was when that request started
|
|
1768
|
+
_startedAt = 0;
|
|
1769
|
+
// the url changed while that request was in flight
|
|
1770
|
+
_changedInFlight = false;
|
|
1771
|
+
// that request was answered (a response arrived, an HTTP error too): not aborted, not a network failure
|
|
1772
|
+
_answered = false;
|
|
1773
|
+
// a request has ended and the page is adding its rows (SETTLE_FRAMES)
|
|
1774
|
+
_settling = false;
|
|
1775
|
+
_onResponse = () => {
|
|
1776
|
+
this._answered = true;
|
|
1777
|
+
};
|
|
1778
|
+
_onLoadingChanged = (event) => {
|
|
1779
|
+
if (event.detail) {
|
|
1780
|
+
this._started();
|
|
1781
|
+
}
|
|
1782
|
+
else {
|
|
1783
|
+
this._ended();
|
|
1784
|
+
}
|
|
1785
|
+
};
|
|
1713
1786
|
get target() {
|
|
1714
1787
|
return this.getAttribute("target") || "";
|
|
1715
1788
|
}
|
|
@@ -1767,6 +1840,7 @@ class InfiniteScroll extends HTMLElement {
|
|
|
1767
1840
|
}
|
|
1768
1841
|
disconnectedCallback() {
|
|
1769
1842
|
this._disconnectObserver();
|
|
1843
|
+
this._follow(null);
|
|
1770
1844
|
}
|
|
1771
1845
|
attributeChangedCallback() {
|
|
1772
1846
|
if (this.isConnected) {
|
|
@@ -1778,15 +1852,27 @@ class InfiniteScroll extends HTMLElement {
|
|
|
1778
1852
|
if (this._done || this.disabled || !this.target || typeof IntersectionObserver === "undefined") {
|
|
1779
1853
|
return;
|
|
1780
1854
|
}
|
|
1781
|
-
this.
|
|
1782
|
-
|
|
1783
|
-
|
|
1855
|
+
this._rootElement = this._resolveRoot();
|
|
1856
|
+
try {
|
|
1857
|
+
this._observer = new IntersectionObserver((entries) => this._report(entries), {
|
|
1858
|
+
root: this._rootElement,
|
|
1859
|
+
rootMargin: this.rootMargin,
|
|
1860
|
+
// (out of 0..1 the browser throws: only the margin is left to refuse)
|
|
1861
|
+
threshold: Math.min(1, Math.max(0, this.threshold)),
|
|
1862
|
+
});
|
|
1863
|
+
}
|
|
1864
|
+
catch (error) {
|
|
1865
|
+
// A margin with no unit ("200", "0 0 900 0"): the browser's own exception came out of this callback,
|
|
1866
|
+
// uncaught, and no page was loaded (docs/mistake-catalogue.md, "root-margin-unit").
|
|
1867
|
+
const margin = this.rootMargin;
|
|
1868
|
+
if (this._warnedMargin !== margin) {
|
|
1869
|
+
this._warnedMargin = margin;
|
|
1870
|
+
console.warn(`[@wcstack/fetch] [wcs/root-margin-unit] <${config.tagNames.infiniteScroll} root-margin="${margin}"> is not a margin ` +
|
|
1871
|
+
`the browser takes (${error.message}): write each length with px or %, as "240px 0px". ` +
|
|
1872
|
+
`Nothing is observed, so no page is loaded.`);
|
|
1784
1873
|
}
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
rootMargin: this.rootMargin,
|
|
1788
|
-
threshold: this.threshold,
|
|
1789
|
-
});
|
|
1874
|
+
return;
|
|
1875
|
+
}
|
|
1790
1876
|
this._observer.observe(this);
|
|
1791
1877
|
}
|
|
1792
1878
|
_disconnectObserver() {
|
|
@@ -1794,13 +1880,63 @@ class InfiniteScroll extends HTMLElement {
|
|
|
1794
1880
|
this._observer.disconnect();
|
|
1795
1881
|
this._observer = null;
|
|
1796
1882
|
}
|
|
1883
|
+
this._inView = false;
|
|
1884
|
+
}
|
|
1885
|
+
/** The observer's report: the last entry is the sentinel's state now, and coming on screen runs the request. */
|
|
1886
|
+
_report(entries) {
|
|
1887
|
+
this._inView = entries[entries.length - 1].isIntersecting;
|
|
1888
|
+
if (this._inView) {
|
|
1889
|
+
this._run(true);
|
|
1890
|
+
}
|
|
1891
|
+
}
|
|
1892
|
+
/**
|
|
1893
|
+
* Takes the reports the browser has made and not delivered yet, before the sentinel's state is read outside a
|
|
1894
|
+
* report. While the user scrolls, the report that a page pushed the sentinel out arrives a frame later than
|
|
1895
|
+
* otherwise — after the frames the end of a request waits (Chromium, Firefox): the sentinel read as still on
|
|
1896
|
+
* screen, and a page the window did not need was asked for (docs/seam-apps.md, landing town-archive).
|
|
1897
|
+
*/
|
|
1898
|
+
_sync() {
|
|
1899
|
+
const pending = this._observer ? this._observer.takeRecords() : [];
|
|
1900
|
+
if (pending.length > 0) {
|
|
1901
|
+
this._report(pending);
|
|
1902
|
+
}
|
|
1797
1903
|
}
|
|
1904
|
+
/**
|
|
1905
|
+
* The scroll container: the element with that id, or — as `<wcs-intersect root>` spells it — the first one the
|
|
1906
|
+
* value matches as a selector. A root that is given and not found, or that does not scroll, is said once per
|
|
1907
|
+
* value: the viewport took its place without a word, and a root that does not clip its content counts
|
|
1908
|
+
* everything in it as on screen (`root="main"` loaded page after page).
|
|
1909
|
+
*/
|
|
1798
1910
|
_resolveRoot() {
|
|
1799
|
-
|
|
1911
|
+
const name = this.root;
|
|
1912
|
+
if (!name)
|
|
1800
1913
|
return null;
|
|
1801
|
-
|
|
1914
|
+
let found = document.getElementById(name);
|
|
1915
|
+
if (!found) {
|
|
1916
|
+
try {
|
|
1917
|
+
found = document.querySelector(name);
|
|
1918
|
+
}
|
|
1919
|
+
catch {
|
|
1920
|
+
// not a selector either
|
|
1921
|
+
}
|
|
1922
|
+
}
|
|
1923
|
+
if (this._warnedRoot !== name) {
|
|
1924
|
+
const tag = `<${config.tagNames.infiniteScroll} root="${name}">`;
|
|
1925
|
+
if (!found) {
|
|
1926
|
+
this._warnedRoot = name;
|
|
1927
|
+
console.warn(`[@wcstack/fetch] [wcs/infinite-scroll-root] ${tag}: no element has that id, and none matches it as a ` +
|
|
1928
|
+
`selector, so the viewport is the scroll root.`);
|
|
1929
|
+
}
|
|
1930
|
+
else if (doesNotScroll(found)) {
|
|
1931
|
+
this._warnedRoot = name;
|
|
1932
|
+
console.warn(`[@wcstack/fetch] [wcs/infinite-scroll-root] ${tag} is <${found.localName}>, which does not scroll ` +
|
|
1933
|
+
`(overflow: visible): everything inside it counts as on screen, so page after page is loaded. Name the ` +
|
|
1934
|
+
`element that scrolls, or leave root out for the viewport.`);
|
|
1935
|
+
}
|
|
1936
|
+
}
|
|
1937
|
+
return found;
|
|
1802
1938
|
}
|
|
1803
|
-
|
|
1939
|
+
_resolveTarget() {
|
|
1804
1940
|
const target = document.getElementById(this.target);
|
|
1805
1941
|
if (!(target instanceof Fetch)) {
|
|
1806
1942
|
// Nothing to load from: say so once per target, instead of loading no more pages without a word
|
|
@@ -1810,18 +1946,117 @@ class InfiniteScroll extends HTMLElement {
|
|
|
1810
1946
|
console.warn(`[@wcstack/fetch] [wcs/infinite-scroll-target] <${config.tagNames.infiniteScroll} target="${this.target}"> ` +
|
|
1811
1947
|
`came into view, but no <${config.tagNames.fetch}> has that id, so no page was loaded.`);
|
|
1812
1948
|
}
|
|
1813
|
-
|
|
1949
|
+
this._follow(null);
|
|
1950
|
+
return null;
|
|
1814
1951
|
}
|
|
1952
|
+
this._follow(target);
|
|
1953
|
+
return target;
|
|
1954
|
+
}
|
|
1955
|
+
/** Follows `target`'s url and requests (or stops following, with null). */
|
|
1956
|
+
_follow(target) {
|
|
1957
|
+
if (this._fetch === target)
|
|
1958
|
+
return;
|
|
1959
|
+
this._fetch?.removeEventListener("wcs-fetch:loading-changed", this._onLoadingChanged);
|
|
1960
|
+
this._fetch?.removeEventListener("wcs-fetch:response", this._onResponse);
|
|
1961
|
+
this._urlObserver?.disconnect();
|
|
1962
|
+
this._urlObserver = null;
|
|
1963
|
+
this._fetch = target;
|
|
1964
|
+
this._settling = false;
|
|
1965
|
+
this._changedInFlight = false;
|
|
1966
|
+
if (!target)
|
|
1967
|
+
return;
|
|
1968
|
+
target.addEventListener("wcs-fetch:loading-changed", this._onLoadingChanged);
|
|
1969
|
+
target.addEventListener("wcs-fetch:response", this._onResponse);
|
|
1970
|
+
this._urlObserver = new MutationObserver(() => this._urlChanged());
|
|
1971
|
+
this._urlObserver.observe(target, { attributes: true, attributeFilter: ["url"] });
|
|
1972
|
+
}
|
|
1973
|
+
/** Where the sentinel is in what scrolls: rows added before it move it, scrolling does not. */
|
|
1974
|
+
_position() {
|
|
1975
|
+
return this.getBoundingClientRect().top + (this._rootElement ? this._rootElement.scrollTop : window.scrollY);
|
|
1976
|
+
}
|
|
1977
|
+
/** A request started on the target (this element's, or the page's own). */
|
|
1978
|
+
_started() {
|
|
1979
|
+
this._asked = this._fetch.url;
|
|
1980
|
+
this._startedAt = this._position();
|
|
1981
|
+
this._changedInFlight = false;
|
|
1982
|
+
this._answered = false;
|
|
1983
|
+
this._settling = false;
|
|
1984
|
+
}
|
|
1985
|
+
/**
|
|
1986
|
+
* The request ended: once the page has had its frames to add rows and move the url on, the sentinel — if it is
|
|
1987
|
+
* still on screen — runs the next request when the url changed while this one was in flight, when this one had no
|
|
1988
|
+
* answer (aborted: a url written right after it is the page's own change, not its reaction to a response), or
|
|
1989
|
+
* when this one added rows. A response that added nothing ends it there.
|
|
1990
|
+
*/
|
|
1991
|
+
_ended() {
|
|
1992
|
+
this._settling = true;
|
|
1993
|
+
afterFrames(SETTLE_FRAMES, () => {
|
|
1994
|
+
if (!this._settling)
|
|
1995
|
+
return;
|
|
1996
|
+
this._settling = false;
|
|
1997
|
+
this._sync();
|
|
1998
|
+
if (this._inView && (this._changedInFlight || !this._answered || this._position() !== this._startedAt)) {
|
|
1999
|
+
this._run(false);
|
|
2000
|
+
}
|
|
2001
|
+
});
|
|
2002
|
+
}
|
|
2003
|
+
/**
|
|
2004
|
+
* The target's url was written. In flight, and while the page settles after a response, the end of that request
|
|
2005
|
+
* decides (_ended). At rest it is the page's own change — the first url, a filter: with the sentinel on screen
|
|
2006
|
+
* it is asked for, at once when nothing was asked yet, otherwise once the emptied list is laid out.
|
|
2007
|
+
*/
|
|
2008
|
+
_urlChanged() {
|
|
2009
|
+
const target = this._fetch;
|
|
1815
2010
|
if (target.loading) {
|
|
2011
|
+
this._changedInFlight = true;
|
|
1816
2012
|
return;
|
|
1817
2013
|
}
|
|
2014
|
+
if (this._settling || !this._inView)
|
|
2015
|
+
return;
|
|
2016
|
+
if (this._asked === null) {
|
|
2017
|
+
this._run(false);
|
|
2018
|
+
return;
|
|
2019
|
+
}
|
|
2020
|
+
afterFrames(2, () => {
|
|
2021
|
+
this._sync();
|
|
2022
|
+
if (this._inView)
|
|
2023
|
+
this._run(false);
|
|
2024
|
+
});
|
|
2025
|
+
}
|
|
2026
|
+
/**
|
|
2027
|
+
* Runs the target's request. `edge`: the sentinel came on screen (a user who scrolls away and back asks again,
|
|
2028
|
+
* also for the same url — the way a failed request is tried again); otherwise only a url not asked yet.
|
|
2029
|
+
*/
|
|
2030
|
+
_run(edge) {
|
|
2031
|
+
const target = this._resolveTarget();
|
|
2032
|
+
if (!target) {
|
|
2033
|
+
return;
|
|
2034
|
+
}
|
|
2035
|
+
// a request in flight, or no url yet: its end, or the url when it is written, comes back here
|
|
2036
|
+
if (target.loading || !target.url) {
|
|
2037
|
+
return;
|
|
2038
|
+
}
|
|
2039
|
+
if (!edge && target.url === this._asked) {
|
|
2040
|
+
return;
|
|
2041
|
+
}
|
|
2042
|
+
this._asked = target.url;
|
|
1818
2043
|
target.trigger = true;
|
|
1819
2044
|
if (this.once) {
|
|
1820
2045
|
this._done = true;
|
|
1821
2046
|
this._disconnectObserver();
|
|
2047
|
+
this._follow(null);
|
|
1822
2048
|
}
|
|
1823
2049
|
}
|
|
1824
2050
|
}
|
|
2051
|
+
/** Calls `fn` after `n` animation frames. */
|
|
2052
|
+
function afterFrames(n, fn) {
|
|
2053
|
+
requestAnimationFrame(() => (n > 1 ? afterFrames(n - 1, fn) : fn()));
|
|
2054
|
+
}
|
|
2055
|
+
/** Whether `element` clips nothing of what is in it: a scroll root must (`overflow` other than `visible`). */
|
|
2056
|
+
function doesNotScroll(element) {
|
|
2057
|
+
const style = getComputedStyle(element);
|
|
2058
|
+
return style.overflowX === "visible" && style.overflowY === "visible";
|
|
2059
|
+
}
|
|
1825
2060
|
|
|
1826
2061
|
// ===========================================================================
|
|
1827
2062
|
// AUTO-GENERATED FILE - DO NOT EDIT.
|