UseCase LifeCycle

LifeCycle events

Event When
onWillExecuteEachUseCase Each UseCase will Execute
onDispatch @1 UseCase call this.dispatch(payload)
onError @1 UseCase call this.throwError(new Error())
onDidExecuteEachUseCase Each UseCase did executed.
onCompleteEachUseCase Each UseCase is completed.

@1 A single UseCase can call multiple.

See API Reference for details of events.

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:

"use strict";
import {UseCase} from "almin";
export default class AsyncUseCase extends UseCase {
    execute() {
        this.dispatch({ type : "start" });
        return Promise.resolve().then(() => {
            // does async function
        });
    }
}

The LifeCycle events of AsyncUseCase:

  1. Sync call onWillExecuteEachUseCase
  2. Sync call onDispatch
  3. Sync call onDidExecuteEachUseCase
  4. Async call onCompleteEachUseCase

Illustrate the lifecycle in the code.

// IMAGE CODE!!!
import {UseCase} from "almin";
export default 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.onWillExecuteEachUseCase((payload, meta) => {});
context.onDispatch((payload, meta) => {});
context.onDidExecuteEachUseCase((payload, meta) => {});
context.onCompleteEachUseCase((payload, meta) => {});

Always onCompleteEachUseCase is called after the onDidExecuteEachUseCase.

results matching ""

    No results matching ""