Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(reactivity): ensure readonly on plain arrays doesn't track array methods. (close: #2493) #2506

Merged
merged 1 commit into from
Nov 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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