@wcstack/fetch 1.11.1 → 1.12.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 CHANGED
@@ -60,15 +60,15 @@ npm install @wcstack/fetch
60
60
  },
61
61
  };
62
62
  </script>
63
+ </wcs-state>
63
64
 
64
- <wcs-fetch data-wcs="url: usersUrl; value: users"></wcs-fetch>
65
+ <wcs-fetch data-wcs="url: usersUrl; value: users"></wcs-fetch>
65
66
 
66
- <ul>
67
- <template data-wcs="for: users">
68
- <li data-wcs="textContent: users.*.name"></li>
69
- </template>
70
- </ul>
71
- </wcs-state>
67
+ <ul>
68
+ <template data-wcs="for: users">
69
+ <li data-wcs="textContent: users.*.name"></li>
70
+ </template>
71
+ </ul>
72
72
  ```
73
73
 
74
74
  これがデフォルトモードです:
@@ -94,30 +94,30 @@ npm install @wcstack/fetch
94
94
  },
95
95
  };
96
96
  </script>
97
+ </wcs-state>
97
98
 
98
- <select data-wcs="value: filterRole">
99
- <option value="">すべて</option>
100
- <option value="admin">Admin</option>
101
- <option value="staff">Staff</option>
102
- </select>
99
+ <select data-wcs="value: filterRole">
100
+ <option value="">すべて</option>
101
+ <option value="admin">Admin</option>
102
+ <option value="staff">Staff</option>
103
+ </select>
103
104
 
104
- <wcs-fetch
105
- data-wcs="url: usersUrl; value: users; loading: listLoading; error: listError">
106
- </wcs-fetch>
105
+ <wcs-fetch
106
+ data-wcs="url: usersUrl; value: users; loading: listLoading; error: listError">
107
+ </wcs-fetch>
107
108
 
108
- <template data-wcs="if: listLoading">
109
- <p>読み込み中...</p>
110
- </template>
111
- <template data-wcs="if: listError">
112
- <p>ユーザーの読み込みに失敗しました。</p>
113
- </template>
109
+ <template data-wcs="if: listLoading">
110
+ <p>読み込み中...</p>
111
+ </template>
112
+ <template data-wcs="if: listError">
113
+ <p>ユーザーの読み込みに失敗しました。</p>
114
+ </template>
114
115
 
115
- <ul>
116
- <template data-wcs="for: users">
117
- <li data-wcs="textContent: users.*.name"></li>
118
- </template>
119
- </ul>
120
- </wcs-state>
116
+ <ul>
117
+ <template data-wcs="for: users">
118
+ <li data-wcs="textContent: users.*.name"></li>
119
+ </template>
120
+ </ul>
121
121
  ```
122
122
 
123
123
  ### 3. `trigger` による手動実行
@@ -136,15 +136,15 @@ npm install @wcstack/fetch
136
136
  },
137
137
  };
138
138
  </script>
139
+ </wcs-state>
139
140
 
140
- <wcs-fetch
141
- url="/api/users"
142
- manual
143
- data-wcs="trigger: shouldRefresh; value: users; loading: listLoading">
144
- </wcs-fetch>
141
+ <wcs-fetch
142
+ url="/api/users"
143
+ manual
144
+ data-wcs="trigger: shouldRefresh; value: users; loading: listLoading">
145
+ </wcs-fetch>
145
146
 
