Skip to content

Commit

Permalink
refactor(vue): use unref instead of access _value
Browse files Browse the repository at this point in the history
  • Loading branch information
undermoonn committed Oct 19, 2023
1 parent 3276b2d commit 4bbd08e
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 76 deletions.
59 changes: 5 additions & 54 deletions README.md
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`
10 changes: 5 additions & 5 deletions packages/vue/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
```vue
<script setup>
import { ref, onMounted } from 'vue'
import { reactiveShake } from '@object-shake/vue'
import { shakeMaybeRef } from '@object-shake/vue'
const target = ref({
a: {
Expand All @@ -11,17 +11,17 @@ const target = ref({
f: 4
})
const [proxyTarget, getShakedTarget] = reactiveShake(target)
const [proxy, getShaked] = shakeMaybeRef(target)
onMounted(() => {
console.log(getShakedTarget()) // { a: { b: { c: 1 } }, f: 4 }
console.log(getShaked()) // { a: { b: { c: 1 } }, f: 4 }
})
</script>
<template>
<div>
<span>{{ proxyTarget.a.b.c }}</span>
<span>{{ proxyTarget.f }}</span>
<span>{{ proxy.a.b.c }}</span>
<span>{{ proxy.f }}</span>
</div>
</template>
```
12 changes: 6 additions & 6 deletions packages/vue/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { ref, reactive } from '@vue/reactivity'
import { test, expect, describe } from 'vitest'
import { reactiveShake } from '.'
import { shakeMaybeRef } from '.'

describe('[preview vue] test', () => {
test('ref shake base', () => {
const [p, s] = reactiveShake(ref({ a: { b: 1 }, c: 2 }))
describe('shake object of vue', () => {
test('ref', () => {
const [p, s] = shakeMaybeRef(ref({ a: { b: 1 }, c: 2 }))
expect(s()).toEqual({})
p.value.a.b
expect(s()).toEqual({ a: { b: 1 } })
})

test('reactive shake base', () => {
const [p, s] = reactiveShake(reactive({ a: { b: 1 }, c: 2 }))
test('reactive', () => {
const [p, s] = shakeMaybeRef(reactive({ a: { b: 1 }, c: 2 }))
expect(s()).toEqual({})
p.a.b
expect(s()).toEqual({ a: { b: 1 } })
Expand Down
15 changes: 4 additions & 11 deletions packages/vue/src/index.ts
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]
}

0 comments on commit 4bbd08e

Please sign in to comment.