-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improve performance for defineComponent
- Loading branch information
Showing
5 changed files
with
23 additions
and
100 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
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 |
---|---|---|
@@ -1,91 +1,6 @@ | ||
// ported from https://github.com/vuejs/vue-next/blob/master/packages/runtime-core/src/apiWatch.ts | ||
export { watch, watchEffect } from '@vue/runtime-core' | ||
|
||
/* eslint-disable array-callback-return */ | ||
import { Ref, ComputedRef, ReactiveEffectOptions } from '@vue/reactivity' | ||
import { watch as _watch, watchEffect as _watchEffect } from '@vue/runtime-core' | ||
|
||
export type WatchEffect = (onInvalidate: InvalidateCbRegistrator) => void | ||
|
||
export type WatchSource<T = any> = Ref<T> | ComputedRef<T> | (() => T) | ||
|
||
export type WatchCallback<V = any, OV = any> = ( | ||
value: V, | ||
oldValue: OV, | ||
onInvalidate: InvalidateCbRegistrator | ||
) => any | ||
|
||
export type WatchStopHandle = () => void | ||
|
||
type MapSources<T> = { | ||
[K in keyof T]: T[K] extends WatchSource<infer V> | ||
? V | ||
: T[K] extends object ? T[K] : never | ||
} | ||
|
||
type MapOldSources<T, Immediate> = { | ||
[K in keyof T]: T[K] extends WatchSource<infer V> | ||
? Immediate extends true ? (V | undefined) : V | ||
: T[K] extends object | ||
? Immediate extends true ? (T[K] | undefined) : T[K] | ||
: never | ||
} | ||
|
||
type InvalidateCbRegistrator = (cb: () => void) => void | ||
|
||
export interface WatchOptionsBase { | ||
flush?: 'pre' | 'post' | 'sync' | ||
onTrack?: ReactiveEffectOptions['onTrack'] | ||
onTrigger?: ReactiveEffectOptions['onTrigger'] | ||
} | ||
|
||
export interface WatchOptions<Immediate = boolean> extends WatchOptionsBase { | ||
immediate?: Immediate | ||
deep?: boolean | ||
} | ||
|
||
// Simple effect. | ||
export function watchEffect( | ||
effect: WatchEffect, | ||
options?: WatchOptionsBase, | ||
): WatchStopHandle { | ||
return _watchEffect(effect, options) | ||
} | ||
|
||
// overload #1: array of multiple sources + cb | ||
// Readonly constraint helps the callback to correctly infer value types based | ||
// on position in the source array. Otherwise the values will get a union type | ||
// of all possible value types. | ||
export function watch< | ||
T extends Readonly<Array<WatchSource<unknown> | object>>, | ||
Immediate extends Readonly<boolean> = false | ||
>( | ||
sources: T, | ||
cb: WatchCallback<MapSources<T>, MapOldSources<T, Immediate>>, | ||
options?: WatchOptions<Immediate> | ||
): WatchStopHandle | ||
|
||
// overload #2: single source + cb | ||
export function watch<T, Immediate extends Readonly<boolean> = false>( | ||
source: WatchSource<T>, | ||
cb: WatchCallback<T, Immediate extends true ? (T | undefined) : T>, | ||
options?: WatchOptions<Immediate> | ||
): WatchStopHandle | ||
|
||
// overload #3: watching reactive object w/ cb | ||
export function watch< | ||
T extends object, | ||
Immediate extends Readonly<boolean> = false | ||
>( | ||
source: T, | ||
cb: WatchCallback<T, Immediate extends true ? (T | undefined) : T>, | ||
options?: WatchOptions<Immediate> | ||
): WatchStopHandle | ||
|
||
// implementation | ||
export function watch<T = any>( | ||
source: WatchSource<T> | WatchSource<T>[], | ||
cb: WatchCallback<T>, | ||
options?: WatchOptions, | ||
): WatchStopHandle { | ||
return _watch(source, cb as any, options) | ||
} | ||
export type { | ||
WatchEffect, WatchSource, WatchCallback, WatchStopHandle, | ||
WatchOptions, WatchOptionsBase, | ||
} from '@vue/runtime-core' |