( source: WatchSource | WatchSource[] | WatchEffect | object, cb: WatchCallback | null, options: WatchOptions = EMPTY_OBJ, )
| 144 | } |
| 145 | |
| 146 | function doWatch( |
| 147 | source: WatchSource | WatchSource[] | WatchEffect | object, |
| 148 | cb: WatchCallback | null, |
| 149 | options: WatchOptions = EMPTY_OBJ, |
| 150 | ): WatchHandle { |
| 151 | const { immediate, deep, flush, once } = options |
| 152 | |
| 153 | if (__DEV__ && !cb) { |
| 154 | if (immediate !== undefined) { |
| 155 | warn( |
| 156 | `watch() "immediate" option is only respected when using the ` + |
| 157 | `watch(source, callback, options?) signature.`, |
| 158 | ) |
| 159 | } |
| 160 | if (deep !== undefined) { |
| 161 | warn( |
| 162 | `watch() "deep" option is only respected when using the ` + |
| 163 | `watch(source, callback, options?) signature.`, |
| 164 | ) |
| 165 | } |
| 166 | if (once !== undefined) { |
| 167 | warn( |
| 168 | `watch() "once" option is only respected when using the ` + |
| 169 | `watch(source, callback, options?) signature.`, |
| 170 | ) |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | const baseWatchOptions: BaseWatchOptions = extend({}, options) |
| 175 | |
| 176 | if (__DEV__) baseWatchOptions.onWarn = warn |
| 177 | |
| 178 | // immediate watcher or watchEffect |
| 179 | const runsImmediately = (cb && immediate) || (!cb && flush !== 'post') |
| 180 | let ssrCleanup: (() => void)[] | undefined |
| 181 | if (__SSR__ && isInSSRComponentSetup) { |
| 182 | if (flush === 'sync') { |
| 183 | const ctx = useSSRContext()! |
| 184 | ssrCleanup = ctx.__watcherHandles || (ctx.__watcherHandles = []) |
| 185 | } else if (!runsImmediately) { |
| 186 | const watchStopHandle = () => {} |
| 187 | watchStopHandle.stop = NOOP |
| 188 | watchStopHandle.resume = NOOP |
| 189 | watchStopHandle.pause = NOOP |
| 190 | return watchStopHandle |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | const instance = currentInstance |
| 195 | baseWatchOptions.call = (fn, type, args) => |
| 196 | callWithAsyncErrorHandling(fn, instance, type, args) |
| 197 | |
| 198 | // scheduler |
| 199 | let isPre = false |
| 200 | if (flush === 'post') { |
| 201 | baseWatchOptions.scheduler = job => { |
| 202 | queuePostRenderEffect(job, instance && instance.suspense) |
| 203 | } |
no test coverage detected