- Docs
- Remove watchers with unwatch()
Remove watchers with unwatch()
Removes watched callbacks for one field, a list of fields or the whole form, depending on the arguments you pass.
Removes watched callbacks for one field, a list of fields or the whole form, depending on the arguments you pass.
Signature
unwatch(): FormController
unwatch(field: string): FormController
unwatch(field: string, callback: FormFieldWatcher): FormController
unwatch(fields: string[]): FormController
unwatch(fields: string[], callback: FormFieldsWatcher): FormController
Parameters
| Overload | Description |
|---|---|
unwatch() |
Removes every watcher from every field. |
unwatch(field) |
Removes every watcher for the given field. |
unwatch(field, callback) |
Removes only the matching callback for the given field. |
unwatch(fields) |
Removes every watcher registered with that list of fields. |
unwatch(fields, callback) |
Removes only the matching callback registered with that list. |
Returns
The same FormController — chainable.
Behaviour
- Watchers added via
watchandobserveshare the same internal registry. - For exact-match removal (
field+callback), the same function reference must be passed. - For bulk removal (
fieldonly or no args), all matching watchers are removed. - A list matches watchers registered with the same set of names; order and duplicates do not matter.
unwatch(['a'])does not remove a watcher of['a', 'b']. - A single name only removes single-field watchers.
unwatch('price')leaveswatch(['quantity', 'price'], …)in place.unwatch()with no arguments removes both kinds.
Examples
Remove a specific watcher
import { form } from '@samline/forms'
const profile = form('profile-form')
const callback = (value: unknown) => console.log(value)
profile.watch('email', callback)
profile.unwatch('email', callback)
Remove all watchers for a field
profile.unwatch('email')
Remove a watcher of several fields
const updateTotal = (values: Record<string, unknown>) => console.log(values)
profile.watch(['quantity', 'price'], updateTotal)
profile.unwatch(['quantity', 'price'], updateTotal)
Remove every watcher
profile.unwatch()
Use observe’s unsubscribe
const stop = profile.observe('email', value => console.log(value))
stop() // Equivalent to profile.unwatch('email', theCallback)
Edge cases
unwatchdoes not affect subscribers added viasubscribe. Use the unsubscribe returned bysubscribe.- Watchers added with the same callback twice are deduplicated — calling
unwatchwith that callback removes only one entry. - Bulk removal is safe to call at any time — it does not throw if there are no watchers.