Skip to content

Commit

Permalink
fix(reactivity): should not observe frozen objects
Browse files Browse the repository at this point in the history
fix #867
  • Loading branch information
yyx990803 committed Mar 23, 2020
1 parent 0dc2478 commit 1b2149d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
7 changes: 7 additions & 0 deletions packages/reactivity/__tests__/reactive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,13 @@ describe('reactivity/reactive', () => {
expect(isReactive(obj.bar)).toBe(false)
})

test('should not observe frozen objects', () => {
const obj = reactive({
foo: Object.freeze({ a: 1 })
})
expect(isReactive(obj.foo)).toBe(false)
})

describe('shallowReactive', () => {
test('should not make non-reactive properties reactive', () => {
const props = shallowReactive({ n: { foo: 1 } })
Expand Down
3 changes: 2 additions & 1 deletion packages/reactivity/src/reactive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ const canObserve = (value: any): boolean => {
!value._isVue &&
!value._isVNode &&
isObservableType(toRawType(value)) &&
!nonReactiveValues.has(value)
!nonReactiveValues.has(value) &&
!Object.isFrozen(value)

This comment has been minimized.

Copy link
@thecrypticace

thecrypticace Mar 23, 2020

Contributor

Will this be able to watch changes inside of a frozen object (e.g. a frozen object/array with items/properties containing not-frozen objects)? Or is Object.freeze going to intentionally skip all reactive changes inside and object and all sub-objects?

This comment has been minimized.

Copy link
@LinusBorg

LinusBorg Mar 23, 2020

Member

This utility function is only used to determine wether an object can be "made reactive" - it doesn't affect reactive objects that may be nested inside.

This comment has been minimized.

Copy link
@thecrypticace

thecrypticace Mar 23, 2020

Contributor

Oh you're totally right, I missed that. Thanks!

)
}

Expand Down

0 comments on commit 1b2149d

Please sign in to comment.