Added Context Communication: Implemented a feature for inter-context communication within the library, enabling seamless interaction between different contexts within a project.
Introduced Custom Events: Custom events can now be dispatched both globally and within specific aggregates, providing flexibility in event handling and propagation.
Compatibility with Browser and Node.js: Maintained compatibility between browser and server environments by utilizing CustomEvents in the DOM for browser compatibility and standard event handling for Node.js.
Aggregate Event Management: Aggregates retain event management methods, allowing for the encapsulation of business rules within specific contexts.
Known Issues
Investigating potential chain reactions of lambda functions in certain scenarios.
Future Considerations
Strong Typing: Consideration for enhancing typing support for event parameters to provide better developer guidance and error checking.
Further Investigation: Continue investigating potential implications of fanning-out from a single lambda function to ensure robustness in complex scenarios.
Usage Examples
import { Aggregate, Ok, Result, Context, EventHandler } from 'rich-domain';
// ------------------
// Some Context X
const contextX = Context.events();
// Listening global events
contextX.subscribe('SIGNUP', (arg) => {
console.log(arg);
});
// ------------------
// User Account as Context Y
type Props = { name: string };
class User extends Aggregate<Props>{
private constructor(props: Props) {
super(props);
}
public static signUp(name: string): User {
const user = new User({ name });
// add handler according to business rule
user.addEvent(new SignUpEvent());
return user;
}
public static create(props: Props): Result<User> {
return Ok(new User(props));
}
}
class SignUpEvent extends EventHandler<User> {
constructor() {
super({ eventName: 'USER_CREATED' })
}
dispatch(user: User): void {
// dispatch to global context event manager
user.context().dispatchEvent("SIGNUP", user.toObject());
};
}
const user = User.signUp('John Doe');
// dispatch to call handler
user.dispatchEvent('USER_CREATED');
Acknowledgments
thanks to @mmmoli for contributions and inspirations
[1.21.0] - 2024-04-11
Features Added #134
Added Context Communication: Implemented a feature for inter-context communication within the library, enabling seamless interaction between different contexts within a project.
Introduced Custom Events: Custom events can now be dispatched both globally and within specific aggregates, providing flexibility in event handling and propagation.
Compatibility with Browser and Node.js: Maintained compatibility between browser and server environments by utilizing CustomEvents in the DOM for browser compatibility and standard event handling for Node.js.
Aggregate Event Management: Aggregates retain event management methods, allowing for the encapsulation of business rules within specific contexts.
Known Issues
Future Considerations
Strong Typing: Consideration for enhancing typing support for event parameters to provide better developer guidance and error checking.
Further Investigation: Continue investigating potential implications of fanning-out from a single lambda function to ensure robustness in complex scenarios.
Usage Examples
Acknowledgments
thanks to @mmmoli for contributions and inspirations