146
- <button data-wcs="onclick: reload">更新</button>
147
- </wcs-state>
147
+ <button data-wcs="onclick: reload">更新</button>
148
148
  ```
149
149
 
150
150
  `trigger` は **単方向のコマンドサーフェス** です:
@@ -181,35 +181,86 @@ npm install @wcstack/fetch
181
181
  },
182
182
  };
183
183
  </script>
184
+ </wcs-state>
184
185
 
185
- <input data-wcs="value: newUser.name" placeholder="名前">
186
- <input data-wcs="value: newUser.email" placeholder="メール">
187
-
188
- <button data-wcs="onclick: submit">作成</button>
189
-
190
- <wcs-fetch
191
- url="/api/users"
192
- method="POST"
193
- manual
194
- data-wcs="
195
- body: newUser;
196
- trigger: submitRequest;
197
- value: submitResult;
198
- error: submitError;
199
- loading: submitLoading
200
- ">
201
- <wcs-fetch-header name="Content-Type" value="application/json"></wcs-fetch-header>
202
- </wcs-fetch>
203
-
204
- <template data-wcs="if: submitLoading">
205
- <p>送信中...</p>
206
- </template>
207
- <template data-wcs="if: submitError">
208
- <p>送信に失敗しました。</p>
209
- </template>
186
+ <input data-wcs="value: newUser.name" placeholder="名前">
187
+ <input data-wcs="value: newUser.email" placeholder="メール">
188
+
189
+ <button data-wcs="onclick: submit">作成</button>
190
+
191
+ <wcs-fetch
192
+ url="/api/users"
193
+ method="POST"
194
+ manual
195
+ data-wcs="
196
+ body: newUser;
197
+ trigger: submitRequest;
198
+ value: submitResult;
199
+ error: submitError;
200
+ loading: submitLoading
201
+ ">
202
+ <wcs-fetch-header name="Content-Type" value="application/json"></wcs-fetch-header>
203
+ </wcs-fetch>
204
+
205
+ <template data-wcs="if: submitLoading">
206
+ <p>送信中...</p>
207
+ </template>
208
+ <template data-wcs="if: submitError">
209
+ <p>送信に失敗しました。</p>
210
+ </template>
211
+ ```
212
+
213
+ ### 5. `<wcs-infinite-scroll>` による無限スクロール
214
+
215
+ `<wcs-infinite-scroll>` は、sentinel 要素が表示領域に入ったときに既存の `<wcs-fetch>` を実行します。
216
+ ページ番号、次の URL、レスポンスの append などは `@wcstack/state` 側で宣言し、スクロール検知だけをこのタグに任せます。
217
+
218
+ 動作規約:
219
+
220
+ - target の `<wcs-fetch>` が `loading` 中なら重複起動しません
221
+ - `once` は厳密な一回限りです。一度発火した後は属性を変更しても再監視しません
222
+ - `target` の id が未解決、または対象が `<wcs-fetch>` でない場合は無診断で no-op です
223
+
224
+ ```html
225
+ <wcs-state>
226
+ <script type="module">
227
+ export default {
228
+ page: 1,
229
+ users: [],
230
+ get nextUsersUrl() {
231
+ return "/api/users?page=" + this.page;
232
+ },
233
+ };
234
+ </script>
210
235
  </wcs-state>
236
+
237
+ <wcs-fetch
238
+ id="next-page-fetch"
239
+ manual
240
+ data-wcs="url: nextUsersUrl; loading: listLoading; error: listError">
241
+ </wcs-fetch>
242
+
243
+ <ul>
244
+ <template data-wcs="for: users">
245
+ <li data-wcs="textContent: users.*.name"></li>
246
+ </template>
247
+ </ul>
248
+
249
+ <wcs-infinite-scroll
250
+ target="next-page-fetch"
251
+ root-margin="240px 0px">
252
+ </wcs-infinite-scroll>
211
253
  ```
212
254
 
255
+ 主な属性:
256
+
257
+ - `target`: 実行する `<wcs-fetch>` の `id`
258
+ - `root`: スクロールコンテナの `id`。未指定時は viewport
259
+ - `root-margin`: 先読み距離。`IntersectionObserver` の `rootMargin`
260
+ - `threshold`: 交差しきい値。未指定時は `0`
261
+ - `disabled`: 監視を停止
262
+ - `once`: 最初の実行後に監視を解除し、その後は再武装しない
263
+
213
264
  ## ステートサーフェス vs コマンドサーフェス
214
265
 
215
266
  `<wcs-fetch>` は 2 種類のプロパティを公開します。
@@ -304,6 +355,14 @@ URL 変更時にリクエストが進行中の場合、前のリクエストは
304
355
 
305
356
  `manual` 属性を設定すると自動実行が無効になり、`fetch()` メソッドや `trigger` プロパティで明示的に制御できます。
306
357
 
