-
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.
refactor(vue): use unref instead of access
_value
- Loading branch information
1 parent
3276b2d
commit 4bbd08e
Showing
4 changed files
with
20 additions
and
76 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,10 @@ | ||
# object-shake | ||
# Object Shake | ||
|
||
### Use Cases | ||
|
||
#### - `Vanilla` | ||
- [Vanilla](./packages/core/README.md) | ||
- [Vue3](./packages/vue/README.md) | ||
|
||
```js | ||
import { shake } from '@object-shake/core' | ||
### Performance | ||
|
||
const target = { | ||
a: { | ||
b: { c: 1, d: 2 }, | ||
e: 3 | ||
}, | ||
f: 4 | ||
} | ||
|
||
const [proxyTarget, getShakedTarget] = shake(target) | ||
|
||
proxyTarget.a.b.c | ||
console.log(getShakedTarget()) // { a: { b: { c: 1 } } } | ||
|
||
proxyTarget.f | ||
console.log(getShakedTarget()) // { a: { b: { c: 1 } }, f: 4 } | ||
``` | ||
|
||
#### - `Vue3` | ||
|
||
```vue | ||
<script setup> | ||
import { ref, onMounted } from 'vue' | ||
import { reactiveShake } from '@object-shake/vue' | ||
const target = ref({ | ||
a: { | ||
b: { c: 1, d: 2 }, | ||
e: 3 | ||
}, | ||
f: 4 | ||
}) | ||
const [proxyTarget, getShakedTarget] = reactiveShake(target) | ||
onMounted(() => { | ||
console.log(getShakedTarget()) // { a: { b: { c: 1 } }, f: 4 } | ||
}) | ||
</script> | ||
<template> | ||
<div> | ||
<span>{{ proxyTarget.a.b.c }}</span> | ||
<span>{{ proxyTarget.f }}</span> | ||
</div> | ||
</template> | ||
``` | ||
|
||
### Perf | ||
|
||
- performance Test in Nuxt3: `shake state` vs `state` -> https://github.com/undermoonn/vue-reactive-shake-perf | ||
- Benchmark in [Nuxt3](https://github.com/undermoonn/vue-reactive-shake-perf): `useState` vs `useStateShake` |
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 |
---|---|---|
@@ -1,14 +1,7 @@ | ||
import { shake } from '@object-shake/core' | ||
import { MaybeRef, unref, UnwrapRef } from '@vue/reactivity' | ||
import { unref, ref, isRef, type UnwrapRef, type Ref } from '@vue/reactivity' | ||
|
||
export function reactiveShake<T extends MaybeRef<object>>(target: T): [T, () => UnwrapRef<T>] { | ||
const [p, s] = shake(target) | ||
|
||
return [ | ||
p, | ||
() => { | ||
const res = unref(s()) | ||
return (res as any)._value || res | ||
} | ||
] | ||
export function shakeMaybeRef<T extends object | Ref>(target: T): [T, () => UnwrapRef<T>] { | ||
const [proxy, getShaked] = shake(unref(target)) | ||
return [isRef(target) ? ref(proxy) : proxy, getShaked] | ||
} |