@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.
@@ -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
- async function maybeAutoSave(form) {
19
- if (isAutoSaving && !pendingAutoSaves.current.includes(form)) {
20
- pendingAutoSaves.current.push(form);
21
- }
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 && !isAutoSaving) {
24
- try {
25
- isAutoSaving = true;
26
- // See if we have any reactions that want to run (i.e. added by addRules hooks)
27
- await new Promise((resolve) => setTimeout(resolve, 0));
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
- await autoSaveRef.current(form);
34
- }
35
- finally {
36
- isAutoSaving = false;
37
- if (pendingAutoSaves.current.length > 0) {
38
- await maybeAutoSave(pendingAutoSaves.current.shift());
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), {
@@ -167,7 +167,6 @@ describe("useFormStates", () => {
167
167
  addRules(fs) {
168
168
  // And they have a reactive true that calculates last name
169
169
  mobx_1.reaction(() => fs.firstName.value, (curr) => {
170
- console.log({ curr });
171
170
  fs.lastName.set(curr);
172
171
  });
173
172
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@homebound/form-state",
3
- "version": "2.9.0",
3
+ "version": "2.9.1",
4
4
  "author": "Homebound",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.js",