This package simplifies dispatching process, you shouldn't care about Store service injection as we provide more declarative way to dispatch events out of the box.
📦 Install
To install @ngxs-labs/dispatch-decorator run the following command:
npminstall@ngxs-labs/dispatch-decorator--save# or if you use yarnyarnadd@ngxs-labs/dispatch-decorator
🔨 Usage
Import the module into your root application module:
@Dispatch() is a function that allows you to decorate methods and properties of your components, basically arrow functions are properties. Firstly you have to create a state:
Also, your dispatchers can be asynchronous, they can return Promise or Observable, asynchronous operations are handled outside Angular's zone, thus it doesn't affect performance:
exportclassAppComponent {// `ApiService` is defined somewhereconstructor(private api:ApiService) {} @Dispatch()publicasyncsetAppSchema():Promise<SetAppSchema> {const { version,shouldUseGraphQL } =awaitthis.api.getInformation();const { schema } =awaitthis.api.getSchemaForVersion(version);returnnewSetAppSchema(schema); }// OR @Dispatch()publicsetAppInformation= () =>this.api.getInformation().pipe(switchMap(({ version }) =>this.api.getSchemaForVersion(version)),map(({ schema }) =>newSetAppSchema(schema)) );}
Notice that it doesn't matter if you use an arrow function or a normal class method.
Dispatching multiple events
Your dispatchers can also return arrays with events inside: