ng-state
  • Introduction
  • Main differences
  • Getting Started
    • Instalation
    • Examples
  • Core concepts
    • Main idea
      • More complex flow visualization
    • Configuration
      • Immer Setup
      • ImmutableJs Setup
    • Store
      • Operators
      • Optimistic updates plugin
      • Optimistic updates plugin
      • Form manager plugin
        • onChange hook
        • shouldUpdateState hook
        • Custom form elements
      • Persist state plugin
        • Configuring custom storage
    • Actions
      • Immer
      • ImmutaleJs
      • Injectable Actions
    • Components with Actions
      • Signal Actions
      • State Actions
    • @InjectStore decorator
    • @WithStore decorator
    • @ComponentState decorator
    • Dispatcher
  • Different scenarios
    • Passing list item index via router
    • ngOnChanges hook
    • FormManager pitfalls
  • Unit testing
    • Setup
    • Test store
    • Test actions
    • Test component with actions
    • Test with Angular TestBed
  • Debugging
    • Setup
    • Redux DevTools
    • Automated changes output
    • Manual state changes check
    • Additional debugging information
  • Production
    • Production mode
    • Server Side Rendering (SSR)
  • Other information
    • Best practices
    • CLI
      • Custom Configurations
    • Performance
    • Blog Posts
    • Contributing
Powered by GitBook
On this page
  • Publish message
  • Receive message

Was this helpful?

  1. Core concepts

Dispatcher

There are cases when states of not related components, which has different places in state tree, should change e.g: when list item is selected filter should collapse. This is where dispatcher kicks in. Dispatcher is design to send and receive messages between components.

First create a message

export class UpdateMessage extends Message {
  constructor(payload?: any) {
    super('MessageName', payload);
  }
}

Publish message

and publish message from B component

dispatcher.publish(new UpdateMessage('payload'));
// or
dispatcher.publish('UPDATE_MESSAGE', 'payload');

Receive message

Then in A component subscribe to dispatcher like this:

dispatcher.subscribe(UpdateMessage as any, (payload: any) => {
    this.actions.update....
});

// or

dispatcher.subscribe('UPDATE_MESSAGE', (payload: any) => {
  this.actions.update....
});

or you can listen for messages in A component like this:

dispatcher.listenTo<OptionalPayloadType>(UpdateMessage as any)
    .pipe(takeUntil(this.unsubscriber))
    .subscribe((payload: any) => {
        ...
    });

// or 

dispatcher.listenTo<OptionalPayloadType>('UPDATE_MESSAGE')
    .pipe(takeUntil(this.unsubscriber))
    .subscribe((payload: any) => {
        ...
    });
Previous@ComponentState decoratorNextPassing list item index via router

Last updated 5 years ago

Was this helpful?