MCPcopy
hub / github.com/vuejs/composition-api / createWatcher

Function createWatcher

src/apis/watch.ts:203–398  ·  view source on GitHub ↗
(
  vm: ComponentInstance,
  source: WatchSource | WatchSource[] | WatchEffect,
  cb: WatchCallback | null,
  options: WatchOptions
)

Source from the content-addressed store, hash-verified

201}
202
203function createWatcher(
204 vm: ComponentInstance,
205 source: WatchSource | WatchSource[] | WatchEffect,
206 cb: WatchCallback | null,
207 options: WatchOptions
208): () => void {
209 if (__DEV__ && !cb) {
210 if (options.immediate !== undefined) {
211 warn(
212 `watch() "immediate" option is only respected when using the ` +
213 `watch(source, callback, options?) signature.`
214 )
215 }
216 if (options.deep !== undefined) {
217 warn(
218 `watch() "deep" option is only respected when using the ` +
219 `watch(source, callback, options?) signature.`
220 )
221 }
222 }
223
224 const flushMode = options.flush
225 const isSync = flushMode === 'sync'
226 let cleanup: (() => void) | null
227 const registerCleanup: InvalidateCbRegistrator = (fn: () => void) => {
228 cleanup = () => {
229 try {
230 fn()
231 } catch (
232 // FIXME: remove any
233 error: any
234 ) {
235 logError(error, vm, 'onCleanup()')
236 }
237 }
238 }
239 // cleanup before running getter again
240 const runCleanup = () => {
241 if (cleanup) {
242 cleanup()
243 cleanup = null
244 }
245 }
246 const createScheduler = <T extends Function>(fn: T): T => {
247 if (
248 isSync ||
249 /* without a current active instance, ignore pre|post mode */ vm ===
250 fallbackVM
251 ) {
252 return fn
253 }
254 return ((...args: any[]) =>
255 queueFlushJob(
256 vm,
257 () => {
258 fn(...args)
259 },
260 flushMode as 'pre' | 'post'

Callers 2

watchEffectFunction · 0.85
watchFunction · 0.85

Calls 11

warnFunction · 0.90
isRefFunction · 0.90
isReactiveFunction · 0.90
isArrayFunction · 0.90
isFunctionFunction · 0.90
createVueWatcherFunction · 0.85
patchWatcherTeardownFunction · 0.85
createSchedulerFunction · 0.85
traverseFunction · 0.85
shiftCallbackFunction · 0.85
teardownMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…