exec Act
exec
In order to start the act, you must call the exec method of the ActMaster instance.
The exec method always returns Promise. The first argument is the name field property of the act you want to call. Then all the arguments the act needs to execute are passed.
For the example, we have an act of the following form:
// login.act.ts
import { ActMasterAction } from 'act-master';
import { api } from '.../path/to/api';
export class Login implements ActMasterAction {
name = 'Login';
async exec(loginData: any): Promise<any> {
return await api.login(loginData);
}
}Let's call him:
import { act } from 'act-master';
const loginData = { /* ... */ };
const result = await act().exec('Login', loginData);import { ActMaster } from 'act-master';
const loginData = { /* ... */ };
const $act = ActMaster.getInstance();
const result = await $act.exec('Login', loginData);<script>
export default {
// ...
methods: {
async login() {
const result = await this.$act.exec('Login', this.loginData);
}
}
}
</script>subscribe/unsubscribe on/off
In order to get the result of the exec method, you can subscribe to the desired act.
Then every time in successful execution, you will get the result in the callback.
The
subscribeandonmethods are synonymous, as areunsubscribeandoff.
act().subscribe('Login', (loginData: LoginData) => {
// ...
});act().on('Login', (loginData: LoginData) => {
// ...
});If you subscribe to an act, you will need to unsubscribe if you no longer need it. For example when the component that made it is removed. Otherwise you might have problems with memory leaks.
There are several ways to unsubscribe:
Unsubscribe with the function subscribe returns
const unsubscribe = act().subscribe('Login', loginCallback);
unsubscribe();const off = act().on('Login', loginCallback);
off();Unsubscribe with method
act().subscribe('Login', loginCallback);
act().unsubscribe('Login', loginCallback);act().on('Login', loginCallback);
act().off('Login', loginCallback);once method
If you need only one execution result and immediately unsubscribe, you can use the once method
act().once('Login', loginCallback);ADVANCED: Unsubscribe with subsList
You can mark subscriptions with a key and then delete them all together.
const SUBSCRIBE_KEY = '...';
act().subscribe('Login', loginCallback, SUBSCRIBE_KEY);
act().subscribe('Login2', loginCallback, SUBSCRIBE_KEY);
// ...
// unsubscribe
act().subsList.clear(SUBSCRIBE_KEY);const SUBSCRIBE_KEY = '...';
act().on('Login', loginCallback, SUBSCRIBE_KEY);
act().on('Login2', loginCallback, SUBSCRIBE_KEY);
// ...
// unsubscribe
act().subsList.clear(SUBSCRIBE_KEY);