@stenajs-webui/core 23.21.16 → 23.22.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.
@@ -0,0 +1,72 @@
1
+ export interface UseDebouncedWriteOptions<T> {
2
+ /**
3
+ * How long scheduling has to pause before the latest value is written, in
4
+ * milliseconds.
5
+ */
6
+ delay: number;
7
+ /**
8
+ * Called when a write fails, with the value it was writing.
9
+ *
10
+ * A failed write does not stop the queue. A value scheduled while it was in
11
+ * flight is newer than the one that failed, so it is still written.
12
+ */
13
+ onError?: (error: unknown, value: T) => void;
14
+ /**
15
+ * Called once everything scheduled has been written, whether each write
16
+ * succeeded or not. Useful for re-reading what was actually stored.
17
+ *
18
+ * Not called when there turned out to be nothing to write.
19
+ */
20
+ onSettled?: () => void;
21
+ }
22
+ export interface UseDebouncedWriteResult<T> {
23
+ /**
24
+ * Schedules a value to be written. It replaces any value that has not
25
+ * started writing yet.
26
+ */
27
+ schedule: (value: T) => void;
28
+ /**
29
+ * Writes the scheduled value now, without waiting for the delay. Resolves
30
+ * once nothing is left to write, including anything scheduled while it
31
+ * waits. Never rejects; failures go to onError.
32
+ */
33
+ flush: () => Promise<void>;
34
+ /**
35
+ * Drops the scheduled value if it has not started writing yet. A write that
36
+ * is already in flight cannot be stopped, and finishes as normal.
37
+ */
38
+ cancel: () => void;
39
+ }
40
+ /**
41
+ * Writes the latest scheduled value once scheduling has paused, and never has
42
+ * more than one write in flight.
43
+ *
44
+ * Meant for input the user can repeat quickly, where each change is sent
45
+ * somewhere: steppers, counters, sliders, autosave. The delay turns a burst of
46
+ * changes into a single write. The serialisation is the part that is easy to
47
+ * get wrong: two writes in flight at once race each other, and a slow network
48
+ * can release them together or deliver them out of order.
49
+ *
50
+ * A value scheduled while a write is in flight waits for it, and is written as
51
+ * soon as it finishes. Only the latest waiting value is written. The ones it
52
+ * replaced are no longer what the user is asking for.
53
+ *
54
+ * Serialisation is per hook instance. Two components using this hook against
55
+ * the same resource each have their own queue, and can still overlap.
56
+ *
57
+ * On unmount, a value that has not started writing is dropped, the same as
58
+ * cancel. Call flush first if it must be written, for example before
59
+ * navigating away. A write already in flight finishes, and onSettled still
60
+ * runs.
61
+ *
62
+ * The callbacks are always read from the latest render, so a queued value is
63
+ * written with the write function as it is now, not as it was when the value
64
+ * was scheduled. The returned functions never change identity.
65
+ *
66
+ * @example
67
+ * const { schedule } = useDebouncedWrite(
68
+ * (count: number) => savePassengerCount({ bookingId, count }),
69
+ * { delay: 400, onSettled: refetchBooking },
70
+ * );
71
+ */
72
+ export declare const useDebouncedWrite: <T>(write: (value: T) => Promise<unknown>, { delay, onError, onSettled }: UseDebouncedWriteOptions<T>) => UseDebouncedWriteResult<T>;
package/dist/index.d.ts CHANGED
@@ -31,6 +31,7 @@ export * from "./types/PickByValue";
31
31
  export * from "./hooks/UseArraySet";
32
32
  export * from "./hooks/UseBoolean";
33
33
  export * from "./hooks/UseDebounce";
34
+ export * from "./hooks/UseDebouncedWrite";
34
35
  export * from "./hooks/UseDelayedFalse";
35
36
  export * from "./hooks/UseDomId";
36
37
  export * from "./hooks/UseElementDimensions";