@composi/observer:
Send
An observing watcher is useless if it never receives an event. To send and watch an event you need to use the observer watch
method. This takes two arguments: the event to send and some optional data to send along with the event.
Send With Data
import { Observer } from '@composi/observer'
const observer = new Observer()
observer.watch('whatever', data => {
console.log('Whatever just happened.')
console.log(`Received this data: ${data})
})
// Sometime later:
observer.send('whatever', 'This is whatever')
Send without Data
You can send an event without data. You might do so because the watcher isn't expecting data. Or you've set up the watcher to handle different situations depending on if data is sent or not.
import { Observer } from '@composi/observer'
const observer = new Observer()
observer.watch('whatever', data => {
console.log('Whatever just happened.')
if (data) {
console.log(`Received this data: ${data})
}
})
// Sometime later:
observer.send('whatever')
npm install --save-dev @composi/core