358
+ > **Note (v1.13 以降):** 自動 fetch は同期実行ではなく microtask に遅延されます。
359
+ > 同一 tick 内の複数の入力書き込み(例: `...:` spread が `url` と `manual` を順に
360
+ > 書く場合)は最終的な要素状態に対する 1 回の判定に集約され、変化のない `url` の
361
+ > 再書き込みでは再 fetch しません。接続時の fetch を待つには
362
+ > `connectedCallbackPromise` を使ってください — `appendChild` 直後に `promise` を
363
+ > 同期的に読むと、自動 fetch ではなく初期の解決済み Promise を掴みます。明示的な
364
+ > トリガ(`fetch()`、`trigger`、`fetch` コマンド)は影響を受けず、即時実行のままです。
365
+
307
366
  ## プログラムからの利用
308
367
 
309
368
  ```javascript
@@ -419,9 +478,17 @@ wcstack アプリケーションでは、**`trigger` によるステート駆動
419
478
 
420
479
  `FetchCore` と `<wcs-fetch>` はどちらも wc-bindable-protocol に準拠しており、プロトコル対応の任意のフレームワークやコンポーネントと相互運用できます。
421
480
 
481
+ 宣言は wc-bindable インターフェースモデルの全体に従い、3 つの独立したサーフェスを持ちます:
482
+
483
+ - **`properties`** — `bind()` がサブスクライブする観測可能な出力(`value`, `loading`, `error`, `status`、および Shell の `trigger`)
484
+ - **`inputs`** — 設定可能なサーフェス(`url`, `method`, …)。ツールやコード生成、リモートプロキシが読み取る宣言的メタデータ
485
+ - **`commands`** — 呼び出し可能なメソッド(`fetch`, `abort`)。`@wcstack/state` のようなバインディングシステムが名前で呼び出せる
486
+
487
+ プロトコルに従い、コアの `bind()` が解釈するのは `properties` のみです。`inputs` / `commands`(および `attribute` / `async` ヒント)は記述的なものであり、暗黙の双方向データフローを生成することは**ありません**。
488
+
422
489
  ### Core (`FetchCore`)
423
490
 
424
- `FetchCore` は任意のランタイムからサブスクライブできるバインド可能な非同期状態を宣言します:
491
+ `FetchCore` は任意のランタイムからサブスクライブできるバインド可能な非同期状態と、ポータブルな input/command サーフェスを宣言します:
425
492
 
426
493
  ```typescript
427
494
  static wcBindable = {
@@ -435,6 +502,14 @@ static wcBindable = {
435
502
  { name: "status", event: "wcs-fetch:response",
436
503
  getter: (e) => e.detail.status },
437
504
  ],
505
+ inputs: [
506
+ { name: "url" },
507
+ { name: "method" },
508
+ ],
509
+ commands: [
510
+ { name: "fetch", async: true },
511
+ { name: "abort" },
512
+ ],
438
513
  };
439
514
  ```
440
515
 
@@ -442,7 +517,7 @@ static wcBindable = {
442
517
 
443
518
  ### Shell (`<wcs-fetch>`)
444
519
 
445
- Shell は Core の宣言を拡張し、バインディングシステムから宣言的に fetch を実行できるようにします:
520
+ Shell は Core の宣言を `trigger` 出力と DOM 駆動の input サーフェスで拡張します。`commands`(`fetch` / `abort`)はそのまま継承されます:
446
521
 
447
522
  ```typescript
448
523
  static wcBindable = {
@@ -451,9 +526,19 @@ static wcBindable = {
451
526
  ...FetchCore.wcBindable.properties,
452
527
  { name: "trigger", event: "wcs-fetch:trigger-changed" },
453
528
  ],
529
+ inputs: [
530
+ { name: "url" },
531
+ { name: "method" },
532
+ { name: "target" },
533
+ { name: "manual" },
534
+ { name: "body" },
535
+ { name: "trigger" },
536
+ ],
454
537
  };
455
538
  ```
456
539
 
540
+ Shell の inputs は意図的に `attribute` ヒントを持ちません。各セッター(`url`, `method`, `target`, `manual`)はすでに対応する属性へ反映されるため、`inputs[].attribute` をミラーするバインディングシステムでは属性が二重に設定されてしまうからです。
541
+
457
542
  ## TypeScript 型
458
543
 
459
544
  ```typescript
package/README.md CHANGED
@@ -60,15 +60,15 @@ If another request is already in flight, it aborts the previous one.
60
60
  },
