From 1a569665b3c3fc23b97a50ee3d7441242482394e Mon Sep 17 00:00:00 2001 From: skirtle <65301168+skirtles-code@users.noreply.github.com> Date: Sat, 7 Jan 2023 07:34:12 +0000 Subject: [PATCH] Add more API examples for `watch()` --- src/api/reactivity-core.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/api/reactivity-core.md b/src/api/reactivity-core.md index c3078ae180..e2e2910c4d 100644 --- a/src/api/reactivity-core.md +++ b/src/api/reactivity-core.md @@ -453,10 +453,34 @@ Watches one or more reactive data sources and invokes a callback function when t flush: 'post', onTrack(e) { debugger + }, + onTrigger(e) { + debugger } }) ``` + Stopping the watcher: + + ```js + const stop = watch(source, callback) + + // when the watcher is no longer needed: + stop() + ``` + + Side effect cleanup: + + ```js + watch(id, async (newId, oldId, onCleanup) => { + const { response, cancel } = doAsyncWork(newId) + // `cancel` will be called if `id` changes, cancelling + // the previous request if it hasn't completed yet + onCleanup(cancel) + data.value = await response + }) + ``` + - **See also**: - [Guide - Watchers](/guide/essentials/watchers.html)