Skip to content

Commit

Permalink
refactor(watch): reuse watch types
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Aug 20, 2024
1 parent 7dbab27 commit f2ea25d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 23 deletions.
4 changes: 4 additions & 0 deletions packages/reactivity/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,8 @@ export {
type WatchScheduler,
type WatchStopHandle,
type WatchHandle,
type WatchEffect,
type WatchSource,
type WatchCallback,
type OnCleanup,
} from './watch'
11 changes: 7 additions & 4 deletions packages/reactivity/src/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,17 @@ export enum WatchErrorCodes {
WATCH_CLEANUP,
}

type WatchEffect = (onCleanup: OnCleanup) => void
type WatchSource<T = any> = Ref<T> | ComputedRef<T> | (() => T)
type WatchCallback<V = any, OV = any> = (
export type WatchEffect = (onCleanup: OnCleanup) => void

export type WatchSource<T = any> = Ref<T, any> | ComputedRef<T> | (() => T)

export type WatchCallback<V = any, OV = any> = (
value: V,
oldValue: OV,
onCleanup: OnCleanup,
) => any
type OnCleanup = (cleanupFn: () => void) => void

export type OnCleanup = (cleanupFn: () => void) => void

export interface WatchOptions<Immediate = boolean> extends DebuggerOptions {
immediate?: Immediate
Expand Down
32 changes: 14 additions & 18 deletions packages/runtime-core/src/apiWatch.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import {
type WatchOptions as BaseWatchOptions,
type ComputedRef,
type DebuggerOptions,
type ReactiveMarker,
type Ref,
type WatchCallback,
type WatchEffect,
type WatchHandle,
type WatchSource,
watch as baseWatch,
} from '@vue/reactivity'
import { type SchedulerJob, SchedulerJobFlags, queueJob } from './scheduler'
Expand All @@ -21,17 +22,14 @@ import { warn } from './warning'
import type { ObjectWatchOptionItem } from './componentOptions'
import { useSSRContext } from './helpers/useSsrContext'

export type { WatchHandle, WatchStopHandle } from '@vue/reactivity'

export type WatchEffect = (onCleanup: OnCleanup) => void

export type WatchSource<T = any> = Ref<T, any> | ComputedRef<T> | (() => T)

export type WatchCallback<V = any, OV = any> = (
value: V,
oldValue: OV,
onCleanup: OnCleanup,
) => any
export type {
WatchHandle,
WatchStopHandle,
WatchEffect,
WatchSource,
WatchCallback,
OnCleanup,
} from '@vue/reactivity'

type MaybeUndefined<T, I> = I extends true ? T | undefined : T

Expand All @@ -43,13 +41,11 @@ type MapSources<T, Immediate> = {
: never
}

export type OnCleanup = (cleanupFn: () => void) => void

export interface WatchOptionsBase extends DebuggerOptions {
export interface WatchEffectOptions extends DebuggerOptions {
flush?: 'pre' | 'post' | 'sync'
}

export interface WatchOptions<Immediate = boolean> extends WatchOptionsBase {
export interface WatchOptions<Immediate = boolean> extends WatchEffectOptions {
immediate?: Immediate
deep?: boolean | number
once?: boolean
Expand All @@ -58,7 +54,7 @@ export interface WatchOptions<Immediate = boolean> extends WatchOptionsBase {
// Simple effect.
export function watchEffect(
effect: WatchEffect,
options?: WatchOptionsBase,
options?: WatchEffectOptions,
): WatchHandle {
return doWatch(effect, null, options)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ export type {
MultiWatchSources,
WatchEffect,
WatchOptions,
WatchOptionsBase,
WatchEffectOptions as WatchOptionsBase,
WatchCallback,
WatchSource,
WatchHandle,
Expand Down

0 comments on commit f2ea25d

Please sign in to comment.