wolkenkit
Documentation
News
DocumentationReferenceCreating stateful flowsUsing services

Using services

To use services, add services as third parameter to your reaction.

E.g., if you want to use services when a flow transitions from the pristine state to the awaiting-payment state, use the following code:

const when = {
  pristine: {
    'awaiting-payment' (flow, event, services, mark) {
      // ...

      mark.asDone();
    }
  }  
};

Sending commands

Sometimes you may need to send commands to the application from a reaction. For that use the app service, access the context and the aggregate, and call the command function.

No callbacks here

In contrast to sending commands when building a client you can not use the failed, delivered, await, and timeout functions here.

E.g., if you want to send an awaitPayment command to the invoice that caused a transition from the pristine state to the awaiting-payment state, use the following code:

const when = {
  pristine: {
    'awaiting-payment' (flow, event, services, mark) {
      const app = services.get('app');

      app.accounting.invoice(event.aggregate.id).awaitPayment();

      mark.asDone();
    }
  }  
};

Writing log output

To write JSON-formatted log output use the logger service. Internally this service uses flaschenpost. For details on how to use flaschenpost see its documentation.

E.g., to log messages when a flow transitions from the pristine state to the awaiting-payment state, use the following code:

const when = {
  pristine: {
    'awaiting-payment' (flow, event, services, mark) {
      const logger = services.get('logger');

      logger.info('Transitioning from pristine to awaiting payment...');
      // ...

      mark.asDone();
    }
  }  
};