The type of the MediatorContext.
The type of the event names.
Removes an event listener from the Mediator.
The event name or a wildcard event.
The listener function to be removed.
// Use .off to listen the event once
const myListener = (ctx: Readonly<MyContext>) => {
console.log('my listener')
myMediator.off('item:purchase', myListener)
}
myMediator.on('item:purchase', myListener)
Adds an event listener to the Mediator.
The event name or a wildcard event.
The listener function for the event.
// to listen to a specific event
myMediator.on('item:add', (ctx: Readonly<MyContext>) =>
console.log(`product added`)
)
// to listen to any event
myMediator.on('*', (ctx: Readonly<MyContext>, event: MyEvent) =>
console.log(`${event} modify context to`, ctx))
)
Sends an event through the Mediator, optionally modifying the context.
The specific event name.
Optional
modifier: MediatorContextModifier<Context>The optional context modifier function.
// Send a specific event with context modification.
myMediator.send('item:add', (ctx: Readonly<MyContext>) => ({
...ctx,
items: [...ctx.items, 123]
}))
// Send a specific event without context modification.
myMediator.send('item:purchase')
Generated using TypeDoc
Mediator
Description
Represents a Mediator instance with specific context and event names.