mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-28 15:40:06 +00:00
6926591112
Typing a new Mapbox token or CARTO key and clicking that field's reset link straight away sent two requests at once: the field's save on blur, then the reset. Nothing ordered them, so when the server handled the save last, the key typed a moment ago was stored again while the field showed it cleared, on the desktop tab and on the phone screen alike. The hook both shells share now sends its writes one after another, in the order they were made (useWritesInOrder; a write that fails does not hold up the next). FE-ADMIN-DUS-032 types, clicks reset at once and makes the save the slower request; it fails without the order. FE-HOOK-WRITES-001 to 003 cover the helper.
19 lines
681 B
TypeScript
19 lines
681 B
TypeScript
import { useRef } from 'react';
|
|
|
|
/**
|
|
* Runs async writes one after another, in the order they were started.
|
|
*
|
|
* Two writes to the same record that leave close together, such as a field saved on
|
|
* blur and a reset clicked right after it, otherwise race: the server may handle them
|
|
* in either order, and whichever it handles last is what stays. A write that fails
|
|
* does not hold up the ones queued behind it.
|
|
*/
|
|
export function useWritesInOrder(): (write: () => Promise<void>) => Promise<void> {
|
|
const tail = useRef<Promise<void>>(Promise.resolve());
|
|
return (write) => {
|
|
const next = tail.current.then(write, write);
|
|
tail.current = next;
|
|
return next;
|
|
};
|
|
}
|