Skip to content

Commit

Permalink
fix(reactivity): ensure readonly on plain arrays doesn't track array …
Browse files Browse the repository at this point in the history
…methods. (#2506)

fix #2493
  • Loading branch information
LinusBorg authored Nov 27, 2020
1 parent 53f4885 commit 3470308
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
10 changes: 10 additions & 0 deletions packages/reactivity/__tests__/readonly.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,16 @@ describe('reactivity/readonly', () => {
expect(dummy).toBe(1)
})

test('readonly array should not track', () => {
const arr = [1]
const roArr = readonly(arr)

const eff = effect(() => {
roArr.includes(2)
})
expect(eff.deps.length).toBe(0)
})

test('readonly should track and trigger if wrapping reactive original (collection)', () => {
const a = reactive(new Map())
const b = readonly(a)
Expand Down
3 changes: 2 additions & 1 deletion packages/reactivity/src/baseHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ function createGetter(isReadonly = false, shallow = false) {
}

const targetIsArray = isArray(target)
if (targetIsArray && hasOwn(arrayInstrumentations, key)) {

if (!isReadonly && targetIsArray && hasOwn(arrayInstrumentations, key)) {
return Reflect.get(arrayInstrumentations, key, receiver)
}

Expand Down

0 comments on commit 3470308

Please sign in to comment.