|
|
@@ -3,10 +3,13 @@ import {
|
|
|
type BaseWatchMiddleware,
|
|
|
type BaseWatchOptions,
|
|
|
baseWatch,
|
|
|
- getCurrentScope,
|
|
|
} from '@vue/reactivity'
|
|
|
-import { NOOP, invokeArrayFns, remove } from '@vue/shared'
|
|
|
-import { type ComponentInternalInstance, currentInstance } from './component'
|
|
|
+import { NOOP, extend, invokeArrayFns, remove } from '@vue/shared'
|
|
|
+import {
|
|
|
+ type ComponentInternalInstance,
|
|
|
+ getCurrentInstance,
|
|
|
+ setCurrentInstance,
|
|
|
+} from './component'
|
|
|
import {
|
|
|
createVaporRenderingScheduler,
|
|
|
queuePostRenderEffect,
|
|
|
@@ -15,6 +18,12 @@ import { handleError as handleErrorWithInstance } from './errorHandling'
|
|
|
import { warn } from './warning'
|
|
|
import { invokeDirectiveHook } from './directive'
|
|
|
|
|
|
+interface RenderWatchOptions {
|
|
|
+ immediate?: boolean
|
|
|
+ deep?: boolean
|
|
|
+ once?: boolean
|
|
|
+}
|
|
|
+
|
|
|
type WatchStopHandle = () => void
|
|
|
|
|
|
export function renderEffect(effect: () => void): WatchStopHandle {
|
|
|
@@ -24,20 +33,25 @@ export function renderEffect(effect: () => void): WatchStopHandle {
|
|
|
export function renderWatch(
|
|
|
source: any,
|
|
|
cb: (value: any, oldValue: any) => void,
|
|
|
+ options?: RenderWatchOptions,
|
|
|
): WatchStopHandle {
|
|
|
- return doWatch(source as any, cb)
|
|
|
+ return doWatch(source as any, cb, options)
|
|
|
}
|
|
|
|
|
|
-function doWatch(source: any, cb?: any): WatchStopHandle {
|
|
|
- const extendOptions: BaseWatchOptions = {}
|
|
|
+function doWatch(
|
|
|
+ source: any,
|
|
|
+ cb?: any,
|
|
|
+ options?: RenderWatchOptions,
|
|
|
+): WatchStopHandle {
|
|
|
+ const extendOptions: BaseWatchOptions =
|
|
|
+ cb && options ? extend({}, options) : {}
|
|
|
|
|
|
if (__DEV__) extendOptions.onWarn = warn
|
|
|
|
|
|
// TODO: SSR
|
|
|
// if (__SSR__) {}
|
|
|
|
|
|
- const instance =
|
|
|
- getCurrentScope() === currentInstance?.scope ? currentInstance : null
|
|
|
+ const instance = getCurrentInstance()
|
|
|
|
|
|
extendOptions.onError = (err: unknown, type: BaseWatchErrorCodes) =>
|
|
|
handleErrorWithInstance(err, instance, type)
|
|
|
@@ -78,8 +92,10 @@ const createMiddleware =
|
|
|
instance.isUpdating = true
|
|
|
}
|
|
|
|
|
|
+ const reset = setCurrentInstance(instance)
|
|
|
// run callback
|
|
|
value = next()
|
|
|
+ reset()
|
|
|
|
|
|
if (isFirstEffect) {
|
|
|
queuePostRenderEffect(() => {
|