Skip to content

Commit 975ae1d

Browse files
committed
feat: introduce WatchEffectOptions#signal
1 parent 31b4521 commit 975ae1d

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/api/options-state.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ Declare watch callbacks to be invoked on data change.
275275

276276
type ObjectWatchOptionItem = {
277277
handler: WatchCallback | string
278+
signal?: AbortSignal
278279
immediate?: boolean // default: false
279280
deep?: boolean // default: false
280281
flush?: 'pre' | 'post' | 'sync' // default: 'pre'
@@ -293,6 +294,7 @@ Declare watch callbacks to be invoked on data change.
293294
294295
The value can also be a string of a method name (declared via `methods`), or an object that contains additional options. When using the object syntax, the callback should be declared under the `handler` field. Additional options include:
295296
297+
- **`signal`**: stop the watcher when the given [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) is aborted.
296298
- **`immediate`**: trigger the callback immediately on watcher creation. Old value will be `undefined` on the first call.
297299
- **`deep`**: force deep traversal of the source if it is an object or an array, so that the callback fires on deep mutations. See [Deep Watchers](/guide/essentials/watchers#deep-watchers).
298300
- **`flush`**: adjust the callback's flush timing. See [Callback Flush Timing](/guide/essentials/watchers#callback-flush-timing) and [`watchEffect()`](/api/reactivity-core#watcheffect).

src/api/reactivity-core.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ Runs a function immediately while reactively tracking its dependencies and re-ru
243243
type OnCleanup = (cleanupFn: () => void) => void
244244

245245
interface WatchEffectOptions {
246+
signal?: AbortSignal
246247
flush?: 'pre' | 'post' | 'sync' // default: 'pre'
247248
onTrack?: (event: DebuggerEvent) => void
248249
onTrigger?: (event: DebuggerEvent) => void
@@ -328,6 +329,18 @@ Runs a function immediately while reactively tracking its dependencies and re-ru
328329
})
329330
```
330331
332+
Stopping the watchers with `AbortController`: <sup class="vt-badge" data-text="3.6+" />
333+
334+
```js
335+
const controller = new AbortController()
336+
337+
watchEffect(() => {}, { signal: controller.signal })
338+
watchEffect(() => {}, { signal: controller.signal })
339+
340+
// when the watchers are no longer needed:
341+
controller.abort()
342+
```
343+
331344
Options:
332345
333346
```js

0 commit comments

Comments
 (0)