-import { shallowReactive, isReactive, reactive } from '../src/reactive'
+import { isReactive, reactive, shallowReactive } from '../src/reactive'
+
import { effect } from '../src/effect'
describe('shallowReactive', () => {
expect(isReactive(props.n)).toBe(true)
})
+ // #2843
+ test('should allow shallow und normal reactive for same target', async () => {
+ const original = { foo: {} }
+ const shallowProxy = shallowReactive(original)
+ const reactiveProxy = reactive(original)
+ expect(shallowProxy).not.toBe(reactiveProxy)
+ expect(isReactive(shallowProxy.foo)).toBe(false)
+ expect(isReactive(reactiveProxy.foo)).toBe(true)
+ })
+
describe('collections', () => {
test('should be reactive', () => {
const shallowSet = shallowReactive(new Set())
-import { isReactive, isReadonly, shallowReadonly } from '../src'
+import { isReactive, isReadonly, readonly, shallowReadonly } from '../src'
describe('reactivity/shallowReadonly', () => {
test('should not make non-reactive properties reactive', () => {
).not.toHaveBeenWarned()
})
+ // #2843
+ test('should differentiate from normal readonly calls', async () => {
+ const original = { foo: {} }
+ const shallowProxy = shallowReadonly(original)
+ const reactiveProxy = readonly(original)
+ expect(shallowProxy).not.toBe(reactiveProxy)
+ expect(isReadonly(shallowProxy.foo)).toBe(false)
+ expect(isReadonly(reactiveProxy.foo)).toBe(true)
+ })
+
describe('collection/Map', () => {
;[Map, WeakMap].forEach(Collection => {
test('should make the map/weak-map readonly', () => {
ReactiveFlags,
Target,
readonlyMap,
- reactiveMap
+ reactiveMap,
+ shallowReactiveMap,
+ shallowReadonlyMap
} from './reactive'
import { TrackOpTypes, TriggerOpTypes } from './operations'
import {
return isReadonly
} else if (
key === ReactiveFlags.RAW &&
- receiver === (isReadonly ? readonlyMap : reactiveMap).get(target)
+ receiver ===
+ (isReadonly
+ ? shallow
+ ? shallowReadonlyMap
+ : readonlyMap
+ : shallow
+ ? shallowReactiveMap
+ : reactiveMap
+ ).get(target)
) {
return target
}
}
export const reactiveMap = new WeakMap<Target, any>()
+export const shallowReactiveMap = new WeakMap<Target, any>()
export const readonlyMap = new WeakMap<Target, any>()
+export const shallowReadonlyMap = new WeakMap<Target, any>()
const enum TargetType {
INVALID = 0,
target,
false,
mutableHandlers,
- mutableCollectionHandlers
+ mutableCollectionHandlers,
+ reactiveMap
)
}
target,
false,
shallowReactiveHandlers,
- shallowCollectionHandlers
+ shallowCollectionHandlers,
+ shallowReactiveMap
)
}
target,
true,
readonlyHandlers,
- readonlyCollectionHandlers
+ readonlyCollectionHandlers,
+ readonlyMap
)
}
target,
true,
shallowReadonlyHandlers,
- shallowReadonlyCollectionHandlers
+ shallowReadonlyCollectionHandlers,
+ shallowReadonlyMap
)
}
target: Target,
isReadonly: boolean,
baseHandlers: ProxyHandler<any>,
- collectionHandlers: ProxyHandler<any>
+ collectionHandlers: ProxyHandler<any>,
+ proxyMap: WeakMap<Target, any>
) {
if (!isObject(target)) {
if (__DEV__) {
return target
}
// target already has corresponding Proxy
- const proxyMap = isReadonly ? readonlyMap : reactiveMap
const existingProxy = proxyMap.get(target)
if (existingProxy) {
return existingProxy