61
61
  };
62
62
  </script>
63
+ </wcs-state>
63
64
 
64
- <wcs-fetch data-wcs="url: usersUrl; value: users"></wcs-fetch>
65
+ <wcs-fetch data-wcs="url: usersUrl; value: users"></wcs-fetch>
65
66
 
66
- <ul>
67
- <template data-wcs="for: users">
68
- <li data-wcs="textContent: users.*.name"></li>
69
- </template>
70
- </ul>
71
- </wcs-state>
67
+ <ul>
68
+ <template data-wcs="for: users">
69
+ <li data-wcs="textContent: users.*.name"></li>
70
+ </template>
71
+ </ul>
72
72
  ```
73
73
 
74
74
  This is the default mode:
@@ -94,30 +94,30 @@ A computed URL can drive data fetching automatically:
94
94
  },
95
95
  };
96
96
  </script>
97
+ </wcs-state>
97
98
 
98
- <select data-wcs="value: filterRole">
99
- <option value="">All</option>
100
- <option value="admin">Admin</option>
101
- <option value="staff">Staff</option>
102
- </select>
99
+ <select data-wcs="value: filterRole">
100
+ <option value="">All</option>
101
+ <option value="admin">Admin</option>
102
+ <option value="staff">Staff</option>
103
+ </select>
103
104
 
104
- <wcs-fetch
105
- data-wcs="url: usersUrl; value: users; loading: listLoading; error: listError">
106
- </wcs-fetch>
105
+ <wcs-fetch
106
+ data-wcs="url: usersUrl; value: users; loading: listLoading; error: listError">
107
+ </wcs-fetch>
107
108
 
108
- <template data-wcs="if: listLoading">
109
- <p>Loading...</p>
110
- </template>
111
- <template data-wcs="if: listError">
112
- <p>Failed to load users.</p>
113
- </template>
109
+ <template data-wcs="if: listLoading">
110
+ <p>Loading...</p>
111
+ </template>
112
+ <template data-wcs="if: listError">
113
+ <p>Failed to load users.</p>
114
+ </template>
114
115
 
115
- <ul>
116
- <template data-wcs="for: users">
117
- <li data-wcs="textContent: users.*.name"></li>
118
- </template>
119
- </ul>
120
- </wcs-state>
116
+ <ul>
117
+ <template data-wcs="for: users">
118
+ <li data-wcs="textContent: users.*.name"></li>
119
+ </template>
120
+ </ul>
121
121
  ```
122
122
 
123
123
  ### 3. Manual execution with `trigger`
@@ -136,15 +136,15 @@ Use `manual` when you want to prepare inputs first and execute later.
136
136
  },
137
137
  };
138
138
  </script>
139
+ </wcs-state>
139
140
 
140
- <wcs-fetch
141
- url="/api/users"
142
- manual
143
- data-wcs="trigger: shouldRefresh; value: users; loading: listLoading">
144
- </wcs-fetch>
141
+ <wcs-fetch
142
+ url="/api/users"
143
+ manual
144
+ data-wcs="trigger: shouldRefresh; value: users; loading: listLoading">
145
+ </wcs-fetch>
145
146
 
