@homebound/form-state 2.9.0 → 2.9.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/dist/useFormStates.js +26 -25
- package/dist/useFormStates.test.js +0 -1
- package/package.json +1 -1
package/dist/useFormStates.js
CHANGED
|
@@ -11,38 +11,39 @@ function useFormStates(opts) {
|
|
|
11
11
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
12
12
|
[config]);
|
|
13
13
|
// Keep track of ObjectStates that triggered auto-save when a save was already in progress.
|
|
14
|
-
const pendingAutoSaves = react_1.useRef(
|
|
14
|
+
const pendingAutoSaves = react_1.useRef(new Set());
|
|
15
15
|
// Use a ref so our memo'ized `autoSave` always see the latest value
|
|
16
16
|
const autoSaveRef = react_1.useRef(autoSave);
|
|
17
17
|
autoSaveRef.current = autoSave;
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
isAutoSaving
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
// If a reaction re-queued our form during the ^ wait, remove it
|
|
29
|
-
let i = -1;
|
|
30
|
-
while ((i = pendingAutoSaves.current.indexOf(form)) !== -1) {
|
|
31
|
-
pendingAutoSaves.current.splice(i, 1);
|
|
18
|
+
const getFormState = react_1.useCallback((input) => {
|
|
19
|
+
const existing = objectStateCache[getId(input)];
|
|
20
|
+
let form = existing === null || existing === void 0 ? void 0 : existing[0];
|
|
21
|
+
async function maybeAutoSave(form) {
|
|
22
|
+
// Don't use canSave() because we don't want to set touched for all the fields
|
|
23
|
+
if (autoSaveRef.current && form.dirty && form.valid) {
|
|
24
|
+
const { current: pending } = pendingAutoSaves;
|
|
25
|
+
if (isAutoSaving) {
|
|
26
|
+
pending.add(form);
|
|
27
|
+
return;
|
|
32
28
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
29
|
+
try {
|
|
30
|
+
isAutoSaving = true;
|
|
31
|
+
// See if we have any reactions that want to run (i.e. added by addRules hooks)
|
|
32
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
33
|
+
// If a reaction re-queued our form during the ^ wait, remove it
|
|
34
|
+
pending.delete(form);
|
|
35
|
+
await autoSaveRef.current(form);
|
|
36
|
+
}
|
|
37
|
+
finally {
|
|
38
|
+
isAutoSaving = false;
|
|
39
|
+
if (pending.size > 0) {
|
|
40
|
+
const first = pending.values().next().value;
|
|
41
|
+
pending.delete(first);
|
|
42
|
+
await maybeAutoSave(first);
|
|
43
|
+
}
|
|
39
44
|
}
|
|
40
45
|
}
|
|
41
46
|
}
|
|
42
|
-
}
|
|
43
|
-
const getFormState = react_1.useCallback((input) => {
|
|
44
|
-
const existing = objectStateCache[getId(input)];
|
|
45
|
-
let form = existing === null || existing === void 0 ? void 0 : existing[0];
|
|
46
47
|
// If it didn't exist, then add to the cache.
|
|
47
48
|
if (!form) {
|
|
48
49
|
form = formState_1.createObjectState(config, utils_1.initValue(config, map ? { map, input } : input), {
|