Skip to content

Commit

Permalink
fix(finalization): fix proxy revoked error in finalization with marking
Browse files Browse the repository at this point in the history
  • Loading branch information
unadlib committed Apr 19, 2024
1 parent d5d9b48 commit c51115d
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/utils/finalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ import {
} from './draft';
import { forEach } from './forEach';

export function handleValue(target: any, handledSet: WeakSet<any>) {
export function handleValue(
target: any,
handledSet: WeakSet<any>,
options?: ProxyDraft['options']
) {
if (
isDraft(target) ||
!isDraftable(target) ||
!isDraftable(target, options) ||
handledSet.has(target) ||
Object.isFrozen(target)
)
Expand All @@ -35,7 +39,7 @@ export function handleValue(target: any, handledSet: WeakSet<any>) {
// final update value
set(isSet ? setMap! : target, key, updatedValue);
} else {
handleValue(value, handledSet);
handleValue(value, handledSet, options);
}
});
if (setMap) {
Expand All @@ -57,7 +61,11 @@ export function finalizeAssigned(proxyDraft: ProxyDraft, key: PropertyKey) {
proxyDraft.assignedMap!.get(key) &&
copy
) {
handleValue(get(copy, key), proxyDraft.finalities.handledSet);
handleValue(
get(copy, key),
proxyDraft.finalities.handledSet,
proxyDraft.options
);
}
}

Expand Down
47 changes: 47 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
Draft,
markSimpleObject,
rawReturn,
makeCreator,
} from '../src';
import { PROXY_DRAFT } from '../src/constant';

Expand Down Expand Up @@ -3881,3 +3882,49 @@ test('object with non-enumerable Symbol key at root - 1', () => {
foobar: 'str',
});
});

test('#37 - Proxy revoked error with `mark` option when performing chai deep equality', () => {
class Entity {
constructor(public readonly name: string) {}
}

class Group {
constructor(
public readonly id: string,
public readonly children: Entity[]
) {}

public clone() {
return new Group(this.id, this.children);
}
}

class Data {
constructor(public readonly groupData: Record<string, Group>) {}
}

const create = makeCreator({
mark: () => 'immutable',
});

const entity1 = new Entity('entity1');
const entity2 = new Entity('entity2');

const data = new Data({
id1: new Group('id1', []),
id2: new Group('id2', [entity1]),
id3: new Group('id3', [entity2]),
});

expect(
create(data, (draft) => {
draft.groupData.id1 = draft.groupData.id1.clone();
})
).toEqual(
new Data({
id1: new Group('id1', []),
id2: new Group('id2', [entity1]),
id3: new Group('id3', [entity2]),
})
);
});

0 comments on commit c51115d

Please sign in to comment.