146
- <button data-wcs="onclick: reload">Refresh</button>
147
- </wcs-state>
147
+ <button data-wcs="onclick: reload">Refresh</button>
148
148
  ```
149
149
 
150
150
  `trigger` is a **one-way command surface**:
@@ -181,35 +181,86 @@ and no event fires. Set the `url` first, then write `true` again to execute.
181
181
  },
182
182
  };
183
183
  </script>
184
+ </wcs-state>
184
185
 
185
- <input data-wcs="value: newUser.name" placeholder="Name">
186
- <input data-wcs="value: newUser.email" placeholder="Email">
187
-
188
- <button data-wcs="onclick: submit">Create</button>
189
-
190
- <wcs-fetch
191
- url="/api/users"
192
- method="POST"
193
- manual
194
- data-wcs="
195
- body: newUser;
196
- trigger: submitRequest;
197
- value: submitResult;
198
- error: submitError;
199
- loading: submitLoading
200
- ">
201
- <wcs-fetch-header name="Content-Type" value="application/json"></wcs-fetch-header>
202
- </wcs-fetch>
203
-
204
- <template data-wcs="if: submitLoading">
205
- <p>Submitting...</p>
206
- </template>
207
- <template data-wcs="if: submitError">
208
- <p>Submit failed.</p>
209
- </template>
186
+ <input data-wcs="value: newUser.name" placeholder="Name">
187
+ <input data-wcs="value: newUser.email" placeholder="Email">
188
+
189
+ <button data-wcs="onclick: submit">Create</button>
190
+
191
+ <wcs-fetch
192
+ url="/api/users"
193
+ method="POST"
194
+ manual
195
+ data-wcs="
196
+ body: newUser;
197
+ trigger: submitRequest;
198
+ value: submitResult;
199
+ error: submitError;
200
+ loading: submitLoading
201
+ ">
202
+ <wcs-fetch-header name="Content-Type" value="application/json"></wcs-fetch-header>
203
+ </wcs-fetch>
204
+
205
+ <template data-wcs="if: submitLoading">
206
+ <p>Submitting...</p>
207
+ </template>
208
+ <template data-wcs="if: submitError">
209
+ <p>Submit failed.</p>
210
+ </template>
211
+ ```
212
+
213
+ ### 5. Infinite scroll with `<wcs-infinite-scroll>`
214
+
215
+ `<wcs-infinite-scroll>` runs an existing `<wcs-fetch>` when its sentinel element enters the viewport.
216
+ Keep page numbers, next URLs, and response append behavior in `@wcstack/state`; this tag only owns scroll detection.
217
+
218
+ Behavior rules:
219
+
220
+ - it does not trigger again while the target `<wcs-fetch>` is `loading`
221
+ - `once` is strict: after the first execution, changing attributes does not re-arm observation
222
+ - if `target` does not resolve, or resolves to a non-`<wcs-fetch>` element, it is a silent no-op
223
+
224
+ ```html
225
+ <wcs-state>
226
+ <script type="module">
227
+ export default {
228
+ page: 1,
229
+ users: [],
230
+ get nextUsersUrl() {
231
+ return "/api/users?page=" + this.page;
232
+ },
233
+ };
234
+ </script>
210
235
  </wcs-state>
236
+
237
+ <wcs-fetch
238
+ id="next-page-fetch"
239
+ manual
240
+ data-wcs="url: nextUsersUrl; loading: listLoading; error: listError">
241
+ </wcs-fetch>
242
+
243
+ <ul>
244
+ <template data-wcs="for: users">
245
+ <li data-wcs="textContent: users.*.name"></li>
246
+ </template>
247
+ </ul>
248
+
249
+ <wcs-infinite-scroll
250
+ target="next-page-fetch"
251
+ root-margin="240px 0px">
252
+ </wcs-infinite-scroll>
211
253
  ```
212
254
 
255
+ Attributes:
256
+
257
+ - `target`: `id` of the `<wcs-fetch>` to run
258
+ - `root`: `id` of the scroll container. Defaults to the viewport
259
+ - `root-margin`: preload distance. Passed to `IntersectionObserver.rootMargin`
260
+ - `threshold`: intersection threshold. Defaults to `0`
261
+ - `disabled`: stops observing
262
+ - `once`: disconnects after the first execution and does not re-arm afterwards
263
+
213
264
  ## State Surface vs Command Surface
214
265
 
215
266
  `<wcs-fetch>` exposes two different kinds of properties.
@@ -305,6 +356,15 @@ If a request is already in flight when the URL changes, the previous request is
305
356
 
306
357
  Set the `manual` attribute to disable auto-fetch and control execution explicitly via `fetch()` or `trigger`.
307
358
 
359
+ > **Note (since v1.13):** Auto-fetch is deferred to a microtask instead of firing
360
+ > synchronously. Multiple input writes in the same tick (e.g. a `...:` spread
361
+ > writing `url` and `manual` in sequence) collapse into a single decision made
362
+ > against the final element state, and rewriting an unchanged `url` does not
363
+ > refetch. To await the connect-time fetch, use `connectedCallbackPromise` —
364
+ > reading `promise` synchronously right after `appendChild` returns the initial
365
+ > resolved promise, not the auto-fetch. Explicit triggers (`fetch()`, `trigger`,
366
+ > the `fetch` command) are unaffected and still run immediately.
367
+
308
368
  ## Programmatic Usage
