The type of the MediatorContext.
The type of the event names.
The immutable context snapshot (same for all middlewares).
The input data containing pending changes.
The event name being processed.
Either undefined (pass through), modified input data, or a cancel event.
// Logger middleware (observes only)
const logger: MediatorMiddleware<MyContext, string> = (context, input, event) => {
console.log(`Event ${event} triggered`)
// No return - passes through unchanged
}
// Validation middleware (can cancel)
const validator: MediatorMiddleware<MyContext, string> = (context, input, event) => {
if (input.pendingChanges && 'count' in input.pendingChanges && input.pendingChanges.count < 0) {
return { cancel: true } // Stop processing
}
return input // Pass through
}
// Transform middleware (modifies data)
const transformer: MediatorMiddleware<MyContext, string> = (context, input, event) => {
return {
pendingChanges: {
...(input.pendingChanges ?? {}),
timestamp: Date.now()
}
}
}
Represents a middleware function for Mediator events. Middlewares execute before event listeners and can observe, transform, or cancel events.