title: Self-resetting circular flow in wolkenkit
body: <p>I have a question about stateful flows, I'll try to break it down to a really simple example:
  I am using different states, lets say pristine, add, and calculate.
  My state consists of an int that I want to add to, and after I added to it twice, I want to calculate the doubled amount, then set it to pristine again.
  So now I'll start off with a pristine flow, int = 0. Inside my transitions I react to a <code>addNumber</code> event, take an int out of the event data and add it to my state, then I <code>transitionTo('add')</code>.
  I repeat this, add another number and <code>transitionTo('calculate')</code>.
  Inside my reactions I now react to the transition between add and calculating and set the new State to double the amount of the int.
  What i want to do now is work with that number, for example send a command containing it, and then reset the state and <code>transitionTo('pristine')</code>.
  The problem is: I can't set the state neither can I transition.
  How do you tackle this?</p>
  
  <p>Here you have the actual code so that it becomes a bit more understandable:</p>
  
  <pre><code>'use strict';
  
  const identity = {
          'usermgmt.user.presentSwitched': event =&gt; event.user.id,
      'usermgmt.user.pauseSwitched': event =&gt; event.user.id
  };
  
  const initialState = {
      is: 'pristine',
      present: false,
      pause: false,
      presentSince: null,
      presentUntil: null,
      pauses: [],
  };
  
  const transitions = {
      pristine: {
          'usermgmt.user.presentSwitched' (flow, event) {
              flow.setState({
                  present: true,
                  presentSince: event.data.timestamp
              });
              flow.transitionTo('present');
          }
      },
      present: {
          'usermgmt.user.presentSwitched' (flow, event) {
              flow.setState({
                  present: false,
                  presentUntil: event.data.timestamp
              });
              flow.transitionTo('calculating');
          },
          'usermgmt.user.pauseSwitched' (flow, event) {
              const newPause = {pauseSince: event.data.timestamp};
              flow.setState({
                  pause: true,
                  pauses: [...flow.state.pauses, newPause]
          });
              flow.transitionTo('pause');
          }
      },
      pause: {
          'usermgmt.user.pauseSwitched' (flow, event) {
              const pauses = Object.create(flow.state.pauses);
              pauses[flow.state.pauses.length - 1].pauseUntil = event.data.timestamp;
              flow.setState({
                  pause: false,
                  pauseSince: event.data.timestamp,
                  pauses
              });
              flow.transitionTo('present');
          }
      }
  };
  
  const reactions = {
      present: {
          'calculating' (flow, event, services) {
              const {app, logger} = services;
  
              const workDayId = 'kek';
              const from = flow.state.presentSince;
              const to = flow.state.presentUntil;
              const pauses = flow.state.pauses;
  
              logger.info(JSON.stringify(flow));
  
              app.keksing.recording().createRecording({workDayId, from, to, type: 'working'});
  
              flow.setState({
                  present: false,
                  pause: false,
                  presentSince: null,
                  presentUntil: null,
                  pauses: [],
              });
              flow.transitionTo('pristine');
          }
      }
  };
  
  module.exports = { identity, initialState, transitions, reactions };
  </code></pre>
  
  <p><a href="https://gist.github.com/DrFelder/122a72ffed3eb239a1a3ae33c99ea00d" rel="nofollow noreferrer">https://gist.github.com/DrFelder/122a72ffed3eb239a1a3ae33c99ea00d</a></p>