UseCase LifeCycle
Almin has life-cycle events. These events are useful for logging like almin-logger.
For more information about logging, see Logging tips.
LifeCycle events
| Event | When |
|---|---|
| onBeginTransaction | A transaction begin |
| onEndTransaction | A transaction end |
| onWillNotExecuteEachUseCase | A UseCase will not Execute |
| onWillExecuteEachUseCase | Each UseCase will Execute |
| onDispatch @1 | UseCase calls this.dispatch(payload) |
| onErrorDispatch @1 | UseCase calls this.throwError(new Error()) |
| onDidExecuteEachUseCase | Each UseCase did executed |
| onCompleteEachUseCase | Each UseCase is completed |
@1 A single UseCase can call multiple.
For more details, see LifeCycleEventHub · API Reference.
What the difference between onDidExecuteEachUseCase and onCompleteEachUseCase?
Some UseCase's task is async. The difference is came up at the async case.
For example, We can write AsyncUseCase like following:
import { UseCase } from "almin";
export class AsyncUseCase extends UseCase {
execute() {
this.dispatch({ type: "start" });
return Promise.resolve().then(() => {
// does async function
});
}
}
The LifeCycle events of AsyncUseCase:
- Sync call
onWillExecuteEachUseCase - Sync call
onDispatch - Sync call
onDidExecuteEachUseCase - Async call
onCompleteEachUseCase
The following is to illustrate the lifecycle in the code.
// IMAGE CODE!!!
import {UseCase} from "almin";
export class AsyncUseCase extends UseCase {
// 1. call onWillExecuteEachUseCase
execute() {
// 2. call onDispatch
this.dispatch({ type : "start" });
return Promise.resolve().then(() => {
// does async function
}).then(() => {
// 4. call onCompleteEachUseCase
});
}
// 3. call onDidExecuteEachUseCase
}
// listen on*
context.events.onWillExecuteEachUseCase((payload, meta) => {});
context.events.onDispatch((payload, meta) => {});
context.events.onDidExecuteEachUseCase((payload, meta) => {});
context.events.onCompleteEachUseCase((payload, meta) => {});
onCompleteEachUseCase is always called after the onDidExecuteEachUseCase.