@homebound/form-state 2.27.1 → 2.27.2

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.
@@ -114,12 +114,18 @@ function newValueFieldState(parentCopy, parentInstance, parentState, key, rules,
114
114
  if (opts.refreshing && this.dirty) {
115
115
  // See if we should ignore the incoming server-side value, to avoid dropping local WIP changes
116
116
  const isAckingUnset = this.value === null && (value === null || value === undefined);
117
- const acceptServerAck = hasInflightChangedValue && this.value === lastChangedValue;
117
+ const acceptServerAck = hasInflightChangedValue &&
118
+ this.value === lastChangedValue &&
119
+ // If value === originalValue, this is likely a cache refresh firing in-between "we put the new value
120
+ // on the wire" and "the server acked our change". Granted, this heuristic means that if the server
121
+ // really does reject/rollback our change, we'll ignore it, but atm we don't have a way of differentiating
122
+ // "this refresh is from a pre-response cache refresh" vs. "post-response cache refresh".
123
+ value !== this.originalValue;
118
124
  // Ignore incoming values if we have changes (this.dirty) unless:
119
125
  // - our latest change (this.value) matches the incoming value (value), i.e. the server
120
126
  // is exactly acking our change, or
121
127
  // - the user hasn't made any changes since `.changedValue` was put on the wire, i.e. the
122
- // server received our value, but wanted to to change it.
128
+ // server received our value, but wanted to change it.
123
129
  const keepLocalWipChange = this.value !== value && !isAckingUnset && !acceptServerAck;
124
130
  if (keepLocalWipChange)
125
131
  return;
@@ -1592,6 +1592,18 @@ describe("formState", () => {
1592
1592
  expect(formState.firstName.value).toEqual("FF");
1593
1593
  expect(formState.firstName.dirty).toEqual(false);
1594
1594
  });
1595
+ it("ignores cache refresh of our original value", () => {
1596
+ const formState = (0, objectField_1.createObjectState)(authorWithBooksConfig, { id: "a:1", firstName: "f" });
1597
+ // Given first name has wip edits
1598
+ formState.firstName.set("ff");
1599
+ // And we've called changedValue to put it on the wire
1600
+ expect(formState.changedValue).toEqual({ id: "a:1", firstName: "ff" });
1601
+ // But then a cache refresh fires with our original `f`
1602
+ formState.set({ firstName: "f" }, { refreshing: true });
1603
+ // Then we keep our WIP value
1604
+ expect(formState.firstName.value).toEqual("ff");
1605
+ expect(formState.firstName.dirty).toEqual(true);
1606
+ });
1595
1607
  it("rejects server result if we have further changes", () => {
1596
1608
  const formState = (0, objectField_1.createObjectState)(authorWithBooksConfig, { id: "a:1", firstName: "f" });
1597
1609
  // Given first name has wip edits
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@homebound/form-state",
3
- "version": "2.27.1",
3
+ "version": "2.27.2",
4
4
  "author": "Homebound",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.js",