forked from vitest-dev/vitest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(toReactive): new function (vitest-dev#671)
- Loading branch information
Showing
7 changed files
with
185 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
--- | ||
category: Utilities | ||
--- | ||
|
||
# toReactive | ||
|
||
Converts ref to reactive. Also made possible to create a "swapable" reactive object. | ||
|
||
## Usage | ||
|
||
```ts | ||
import { toReactive } from '@vueuse/core' | ||
|
||
const refState = ref({ foo: 'bar' }) | ||
|
||
console.log(refState.value.foo) // => 'bar' | ||
|
||
const state = toReactive(refState) // <-- | ||
|
||
console.log(state.foo) // => 'bar' | ||
|
||
refState.value = { bar: 'foo' } | ||
|
||
console.log(state.foo) // => undefined | ||
console.log(state.bar) // => 'foo' | ||
``` | ||
|
||
<!--FOOTER_STARTS--> | ||
|
||
|
||
## Source | ||
|
||
[Source](https://github.com/vueuse/vueuse/blob/main/packages/shared/toReactive/index.ts) • [Docs](https://github.com/vueuse/vueuse/blob/main/packages/shared/toReactive/index.md) | ||
|
||
|
||
<!--FOOTER_ENDS--> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { toRefs } from '@vueuse/core' | ||
import { isReactive, nextTick, reactive, ref, watchSyncEffect } from 'vue-demi' | ||
import { toReactive } from '.' | ||
|
||
describe('toRefs', () => { | ||
it('should be defined', () => { | ||
expect(toReactive).toBeDefined() | ||
}) | ||
|
||
it('should work', () => { | ||
const r = ref({ a: 'a', b: 0 }) | ||
const state = toReactive(r) | ||
expect(state.a).toBe('a') | ||
expect(state.b).toBe(0) | ||
|
||
r.value.a = 'b' | ||
r.value.b = 1 | ||
expect(state.a).toBe('b') | ||
expect(state.b).toBe(1) | ||
}) | ||
|
||
it('should be enumerable', () => { | ||
const obj = { a: 'a', b: 0 } | ||
const r = ref(obj) | ||
const state = toReactive(r) | ||
|
||
expect(JSON.stringify(state)).toBe(JSON.stringify(r.value)) | ||
expect(state).toEqual(obj) | ||
}) | ||
|
||
it('should be reactive', async() => { | ||
const r = ref({ a: 'a', b: 0 }) | ||
const state = toReactive(r) | ||
let dummy = 0 | ||
|
||
expect(state.a).toBe('a') | ||
expect(state.b).toBe(0) | ||
expect(isReactive(state)).toBe(true) | ||
|
||
watchSyncEffect(() => { | ||
dummy = state.b | ||
}) | ||
|
||
expect(dummy).toBe(0) | ||
|
||
r.value.b += 1 | ||
|
||
expect(dummy).toBe(1) | ||
|
||
state.b += 1 | ||
|
||
expect(dummy).toBe(2) | ||
expect(r.value.b).toBe(2) | ||
}) | ||
|
||
it('should be replaceable', async() => { | ||
const r = ref<any>({ a: 'a', b: 0 }) | ||
const state = toReactive(r) | ||
let dummy = 0 | ||
|
||
expect(state.a).toBe('a') | ||
expect(state.b).toBe(0) | ||
|
||
watchSyncEffect(() => { | ||
dummy = state.b | ||
}) | ||
|
||
expect(dummy).toBe(0) | ||
|
||
r.value = { b: 1, a: 'a' } | ||
|
||
expect(dummy).toBe(1) | ||
|
||
state.b += 1 | ||
|
||
await nextTick() | ||
|
||
expect(dummy).toBe(2) | ||
|
||
r.value = { a: 'c' } | ||
|
||
expect(dummy).toBe(undefined) | ||
expect(state).toEqual({ a: 'c' }) | ||
}) | ||
|
||
it('toReactive(toRefs())', () => { | ||
const a = reactive({ a: 'a', b: 0 }) | ||
const b = toRefs(a) | ||
const c = toReactive(b) | ||
|
||
expect(a).toEqual(c) | ||
|
||
a.b = 1 | ||
|
||
expect(c.b).toEqual(1) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { isRef, toRefs as _toRefs, reactive } from 'vue-demi' | ||
import { MaybeRef } from '../utils' | ||
|
||
/** | ||
* Converts ref to reactive. | ||
* | ||
* @see https://vueuse.org/toReactive | ||
* @param objectRef A ref of object | ||
*/ | ||
export function toReactive<T extends object>( | ||
objectRef: MaybeRef<T>, | ||
): T { | ||
if (!isRef(objectRef)) | ||
return reactive(objectRef) as T | ||
|
||
const proxy = new Proxy({}, { | ||
get(_, p, receiver) { | ||
return Reflect.get(objectRef.value, p, receiver) | ||
}, | ||
set(_, p, value) { | ||
(objectRef.value as any)[p] = value | ||
return true | ||
}, | ||
deleteProperty(_, p) { | ||
return Reflect.deleteProperty(objectRef.value, p) | ||
}, | ||
has(_, p) { | ||
return Reflect.has(objectRef.value, p) | ||
}, | ||
ownKeys() { | ||
return Object.keys(objectRef.value) | ||
}, | ||
getOwnPropertyDescriptor() { | ||
return { | ||
enumerable: true, | ||
configurable: true, | ||
} | ||
}, | ||
}) | ||
|
||
return reactive(proxy) as T | ||
} |