Skip to content

Commit

Permalink
fix(patch): filter no-referenced patches (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
unadlib authored Nov 25, 2023
1 parent f746039 commit 5abdd7e
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 8 deletions.
10 changes: 8 additions & 2 deletions src/utils/draft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,19 @@ export function isDraftable(value: any, options?: { mark?: Mark<any, any> }) {
export function getPath(
target: ProxyDraft,
path: any[] = []
): (string | number | object)[] {
if (Object.hasOwnProperty.call(target, 'key'))
): (string | number | object)[] | null {
if (Object.hasOwnProperty.call(target, 'key')) {
// check if the parent is a draft and the original value is not equal to the current value
const proxyDraft = getProxyDraft(get(target.parent!.copy, target.key!));
if (proxyDraft !== null && proxyDraft?.original !== target.original) {
return null;
}
path.push(
target.parent!.type === DraftType.Set
? Array.from(target.parent!.setMap!.keys()).indexOf(target.key)
: target.key
);
}
if (target.parent) {
return getPath(target.parent, path);
}
Expand Down
4 changes: 3 additions & 1 deletion src/utils/finalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ export function finalizePatches(
if (shouldFinalize) {
if (patches && inversePatches) {
const basePath = getPath(target);
generatePatches(target, basePath, patches, inversePatches);
if (basePath) {
generatePatches(target, basePath, patches, inversePatches);
}
}
target.finalized = true;
}
Expand Down
41 changes: 37 additions & 4 deletions test/immer-non-support.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ test('circular reference', () => {
}
});

test('#18 - set: assigning a non-draft with the same key', () => {
test('#18 - set: assigning a non-draft with the same key - 1', () => {
const baseState = {
array: [
{
Expand Down Expand Up @@ -345,7 +345,7 @@ test('#18 - set: assigning a non-draft with the same key', () => {
// @ts-ignore
expect(Array.from(created[0].array[0].one)[0].three).toBe(2);
expect(apply(baseState, created[1])).toEqual(created[0]);
// expect(apply(created[0], created[2])).toEqual(baseState);
expect(apply(created[0], created[2])).toEqual(baseState);

enablePatches();
// @ts-ignore
Expand All @@ -369,7 +369,40 @@ test('#18 - set: assigning a non-draft with the same key', () => {
}).toThrowError();

// @ts-ignore
// expect(applyPatches(baseState, produced[1])).toEqual(produced[0]);
expect(() => applyPatches(baseState, produced[1])).toThrowError();
// @ts-ignore
// expect(applyPatches(produced[0], produced[2])).toEqual(baseState);
expect(applyPatches(produced[0], produced[2])).toEqual(baseState);
});

test('#18 - set: assigning a non-draft with the same key - 2', () => {
const baseState = { c: [{ a: 1 }, { a: 1 }] };
enablePatches();
// @ts-ignore
const produced = produceWithPatches(baseState, (draft) => {
const f = draft.c.pop();
// @ts-ignore
f.a = 2;
// @ts-ignore
draft.c = new Set([draft.c[0], f]);
});
// @ts-ignore
expect(() => applyPatches(baseState, produced[1])).toThrowError();
// @ts-ignore
expect(applyPatches(produced[0], produced[2])).toEqual(baseState);

const created = create(
baseState,
(draft) => {
const f = draft.c.pop();
// @ts-ignore
f.a = 2;
// @ts-ignore
draft.c = new Set([draft.c[0], f]);
},
{
enablePatches: true,
}
);
expect(apply(baseState, created[1])).toEqual(created[0]);
expect(apply(created[0], created[2])).toEqual(baseState);
});
51 changes: 50 additions & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3059,7 +3059,56 @@ test('#18 - set: assigning a non-draft with the same key - deep6', () => {
// @ts-ignore
expect(apply(baseState, created[1])).toEqual(created[0]);
// @ts-ignore
// expect(apply(created[0], created[2])).toEqual(baseState);
expect(apply(created[0], created[2])).toEqual(baseState);
expect(created).toMatchInlineSnapshot(`
[
{
"array": [
{
"one": Set {
{
"three": 2,
},
},
},
],
},
[
{
"op": "replace",
"path": [
"array",
],
"value": [
{
"one": Set {
{
"three": 2,
},
},
},
],
},
],
[
{
"op": "replace",
"path": [
"array",
],
"value": [
{
"one": {
"two": {
"three": 3,
},
},
},
],
},
],
]
`);
});

test('#18 - set: assigning a non-draft with the same key - deep3', () => {
Expand Down
1 change: 1 addition & 0 deletions test/json-patch/duplexSpec.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable prefer-arrow-callback */
// @ts-nocheck
import { create, apply } from '../../src';

Expand Down

0 comments on commit 5abdd7e

Please sign in to comment.