Skip to content

Commit

Permalink
fix: update types to algin with vue-next (#653)
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Mar 3, 2021
1 parent b3bb2e7 commit 24eaa56
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 51 deletions.
49 changes: 28 additions & 21 deletions src/apis/computed.ts
Expand Up @@ -7,32 +7,39 @@ import {
getVueInternalClasses,
} from '../utils'

interface Option<T> {
get: () => T
set: (value: T) => void
}

export interface ComputedRef<T = any> extends WritableComputedRef<T> {
readonly value: T
}

export interface WritableComputedRef<T> extends Ref<T> {}

export type ComputedGetter<T> = (ctx?: any) => T
export type ComputedSetter<T> = (v: T) => void

export interface WritableComputedOptions<T> {
get: ComputedGetter<T>
set: ComputedSetter<T>
}

// read-only
export function computed<T>(getter: Option<T>['get']): ComputedRef<T>
export function computed<T>(getter: ComputedGetter<T>): ComputedRef<T>
// writable
export function computed<T>(options: Option<T>): WritableComputedRef<T>
export function computed<T>(
options: WritableComputedOptions<T>
): WritableComputedRef<T>
// implement
export function computed<T>(
options: Option<T>['get'] | Option<T>
getterOrOptions: ComputedGetter<T> | WritableComputedOptions<T>
): ComputedRef<T> | WritableComputedRef<T> {
const vm = getCurrentInstance()?.proxy
let get: Option<T>['get'], set: Option<T>['set'] | undefined
if (typeof options === 'function') {
get = options
let getter: ComputedGetter<T>
let setter: ComputedSetter<T> | undefined

if (typeof getterOrOptions === 'function') {
getter = getterOrOptions
} else {
get = options.get
set = options.set
getter = getterOrOptions.get
setter = getterOrOptions.set
}

let computedSetter
Expand All @@ -43,7 +50,7 @@ export function computed<T>(
let watcher: any
computedGetter = () => {
if (!watcher) {
watcher = new Watcher(vm, get, noopFn, { lazy: true })
watcher = new Watcher(vm, getter, noopFn, { lazy: true })
}
if (watcher.dirty) {
watcher.evaluate()
Expand All @@ -55,22 +62,22 @@ export function computed<T>(
}

computedSetter = (v: T) => {
if (__DEV__ && !set) {
if (__DEV__ && !setter) {
warn('Write operation failed: computed value is readonly.', vm!)
return
}

if (set) {
set(v)
if (setter) {
setter(v)
}
}
} else {
// fallback
const computedHost = defineComponentInstance(getVueConstructor(), {
computed: {
$$state: {
get,
set,
get: getter,
set: setter,
},
},
})
Expand All @@ -79,7 +86,7 @@ export function computed<T>(

computedGetter = () => (computedHost as any).$$state
computedSetter = (v: T) => {
if (__DEV__ && !set) {
if (__DEV__ && !setter) {
warn('Write operation failed: computed value is readonly.', vm!)
return
}
Expand All @@ -93,6 +100,6 @@ export function computed<T>(
get: computedGetter,
set: computedSetter,
},
!set
!setter
)
}
27 changes: 1 addition & 26 deletions src/apis/index.ts
@@ -1,29 +1,4 @@
export {
del,
isReactive,
isRef,
isRaw,
markRaw,
reactive,
ref,
customRef,
Ref,
set,
shallowReactive,
shallowRef,
toRaw,
toRef,
toRefs,
triggerRef,
unref,
UnwrapRef,
isReadonly,
shallowReadonly,
proxyRefs,
ShallowUnwrapRef,
readonly,
DeepReadonly,
} from '../reactivity'
export * from '../reactivity'
export * from './lifecycle'
export * from './watch'
export * from './computed'
Expand Down
8 changes: 4 additions & 4 deletions src/reactivity/index.ts
Expand Up @@ -10,17 +10,17 @@ export {
ref,
customRef,
isRef,
Ref,
createRef,
UnwrapRef,
toRefs,
toRef,
unref,
shallowRef,
triggerRef,
proxyRefs,
ShallowUnwrapRef,
} from './ref'
export { readonly, isReadonly, shallowReadonly, DeepReadonly } from './readonly'
export { readonly, isReadonly, shallowReadonly } from './readonly'
export { set } from './set'
export { del } from './del'

export type { Ref, ToRefs, UnwrapRef, ShallowUnwrapRef } from './ref'
export type { DeepReadonly } from './readonly'

0 comments on commit 24eaa56

Please sign in to comment.