Skip to content

Commit

Permalink
Example of how shallowReactive could be fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
skirtles-code committed Jun 29, 2024
1 parent 2f11a45 commit 073ab1c
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions packages/reactivity/src/collectionHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ function size(target: IterableCollections, isReadonly = false) {
}

function add(this: SetTypes, value: unknown) {
value = toRaw(value)
const target = toRaw(this)
const proto = getProto(target)
const hadKey = proto.has.call(target, value)
Expand All @@ -85,7 +84,6 @@ function add(this: SetTypes, value: unknown) {
}

function set(this: MapTypes, key: unknown, value: unknown) {
value = toRaw(value)
const target = toRaw(this)
const { has, get } = getProto(target)

Expand Down Expand Up @@ -248,8 +246,19 @@ function createInstrumentations() {
return size(this as unknown as IterableCollections)
},
has,
add,
set,
add(this: SetTypes, value: unknown) {
// This is what I was proposing...
return add.call(this, toRaw(value))

// ... but you could include the extra checks too:
// return add.call(this, !isShallow(value) && !isReadonly(value) ? toRaw(value) : value)
},
set(this: MapTypes, key: unknown, value: unknown) {
return set.call(this, key, toRaw(value))

// Same again, the extra checks could also be included:
// return set.call(this, key, !isShallow(value) && !isReadonly(value) ? toRaw(value) : value)
},
delete: deleteEntry,
clear,
forEach: createForEach(false, false),
Expand Down

0 comments on commit 073ab1c

Please sign in to comment.