PayloadFetchStoreclass
new PayloadFetchStore<P, R>(payload: P | typeof NONE, value: R | typeof NONE, callback?: PayloadFetchCallback<P, R>, debounce = 0)
| Param | Type | |
|---|---|---|
payload | Ptypeof NONE | The initial payload for the store. required |
value | Rtypeof NONE | The initial value for the store, or NONE if it does not have one yet. required |
callback | PayloadFetchCallback<P, R> | An optional callback that, if set, will be called with the current payload when the fetch() method is invoked to fetch the next value. |
debounce | unknown | Delay in milliseconds before the fetch is triggered after a payload change. busy becomes true immediately; the actual fetch waits for the debounce period to expire. If the payload changes again before the delay expires the previous fetch is cancelled and the timer resets. Defaults to 0 |
| Return | |
|---|---|
PayloadFetchStore<P, R> | Store that fetches its values from a remote source by sending it a payload. |
| Property | Type | |
|---|---|---|
.payload | Store<P> | Store keeping the current payload to send to the fetch on send. - New payloads can be set using this.payload.value required readonly |
Store that fetches its values from a remote source by sending it a payload.
- Holds the current payload in a nested
this.payloadstore; settingthis.payload.valuetriggers a fresh fetch. - Optionally debounces fetches so rapid payload changes only result in a single request.
A FetchStore whose fetch is driven by a payload. PayloadFetchStore<P, R> holds an inner .payload store; whenever the payload changes, any in-flight fetch is cancelled and a fresh one starts with the new payload.
This is the base class for EndpointStore in the shelving/api module — the payload is the endpoint's request payload.
Usage
Pass an initial payload, an initial value (or NONE), and a callback that receives the current payload:
import { PayloadFetchStore, NONE } from "shelving/store";
const store = new PayloadFetchStore<string, User>(
"u_1",
NONE,
async (id, signal) => (await fetch(`/api/users/${id}`, { signal })).json(),
);
const first = await store.next; // fetches for "u_1"
// Changing the payload cancels the previous fetch and starts a new one.
store.payload.value = "u_2";Pass a fourth debounce argument (milliseconds) to delay the fetch after a payload change — rapid changes reset the timer, so only the final payload is fetched.
Examples
const store = new PayloadFetchStore("dave", NONE, async (name, signal) => (await fetch(`/api/user/${name}`, { signal })).json());
store.payload.value = "sam"; // triggers a new fetch