Skip to content

Messaging

Arc core provides a typed publish/subscribe messenger through @cratis/arc/messaging.

IMessenger publishes and subscribes by runtime type:

import { IMessenger } from '@cratis/arc/messaging';
class UserSelected {
constructor(readonly userId: string) {
}
}
messenger.subscribe(UserSelected, message => {
console.log(message.userId);
});
messenger.publish(new UserSelected('42'));

Messenger can be created with a parent messenger:

import { Messenger } from '@cratis/arc/messaging';
const root = new Messenger();
const child = new Messenger(root);

Default behavior:

  • Messages published in a child stay in that branch (no bubbling to parent).
  • Messages published in a parent trickle down to all descendants recursively.

Each messenger has two properties:

  • bubbleToParent (default false) — bubble published messages to parent.
  • trickleDownToChildren (default true) — forward messages to child messengers.