#Troubles with multiple instances of a machine and context

1 messages · Page 1 of 1 (latest)

odd berry
#

I'm having a state machine that places data into it context. I have multiple actors of this machine.

I have read that I should use the assign function to update internal states or i will get unexpected behavior.

However the contexts is still mixed between the actors. (This happens silently, so something you should really be aware of).

I don't want to use inline assignments as multiple states call the same action.

I've tried to assign an returned object with each property. (see follow up)

I've also tried to use multiple actions and update a single context property...

This seems to be a syntax issue. Could some one point out 'the way' to me?

#
actions: {
    updateTemperature: assign(({ context, event }: { context: TemperatureContext; event: TemperatureEvent }) => {
      return {
        temperature: (() => {
          console.log('temperature', event['value']);
          return event['value'];
        })(),
        recentTemperatures: (() => {
          console.log('called');
          const recentTemperatures = context.recentTemperatures;

          recentTemperatures.push(event['value']);

          if (recentTemperatures.length > context.smoothingFactor) {
            recentTemperatures.shift();
          }
          console.log('recentTemperatures', recentTemperatures);

          return recentTemperatures;
        })(),
        updateAveragedTemperature: (() => {
          const averagedTemperature = context.recentTemperatures.reduce((acc, temp) => acc + temp, 0) / context.recentTemperatures.length;
          console.log('averagedTemperature', averagedTemperature);
          return averagedTemperature;
        })(),
        entryTimestamp: (() => {
          const entryTimestamp =
            context.averagedTemperature >= context.lowerThreshold && context.averagedTemperature <= context.upperThreshold
              ? (context.entryTimestamp ?? Date.now())
              : null;
          console.log('entryTimestamp', entryTimestamp);
          return entryTimestamp;
        })(),
      };
    }),
  },

These are being called, but as I can tell by the log the actors context are mixed...

recentTemperatures (5) [29.76, 220.18, 29.59, 220.2, 29.8]

(I know only one device is turned on with temp ~220), so it should look something like

recentTemperatures (5) [220.10, 220.18, 220.31, 220.21, 220.14]