Aggregating local events in SCXML

138 views Asked by At

My state machine has a self loop every time some request event is created. I want to store these events in a local context list against a key and everytime this self loop is executed an element is appended to the list. Then this list after a certain expiry period ,of say 1 Hour , is added to global context of SCXML. How can I achieve this?

Basically I want to aggregate the requests before I trigger a particular action.

  <state id="S02" label="REQUEST CREATED">
        <onentry>
            <action:trigger id="ACC1" name="EXPIRY_EVENT_expiry.sm00007" />
        </onentry>
        <transition event="expiry.sm00007" target="S03">
            <action:trigger id="ACC2" name="TO_DO_SOMETHING" />
            // add the local event list to global context
        </transition>
     <transition event=reqCreated" target="S02" >
      // keep adding the event to local context like appending to list
      </transition>
  </state>
1

There are 1 answers

0
jbeard4 On

In the SCXML spec, all datamodel variables are global so there's not really a "local" context. But you could use a key to index into a JavaScript object. Something like:

  <datamodel>
    <data id="globalEventList"/>
    <data id="localEventListMap" expr="{}"/>
    <data id="localKey" expr="'foo'"/>
  </datemodel>
  <state id="init">
    <onentry>
      <script>
        localEventListMap[localKey] = [];
      </script>
    </onentry>
    <transition target="S02"/>
  </state>
  <state id="S02" label="REQUEST CREATED">
        <onentry>
            <action:trigger id="ACC1" name="EXPIRY_EVENT_expiry.sm00007" />
        </onentry>
        <transition event="expiry.sm00007" target="S03">
            <action:trigger id="ACC2" name="TO_DO_SOMETHING" />
            <script>
              // add the local event list to global context
              globalEventList = localEventListMap[key];
            </script>
        </transition>
     <transition event="reqCreated" target="S02" >
        <script>
          // keep adding the event to local context like appending to list
          localEventListMap[key].push(_event);
        </script>
      </transition>
  </state>