feat: adds middleware support for event interception#28
Merged
Conversation
0d126c5 to
5d90b17
Compare
* include vitest config file
5d90b17 to
277c782
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Middleware System Implementation
Overview
This PR introduces a middleware system to the Mediator library. Middlewares are configured as part of the mediator creation step, reinforcing their role as part of the mediator’s infrastructure. Middlewares provide a dedicated layer that can observe events, modify the context or pending changes, and even cancel propagation. This ensures more control, observability, and separation of concerns while keeping the API minimalistic.
Features
🎯 Core Functionality
*.🔧 API Design
Middlewares are declared through the
optionsparameter increateMediator:Middleware Data Types
Usage Examples
Creating a Mediator with Middlewares
Execution Flow
mediator.send(event, modifier)is called.A frozen snapshot of the current context is created (
Object.freeze(structuredClone(context))).Modifier generates initial
pendingChangesif provided.Middlewares execute in registration order:
pendingChanges.{ cancel: true }to stop propagation immediately.Final context is updated with shallow merge (
Object.assign) of allpendingChanges.Event listeners run with the updated context unless propagation was cancelled.
Wildcard listeners (
*) also execute after specific listeners.Benefits
🚀 Clarity & Simplicity
🛡️ Strong Guarantees
🔍 Observability
Advanced Examples
Middleware Return Types
Accumulating Pending Changes
Wildcard vs Specific Events
Key Design Decisions
🔒 Immutable Context
Object.freeze(structuredClone(context))ensures true immutability🔄 Pending Changes Accumulation
pendingChangesonlyObject.assignfor shallow merging🎯 Simple API Design
(context, input, event)- clear separation of concernsFuture Enhancements
This middleware system proposes a powerful yet minimalistic way to control event flow in Mediator. Middlewares can watch, transform, or cancel events, providing security, observability, and flexibility while maintaining the library's lightweight philosophy.