@@ -460,29 +460,82 @@ export default class Order extends Aggregate<Props> {
460460
461461```
462462
463- How to use events
463+ #### How to use events
464+
465+ Event Handler
466+
467+ ``` ts
468+
469+ import { Context , EventHandler } from ' rich-domain' ;
470+
471+
472+ class OrderCreatedEvent extends EventHandler <Order > {
473+
474+ constructor () {
475+ super ({ eventName: ' OrderCreated' });
476+ }
477+
478+ dispatch(order : Order ): void {
479+ // dispatch event to another context
480+ order .context ().dispatchEvent (' Context:Event' , order .toObject ());
481+ };
482+ }
483+
484+ ```
485+
486+ Aggregates domain events
487+
464488
465489``` ts
466490
467- order .addEvent (' OTHER_EVENT ' , (... args ) => {
491+ order .addEvent (' Event ' , (... args ) => {
468492 console .log (args );
469493});
470494
471495// Or add an EventHandler instance
472- order .addEvent (new GenerateInvoiceEvent ());
496+ order .addEvent (new OrderCreatedEvent ());
473497
474- order .dispatchEvent (' ORDER_HAS_BEGUN ' );
498+ order .dispatchEvent (' OrderBegun ' );
475499
476500// dispatch with args
477- order .dispatchEvent (' OTHER_EVENT ' , { info: ' custom_args' });
501+ order .dispatchEvent (' Event ' , { info: ' custom_args' });
478502
479503// OR call all added events
480504await order .dispatchAll ();
481505
482506
483507```
484508
485- ---
509+ #### How to subscribe to a global event
510+
511+ ``` ts
512+
513+ import { Context } from ' rich-domain' ;
514+
515+ const context = Context .events ();
516+
517+ context .subscribe (' Context:Event' , (event ) => {
518+ const [model] = event .detail ;
519+ console .log (model );
520+ });
521+
522+ // dispatch an event to a context with args
523+ context .dispatchEvent (' Context:Event' , { name: ' Jane' });
524+
525+ // Dispatching events to specific contexts
526+ // Dispatches the SIGNUP event to Context-X
527+ context .dispatchEvent (' Context-X:Signup' );
528+
529+ // Dispatches the SIGNUP event to all contexts
530+ context .dispatchEvent (' *:Signup' );
531+
532+ // Dispatches all events to all contexts. Not recommended
533+ context .dispatchEvent (' *:*' );
534+
535+ // Dispatches all events under Context-Y
536+ context .dispatchEvent (' Context-Y:*' );
537+
538+ ```
486539
487540## Lib Full Documentation
488541
0 commit comments