309
369
 
310
370
  ```javascript
package/dist/index.d.ts CHANGED
@@ -2,11 +2,13 @@ interface ITagNames {
2
2
  readonly fetch: string;
3
3
  readonly fetchHeader: string;
4
4
  readonly fetchBody: string;
5
+ readonly infiniteScroll: string;
5
6
  }
6
7
  interface IWritableTagNames {
7
8
  fetch?: string;
8
9
  fetchHeader?: string;
9
10
  fetchBody?: string;
11
+ infiniteScroll?: string;
10
12
  }
11
13
  interface IConfig {
12
14
  readonly autoTrigger: boolean;
@@ -122,11 +124,14 @@ declare class Fetch extends HTMLElement {
122
124
  private _body;
123
125
  private _trigger;
124
126
  private _connectedCallbackPromise;
127
+ private _autoPending;
128
+ private _connectResolve;
129
+ private _lastFetchedUrl;
125
130
  constructor();
126
131
  get url(): string;
127
- set url(value: string);
132
+ set url(value: string | null);
128
133
  get method(): string;
129
- set method(value: string);
134
+ set method(value: string | null);
130
135
  get target(): string | null;
131
136
  set target(value: string | null);
132
137
  get value(): any;
@@ -144,11 +149,55 @@ declare class Fetch extends HTMLElement {
144
149
  private _collectHeaders;
145
150
  private _collectBody;
146
151
  abort(): void;
152
+ /**
153
+ * Coalesce auto-fetch requests in the current task into a single microtask.
154
+ *
155
+ * Multiple synchronous input writes in the same tick — e.g. a `...` spread
156
+ * writing `url` before `manual` — collapse into one decision made against the
157
+ * FINAL element state, so the spread application order can no longer trigger a
158
+ * stray fetch. The microtask re-reads `isConnected` / `manual` / `url` at fire
159
+ * time; whatever was written last wins.
160
+ *
161
+ * Only the implicit auto-fetch (url attribute change, connect-time) is routed
162
+ * here. Explicit triggers — the `trigger` setter, the `fetch` command, and
163
+ * autoTrigger (data-fetchtarget clicks) — must fire immediately and stay on
164
+ * their own synchronous paths.
165
+ *
166
+ * The connect-time promise (connectedCallbackPromise) is resolved here in
167
+ * EVERY exit path, including the no-fetch branch, so awaiting it never hangs
168
+ * when the final state turns out to be manual / url-less / disconnected.
169
+ */
170
+ private _scheduleAutoFetch;
147
171
  fetch(): Promise<any>;
148
- attributeChangedCallback(name: string, _oldValue: string | null, newValue: string | null): void;
172
+ attributeChangedCallback(name: string, _oldValue: string | null, _newValue: string | null): void;
149
173
  connectedCallback(): void;
150
174
  disconnectedCallback(): void;
151
175
  }
152
176
 
153
- export { FetchCore, Fetch as WcsFetch, bootstrapFetch, getConfig };
177
+ declare class InfiniteScroll extends HTMLElement {
178
+ static get observedAttributes(): string[];
179
+ private _observer;
180
+ private _done;
181
+ get target(): string;
182
+ set target(value: string);
183
+ get root(): string | null;
184
+ set root(value: string | null);
185
+ get rootMargin(): string;
186
+ set rootMargin(value: string);
187
+ get threshold(): number;
188
+ set threshold(value: number);
189
+ get disabled(): boolean;
190
+ set disabled(value: boolean);
191
+ get once(): boolean;
192
+ set once(value: boolean);
193
+ connectedCallback(): void;
194
+ disconnectedCallback(): void;
195
+ attributeChangedCallback(): void;
196
+ private _observe;
197
+ private _disconnectObserver;
198
+ private _resolveRoot;
199
+ private _triggerFetch;
200
+ }
201
+
202
+ export { FetchCore, Fetch as WcsFetch, InfiniteScroll as WcsInfiniteScroll, bootstrapFetch, getConfig };
154
203
  export type { FetchRequestOptions, IWritableConfig, IWritableTagNames, WcsFetchCoreValues, WcsFetchHttpError, WcsFetchValues };