@composi/observer:
Unwatch
Unwatch an Event
There may be situations where you need an observer, but only temporarily. You can use the observer's unwatch
method to stop watchers from reacting to a particular event. This takes just one argument: the event to stop watching. When you turn off an event with unwatch
, this affects all watchers for that event.
import { Observer } from '@composi/observer'
const observer = new Observer()
observer.watch('whatever', () => {
alert('Whatever just happened!')
})
// Sometime later send the event:
observer.send('whatever')
// alert: 'Whatever just happened!'
// Sometime later unwatch the event:
observer.unwatch('whatever')
// Sending the event now does nothing:
observer.send('whatever')
If you need to revive an unwatched event, you can do so by initalizing a watcher for it later.