Actions
ACTIONS is a main way to connect all your application parts (modules and plugins) together. It works like Event Emitter: you can trigger an event to any module/plugin which can listen. And subscribe every module/plugin to every event from other module/plugin is anywhere in an application. Every ACTIONS response need to return Promise or if you don't need to return any information - you can return nothing.
Imagine that we have two modules: user and product. User wanna get information from the product.
First of all, we need to subscribe in product module to "get information" event and send some info to response.
ACTIONS.on('product.getInfo', () => {
return Promise.resolve({ name: 'potato', weight: 300 });
});
Next step is send event to product module from user module for getting response.
ACTIONS.send('product.getInfo').then((info) => console.log(info));
If you want to send some parameters in event - you can add them as second attribute.
// product module
ACTIONS.on('product.set', (data) => {
const newProduct = data;
return Promise.resolve(newProduct);
});
// user module
ACTIONS.send('product.set', { name: 'tomato' }).then((info) => console.log(info));