@wcstack/fetch 4.22.0 → 4.24.0
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 +81 -16
- package/README.md +81 -16
- package/dist/auto.min.js +1 -1
- package/dist/auto.min.js.map +1 -1
- package/dist/index.d.ts +104 -3
- package/dist/index.esm.js +278 -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
|
|
|
@@ -276,6 +288,9 @@ declare class Fetch extends HTMLElement {
|
|
|
276
288
|
private _lastFetchedUrl;
|
|
277
289
|
private _toldTriggerWithoutUrl;
|
|
278
290
|
private _toldUndefinedHelper;
|
|
291
|
+
private _sentBody;
|
|
292
|
+
private _toldBodyGone;
|
|
293
|
+
private _auto;
|
|
279
294
|
private _internals;
|
|
280
295
|
constructor();
|
|
281
296
|
get debugStates(): string[];
|
|
@@ -358,17 +373,73 @@ declare class Fetch extends HTMLElement {
|
|
|
358
373
|
*/
|
|
359
374
|
private _warnUndefinedHelper;
|
|
360
375
|
private _warnUnsafeAutoFetch;
|
|
361
|
-
|
|
376
|
+
/**
|
|
377
|
+
* `body` is a one-shot input: sent with one request, then gone. A request that changes data (POST, PUT,
|
|
378
|
+
* PATCH) going out with no body after one that carried a body is a second send whose body was not written
|
|
379
|
+
* again: a binding does not write a value equal to the one it wrote last, so the same JSON string on a
|
|
380
|
+
* retry never reaches the element, and the server got an empty request without a word
|
|
381
|
+
* (docs/seam-apps.md, ekiden-entry). Said once until a body is sent again. Not for the request a url
|
|
382
|
+
* change starts, which `[wcs/fetch-auto-unsafe]` reports.
|
|
383
|
+
*/
|
|
384
|
+
private _warnBodyGone;
|
|
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>;
|
|
362
397
|
attributeChangedCallback(name: string, _oldValue: string | null, _newValue: string | null): void;
|
|
363
398
|
connectedCallback(): void;
|
|
399
|
+
connectedMoveCallback(): void;
|
|
364
400
|
disconnectedCallback(): void;
|
|
365
401
|
}
|
|
366
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
|
+
*/
|
|
367
425
|
declare class InfiniteScroll extends HTMLElement {
|
|
368
426
|
static get observedAttributes(): string[];
|
|
369
427
|
private _observer;
|
|
370
428
|
private _done;
|
|
371
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;
|
|
372
443
|
get target(): string;
|
|
373
444
|
set target(value: string);
|
|
374
445
|
get root(): string | null;
|
|
@@ -386,8 +457,38 @@ declare class InfiniteScroll extends HTMLElement {
|
|
|
386
457
|
attributeChangedCallback(): void;
|
|
387
458
|
private _observe;
|
|
388
459
|
private _disconnectObserver;
|
|
460
|
+
/**
|
|
461
|
+
* The scroll container: the element with that id, or — as `<wcs-intersect root>` spells it — the first one the
|
|
462
|
+
* value matches as a selector. A root that is given and not found, or that does not scroll, is said once per
|
|
463
|
+
* value: the viewport took its place without a word, and a root that does not clip its content counts
|
|
464
|
+
* everything in it as on screen (`root="main"` loaded page after page).
|
|
465
|
+
*/
|
|
389
466
|
private _resolveRoot;
|
|
390
|
-
private
|
|
467
|
+
private _resolveTarget;
|
|
468
|
+
/** Follows `target`'s url and requests (or stops following, with null). */
|
|
469
|
+
private _follow;
|
|
470
|
+
/** Where the sentinel is in what scrolls: rows added before it move it, scrolling does not. */
|
|
471
|
+
private _position;
|
|
472
|
+
/** A request started on the target (this element's, or the page's own). */
|
|
473
|
+
private _started;
|
|
474
|
+
/**
|
|
475
|
+
* The request ended: once the page has had its frames to add rows and move the url on, the sentinel — if it is
|
|
476
|
+
* still on screen — runs the next request when the url changed while this one was in flight, when this one had no
|
|
477
|
+
* answer (aborted: a url written right after it is the page's own change, not its reaction to a response), or
|
|
478
|
+
* when this one added rows. A response that added nothing ends it there.
|
|
479
|
+
*/
|
|
480
|
+
private _ended;
|
|
481
|
+
/**
|
|
482
|
+
* The target's url was written. In flight, and while the page settles after a response, the end of that request
|
|
483
|
+
* decides (_ended). At rest it is the page's own change — the first url, a filter: with the sentinel on screen
|
|
484
|
+
* it is asked for, at once when nothing was asked yet, otherwise once the emptied list is laid out.
|
|
485
|
+
*/
|
|
486
|
+
private _urlChanged;
|
|
487
|
+
/**
|
|
488
|
+
* Runs the target's request. `edge`: the sentinel came on screen (a user who scrolls away and back asks again,
|
|
489
|
+
* also for the same url — the way a failed request is tried again); otherwise only a url not asked yet.
|
|
490
|
+
*/
|
|
491
|
+
private _run;
|
|
391
492
|
}
|
|
392
493
|
|
|
393
494
|
/**
|
|
@@ -446,4 +547,4 @@ declare global {
|
|
|
446
547
|
}
|
|
447
548
|
|
|
448
549
|
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 };
|
|
449
|
-
export type { FetchRequestOptions, IWcsTrustedTypesPolicy, IWritableConfig, IWritableTagNames, WcsFetchCoreValues, WcsFetchHttpError, WcsFetchValues, WcsIoErrorInfo, WcsIoErrorPhase };
|
|
550
|
+
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.0"};
|
|
72
72
|
|
|
73
73
|
// ===========================================================================
|
|
74
74
|
// AUTO-GENERATED FILE - DO NOT EDIT.
|
|
@@ -1169,6 +1169,11 @@ class Fetch extends HTMLElement {
|
|
|
1169
1169
|
_lastFetchedUrl = null;
|
|
1170
1170
|
_toldTriggerWithoutUrl = false;
|
|
1171
1171
|
_toldUndefinedHelper = false;
|
|
1172
|
+
// 本文を運んだ要求を出したことがある(body の入力か <wcs-fetch-body> から)
|
|
1173
|
+
_sentBody = false;
|
|
1174
|
+
_toldBodyGone = false;
|
|
1175
|
+
// 次に始まる要求は url が始めるもの(auto-fetch)で、コマンドや trigger のものではない
|
|
1176
|
+
_auto = false;
|
|
1172
1177
|
_internals = null;
|
|
1173
1178
|
constructor() {
|
|
1174
1179
|
super();
|
|
@@ -1449,6 +1454,7 @@ class Fetch extends HTMLElement {
|
|
|
1449
1454
|
// (not the request the element starts as it connects with a url: a POST that is meant)
|
|
1450
1455
|
if (resolveConnect === null)
|
|
1451
1456
|
this._warnUnsafeAutoFetch();
|
|
1457
|
+
this._auto = true;
|
|
1452
1458
|
// fetch() cannot reject here: FetchCore swallows network/HTTP errors and
|
|
1453
1459
|
// only rejects on an empty url, which the `url` guard above rules out.
|
|
1454
1460
|
this.fetch().finally(() => resolveConnect?.());
|
|
@@ -1505,21 +1511,66 @@ class Fetch extends HTMLElement {
|
|
|
1505
1511
|
`${method} with no body (its method stays ${method}; a body is sent once). Give loading and each change ` +
|
|
1506
1512
|
`its own <wcs-fetch>, or set \`manual\` on the one that changes data.`);
|
|
1507
1513
|
}
|
|
1508
|
-
|
|
1514
|
+
/**
|
|
1515
|
+
* `body` is a one-shot input: sent with one request, then gone. A request that changes data (POST, PUT,
|
|
1516
|
+
* PATCH) going out with no body after one that carried a body is a second send whose body was not written
|
|
1517
|
+
* again: a binding does not write a value equal to the one it wrote last, so the same JSON string on a
|
|
1518
|
+
* retry never reaches the element, and the server got an empty request without a word
|
|
1519
|
+
* (docs/seam-apps.md, ekiden-entry). Said once until a body is sent again. Not for the request a url
|
|
1520
|
+
* change starts, which `[wcs/fetch-auto-unsafe]` reports.
|
|
1521
|
+
*/
|
|
1522
|
+
_warnBodyGone() {
|
|
1523
|
+
const method = this.method;
|
|
1524
|
+
if (!this._sentBody || this._toldBodyGone || (method !== "POST" && method !== "PUT" && method !== "PATCH"))
|
|
1525
|
+
return;
|
|
1526
|
+
this._toldBodyGone = true;
|
|
1527
|
+
console.warn(`[@wcstack/fetch] [wcs/fetch-body-gone] This <${config.tagNames.fetch}> sent a ${method} with no body: a body is ` +
|
|
1528
|
+
"sent once, and none was written since the last request. A binding does not write a value equal to the one " +
|
|
1529
|
+
"it wrote last, so the same string on a retry never arrives: write a new object each time " +
|
|
1530
|
+
"(this.payload = { ...entry }), or null between two sends. (Said once until a body is sent again.)", this);
|
|
1531
|
+
}
|
|
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 : {});
|
|
1509
1547
|
// Record the url for the auto-fetch same-value guard. Every fetch (explicit
|
|
1510
1548
|
// included) updates it so a later auto-write of the same url is treated as a
|
|
1511
1549
|
// no-op rather than a duplicate request.
|
|
1512
|
-
this._lastFetchedUrl =
|
|
1550
|
+
this._lastFetchedUrl = requestUrl;
|
|
1513
1551
|
// ready again (a request starts): a trigger refused in the next outage is said again
|
|
1514
|
-
if (
|
|
1552
|
+
if (requestUrl)
|
|
1515
1553
|
this._toldTriggerWithoutUrl = false;
|
|
1516
|
-
const headers = this._collectHeaders();
|
|
1554
|
+
const headers = { ...this._collectHeaders(), ...(typeof options.headers === "object" ? options.headers : null) };
|
|
1517
1555
|
// Snapshot and reset `body` synchronously, before any await. The body is a
|
|
1518
1556
|
// one-shot input; resetting it after the await (when another caller may have
|
|
1519
1557
|
// already set a new body for the next request) would silently drop that value.
|
|
1520
|
-
|
|
1521
|
-
|
|
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
|
+
}
|
|
1522
1564
|
const { body, contentType } = this._collectBody(bodySnapshot);
|
|
1565
|
+
const auto = this._auto;
|
|
1566
|
+
this._auto = false;
|
|
1567
|
+
if (body !== null) {
|
|
1568
|
+
this._sentBody = true;
|
|
1569
|
+
this._toldBodyGone = false;
|
|
1570
|
+
}
|
|
1571
|
+
else if (!auto) {
|
|
1572
|
+
this._warnBodyGone();
|
|
1573
|
+
}
|
|
1523
1574
|
// FormData に手動で Content-Type を付けると、ブラウザが付与するはずの multipart
|
|
1524
1575
|
// boundary が失われてサーバー側でパースできなくなる。ヘッダはユーザー指定を
|
|
1525
1576
|
// 尊重して素通しするが、この典型的な誤設定は警告する。
|
|
@@ -1529,8 +1580,8 @@ class Fetch extends HTMLElement {
|
|
|
1529
1580
|
"This drops the multipart boundary the browser adds automatically; remove the " +
|
|
1530
1581
|
"Content-Type header (e.g. the <wcs-fetch-header>) to fix multipart uploads.");
|
|
1531
1582
|
}
|
|
1532
|
-
const result = await this._core.fetch(
|
|
1533
|
-
method: this.method,
|
|
1583
|
+
const result = await this._core.fetch(requestUrl, {
|
|
1584
|
+
method: typeof options.method === "string" && options.method ? options.method.toUpperCase() : this.method,
|
|
1534
1585
|
headers,
|
|
1535
1586
|
body,
|
|
1536
1587
|
contentType,
|
|
@@ -1588,6 +1639,10 @@ class Fetch extends HTMLElement {
|
|
|
1588
1639
|
}
|
|
1589
1640
|
this._scheduleAutoFetch();
|
|
1590
1641
|
}
|
|
1642
|
+
// moveBefore() で動かされたとき(並べ替えで動くリストの行): この要素のすることは置かれた場所に
|
|
1643
|
+
// よらないので、何も止めず、何も始め直さない。connectedMoveCallback が無いと、ブラウザは代わりに
|
|
1644
|
+
// disconnectedCallback と connectedCallback を呼ぶ(docs/timing-and-firing-contract.md §25)
|
|
1645
|
+
connectedMoveCallback() { }
|
|
1591
1646
|
disconnectedCallback() {
|
|
1592
1647
|
this.abort();
|
|
1593
1648
|
// Reset the same-value guard so a remount (reconnect with the same url)
|
|
@@ -1665,6 +1720,30 @@ class FetchBody extends HTMLElement {
|
|
|
1665
1720
|
}
|
|
1666
1721
|
}
|
|
1667
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
|
+
*/
|
|
1668
1747
|
class InfiniteScroll extends HTMLElement {
|
|
1669
1748
|
static get observedAttributes() {
|
|
1670
1749
|
return ["target", "root", "root-margin", "threshold", "disabled"];
|
|
@@ -1673,6 +1752,37 @@ class InfiniteScroll extends HTMLElement {
|
|
|
1673
1752
|
_done = false;
|
|
1674
1753
|
// the target last warned about (once per value)
|
|
1675
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
|
+
};
|
|
1676
1786
|
get target() {
|
|
1677
1787
|
return this.getAttribute("target") || "";
|
|
1678
1788
|
}
|
|
@@ -1730,6 +1840,7 @@ class InfiniteScroll extends HTMLElement {
|
|
|
1730
1840
|
}
|
|
1731
1841
|
disconnectedCallback() {
|
|
1732
1842
|
this._disconnectObserver();
|
|
1843
|
+
this._follow(null);
|
|
1733
1844
|
}
|
|
1734
1845
|
attributeChangedCallback() {
|
|
1735
1846
|
if (this.isConnected) {
|
|
@@ -1741,15 +1852,33 @@ class InfiniteScroll extends HTMLElement {
|
|
|
1741
1852
|
if (this._done || this.disabled || !this.target || typeof IntersectionObserver === "undefined") {
|
|
1742
1853
|
return;
|
|
1743
1854
|
}
|
|
1744
|
-
this.
|
|
1745
|
-
|
|
1746
|
-
|
|
1855
|
+
this._rootElement = this._resolveRoot();
|
|
1856
|
+
try {
|
|
1857
|
+
this._observer = new IntersectionObserver((entries) => {
|
|
1858
|
+
// the last entry is the sentinel's state now
|
|
1859
|
+
this._inView = entries[entries.length - 1].isIntersecting;
|
|
1860
|
+
if (this._inView) {
|
|
1861
|
+
this._run(true);
|
|
1862
|
+
}
|
|
1863
|
+
}, {
|
|
1864
|
+
root: this._rootElement,
|
|
1865
|
+
rootMargin: this.rootMargin,
|
|
1866
|
+
// (out of 0..1 the browser throws: only the margin is left to refuse)
|
|
1867
|
+
threshold: Math.min(1, Math.max(0, this.threshold)),
|
|
1868
|
+
});
|
|
1869
|
+
}
|
|
1870
|
+
catch (error) {
|
|
1871
|
+
// A margin with no unit ("200", "0 0 900 0"): the browser's own exception came out of this callback,
|
|
1872
|
+
// uncaught, and no page was loaded (docs/mistake-catalogue.md, "root-margin-unit").
|
|
1873
|
+
const margin = this.rootMargin;
|
|
1874
|
+
if (this._warnedMargin !== margin) {
|
|
1875
|
+
this._warnedMargin = margin;
|
|
1876
|
+
console.warn(`[@wcstack/fetch] [wcs/root-margin-unit] <${config.tagNames.infiniteScroll} root-margin="${margin}"> is not a margin ` +
|
|
1877
|
+
`the browser takes (${error.message}): write each length with px or %, as "240px 0px". ` +
|
|
1878
|
+
`Nothing is observed, so no page is loaded.`);
|
|
1747
1879
|
}
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
rootMargin: this.rootMargin,
|
|
1751
|
-
threshold: this.threshold,
|
|
1752
|
-
});
|
|
1880
|
+
return;
|
|
1881
|
+
}
|
|
1753
1882
|
this._observer.observe(this);
|
|
1754
1883
|
}
|
|
1755
1884
|
_disconnectObserver() {
|
|
@@ -1757,13 +1886,44 @@ class InfiniteScroll extends HTMLElement {
|
|
|
1757
1886
|
this._observer.disconnect();
|
|
1758
1887
|
this._observer = null;
|
|
1759
1888
|
}
|
|
1889
|
+
this._inView = false;
|
|
1760
1890
|
}
|
|
1891
|
+
/**
|
|
1892
|
+
* The scroll container: the element with that id, or — as `<wcs-intersect root>` spells it — the first one the
|
|
1893
|
+
* value matches as a selector. A root that is given and not found, or that does not scroll, is said once per
|
|
1894
|
+
* value: the viewport took its place without a word, and a root that does not clip its content counts
|
|
1895
|
+
* everything in it as on screen (`root="main"` loaded page after page).
|
|
1896
|
+
*/
|
|
1761
1897
|
_resolveRoot() {
|
|
1762
|
-
|
|
1898
|
+
const name = this.root;
|
|
1899
|
+
if (!name)
|
|
1763
1900
|
return null;
|
|
1764
|
-
|
|
1901
|
+
let found = document.getElementById(name);
|
|
1902
|
+
if (!found) {
|
|
1903
|
+
try {
|
|
1904
|
+
found = document.querySelector(name);
|
|
1905
|
+
}
|
|
1906
|
+
catch {
|
|
1907
|
+
// not a selector either
|
|
1908
|
+
}
|
|
1909
|
+
}
|
|
1910
|
+
if (this._warnedRoot !== name) {
|
|
1911
|
+
const tag = `<${config.tagNames.infiniteScroll} root="${name}">`;
|
|
1912
|
+
if (!found) {
|
|
1913
|
+
this._warnedRoot = name;
|
|
1914
|
+
console.warn(`[@wcstack/fetch] [wcs/infinite-scroll-root] ${tag}: no element has that id, and none matches it as a ` +
|
|
1915
|
+
`selector, so the viewport is the scroll root.`);
|
|
1916
|
+
}
|
|
1917
|
+
else if (doesNotScroll(found)) {
|
|
1918
|
+
this._warnedRoot = name;
|
|
1919
|
+
console.warn(`[@wcstack/fetch] [wcs/infinite-scroll-root] ${tag} is <${found.localName}>, which does not scroll ` +
|
|
1920
|
+
`(overflow: visible): everything inside it counts as on screen, so page after page is loaded. Name the ` +
|
|
1921
|
+
`element that scrolls, or leave root out for the viewport.`);
|
|
1922
|
+
}
|
|
1923
|
+
}
|
|
1924
|
+
return found;
|
|
1765
1925
|
}
|
|
1766
|
-
|
|
1926
|
+
_resolveTarget() {
|
|
1767
1927
|
const target = document.getElementById(this.target);
|
|
1768
1928
|
if (!(target instanceof Fetch)) {
|
|
1769
1929
|
// Nothing to load from: say so once per target, instead of loading no more pages without a word
|
|
@@ -1773,18 +1933,115 @@ class InfiniteScroll extends HTMLElement {
|
|
|
1773
1933
|
console.warn(`[@wcstack/fetch] [wcs/infinite-scroll-target] <${config.tagNames.infiniteScroll} target="${this.target}"> ` +
|
|
1774
1934
|
`came into view, but no <${config.tagNames.fetch}> has that id, so no page was loaded.`);
|
|
1775
1935
|
}
|
|
1776
|
-
|
|
1936
|
+
this._follow(null);
|
|
1937
|
+
return null;
|
|
1777
1938
|
}
|
|
1939
|
+
this._follow(target);
|
|
1940
|
+
return target;
|
|
1941
|
+
}
|
|
1942
|
+
/** Follows `target`'s url and requests (or stops following, with null). */
|
|
1943
|
+
_follow(target) {
|
|
1944
|
+
if (this._fetch === target)
|
|
1945
|
+
return;
|
|
1946
|
+
this._fetch?.removeEventListener("wcs-fetch:loading-changed", this._onLoadingChanged);
|
|
1947
|
+
this._fetch?.removeEventListener("wcs-fetch:response", this._onResponse);
|
|
1948
|
+
this._urlObserver?.disconnect();
|
|
1949
|
+
this._urlObserver = null;
|
|
1950
|
+
this._fetch = target;
|
|
1951
|
+
this._settling = false;
|
|
1952
|
+
this._changedInFlight = false;
|
|
1953
|
+
if (!target)
|
|
1954
|
+
return;
|
|
1955
|
+
target.addEventListener("wcs-fetch:loading-changed", this._onLoadingChanged);
|
|
1956
|
+
target.addEventListener("wcs-fetch:response", this._onResponse);
|
|
1957
|
+
this._urlObserver = new MutationObserver(() => this._urlChanged());
|
|
1958
|
+
this._urlObserver.observe(target, { attributes: true, attributeFilter: ["url"] });
|
|
1959
|
+
}
|
|
1960
|
+
/** Where the sentinel is in what scrolls: rows added before it move it, scrolling does not. */
|
|
1961
|
+
_position() {
|
|
1962
|
+
return this.getBoundingClientRect().top + (this._rootElement ? this._rootElement.scrollTop : window.scrollY);
|
|
1963
|
+
}
|
|
1964
|
+
/** A request started on the target (this element's, or the page's own). */
|
|
1965
|
+
_started() {
|
|
1966
|
+
this._asked = this._fetch.url;
|
|
1967
|
+
this._startedAt = this._position();
|
|
1968
|
+
this._changedInFlight = false;
|
|
1969
|
+
this._answered = false;
|
|
1970
|
+
this._settling = false;
|
|
1971
|
+
}
|
|
1972
|
+
/**
|
|
1973
|
+
* The request ended: once the page has had its frames to add rows and move the url on, the sentinel — if it is
|
|
1974
|
+
* still on screen — runs the next request when the url changed while this one was in flight, when this one had no
|
|
1975
|
+
* answer (aborted: a url written right after it is the page's own change, not its reaction to a response), or
|
|
1976
|
+
* when this one added rows. A response that added nothing ends it there.
|
|
1977
|
+
*/
|
|
1978
|
+
_ended() {
|
|
1979
|
+
this._settling = true;
|
|
1980
|
+
afterFrames(SETTLE_FRAMES, () => {
|
|
1981
|
+
if (!this._settling)
|
|
1982
|
+
return;
|
|
1983
|
+
this._settling = false;
|
|
1984
|
+
if (this._inView && (this._changedInFlight || !this._answered || this._position() !== this._startedAt)) {
|
|
1985
|
+
this._run(false);
|
|
1986
|
+
}
|
|
1987
|
+
});
|
|
1988
|
+
}
|
|
1989
|
+
/**
|
|
1990
|
+
* The target's url was written. In flight, and while the page settles after a response, the end of that request
|
|
1991
|
+
* decides (_ended). At rest it is the page's own change — the first url, a filter: with the sentinel on screen
|
|
1992
|
+
* it is asked for, at once when nothing was asked yet, otherwise once the emptied list is laid out.
|
|
1993
|
+
*/
|
|
1994
|
+
_urlChanged() {
|
|
1995
|
+
const target = this._fetch;
|
|
1778
1996
|
if (target.loading) {
|
|
1997
|
+
this._changedInFlight = true;
|
|
1998
|
+
return;
|
|
1999
|
+
}
|
|
2000
|
+
if (this._settling || !this._inView)
|
|
2001
|
+
return;
|
|
2002
|
+
if (this._asked === null) {
|
|
2003
|
+
this._run(false);
|
|
2004
|
+
return;
|
|
2005
|
+
}
|
|
2006
|
+
afterFrames(2, () => {
|
|
2007
|
+
if (this._inView)
|
|
2008
|
+
this._run(false);
|
|
2009
|
+
});
|
|
2010
|
+
}
|
|
2011
|
+
/**
|
|
2012
|
+
* Runs the target's request. `edge`: the sentinel came on screen (a user who scrolls away and back asks again,
|
|
2013
|
+
* also for the same url — the way a failed request is tried again); otherwise only a url not asked yet.
|
|
2014
|
+
*/
|
|
2015
|
+
_run(edge) {
|
|
2016
|
+
const target = this._resolveTarget();
|
|
2017
|
+
if (!target) {
|
|
2018
|
+
return;
|
|
2019
|
+
}
|
|
2020
|
+
// a request in flight, or no url yet: its end, or the url when it is written, comes back here
|
|
2021
|
+
if (target.loading || !target.url) {
|
|
1779
2022
|
return;
|
|
1780
2023
|
}
|
|
2024
|
+
if (!edge && target.url === this._asked) {
|
|
2025
|
+
return;
|
|
2026
|
+
}
|
|
2027
|
+
this._asked = target.url;
|
|
1781
2028
|
target.trigger = true;
|
|
1782
2029
|
if (this.once) {
|
|
1783
2030
|
this._done = true;
|
|
1784
2031
|
this._disconnectObserver();
|
|
2032
|
+
this._follow(null);
|
|
1785
2033
|
}
|
|
1786
2034
|
}
|
|
1787
2035
|
}
|
|
2036
|
+
/** Calls `fn` after `n` animation frames. */
|
|
2037
|
+
function afterFrames(n, fn) {
|
|
2038
|
+
requestAnimationFrame(() => (n > 1 ? afterFrames(n - 1, fn) : fn()));
|
|
2039
|
+
}
|
|
2040
|
+
/** Whether `element` clips nothing of what is in it: a scroll root must (`overflow` other than `visible`). */
|
|
2041
|
+
function doesNotScroll(element) {
|
|
2042
|
+
const style = getComputedStyle(element);
|
|
2043
|
+
return style.overflowX === "visible" && style.overflowY === "visible";
|
|
2044
|
+
}
|
|
1788
2045
|
|
|
1789
2046
|
// ===========================================================================
|
|
1790
2047
|
// AUTO-GENERATED FILE - DO NOT EDIT.
|