Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Context Communication #134 #139

Merged
merged 18 commits into from
Apr 12, 2024
Merged

Added Context Communication #134 #139

merged 18 commits into from
Apr 12, 2024

Conversation

4lessandrodev
Copy link
Owner

@4lessandrodev 4lessandrodev commented Apr 12, 2024

[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

  • 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


@4lessandrodev 4lessandrodev merged commit 2a43a8a into main Apr 12, 2024
1 check passed
@mmmoli
Copy link
Contributor

mmmoli commented Apr 12, 2024

Been thinking about the chain reaction problem.

You can always not await the promise. Or, use something like upstash.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants