4lessandrodev / rich-domain

A lib to help you create a robust project based on domain driven-design (ddd) principles with typescript and zero dependencies.
https://www.npmjs.com/package/rich-domain
MIT License
122 stars 5 forks source link

Change the way to dispatch domain event #120

Closed 4lessandrodev closed 7 months ago

4lessandrodev commented 7 months ago

Is your feature request related to a problem? Please describe.

Currently, triggering an event using the available feature requires the following syntax:


const handler = new UseCase();
aggregate.dispatch(eventName, handler);

This syntax deviates from the native JavaScript event handling pattern, which can be confusing and less intuitive for developers.

Describe the solution you'd like

I would like to propose changing the event handling syntax to align with the native JavaScript event handling pattern. The proposed syntax would be as follows:


aggregate.addEvent(eventName, (args) => {}, options);
aggregate.dispatchEvent(eventName);
aggregate.dispatchEvents();

This change would make the event handling mechanism more familiar and consistent with the standard JavaScript practice, improving readability and developer experience.

Describe alternatives you've considered

No alternative solutions have been considered at this time.

Additional context

No additional context is currently available.

4lessandrodev commented 7 months ago

[1.20.0] - 2024-03-17

Changed

Details: Introduced a new method to create an event management instance for the aggregate. Improved event handling and organization, enhancing the overall performance and efficiency of event management. Enabled easier integration and usage of event management features in various applications.


import { TsEvents } from 'rich-domain';

/**
 * Create a new instance of the event management class
 * Pass the aggregate as a parameter
 * Return an event management instance for the aggregate
 */
const events = new TsEvents(aggregate);

events.addEvent('eventName', (...args) => {
    console.log(args);
});

// dispatch all events
await events.dispatchEvents();

// OR dispatch a specific event
events.dispatchEvent('eventName');

Implemented a new event handling mechanism using the Handler class. Example:


// implement extending to EventHandler
class Handler extends EventHandler<Product> {

    constructor() { 
      super({ eventName: 'sample' });
    };

    dispatch(product: Product, params: [DEvent<Product>, any[]]): void | Promise<void> {
        const model = product.toObject();
        const [event, args] = params;

        console.log(model);
        console.log(event);

        // custom params provided on call dispatchEvent
        console.log(args);
    }
}

const event = new Handler();

aggregate.addEvent(event);

// or 

aggregate.addEvent('sample', (...args) => {
    console.log(args);
})

aggregate.dispatchEvent('sample', { custom: 'params' });

Bug Fixes

Fixed an issue with the event dispatching mechanism. Notes This version introduces significant changes to the event handling system, enhancing the flexibility and usability of the Aggregate class.