Devessier / xstate-temporal-continue-as-new

Use continueAsNew with XState

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Continue as New + XState

This example is adapted from Temporal's official sample for continueAsNew API used with TypeScript SDK. See the official documentation: https://github.com/temporalio/samples-typescript/tree/main/continue-as-new.

To use XState with long-running workflows, we need to be able to call continueAsNew regularly. But an error is thrown when we continueAsNew while a timer was active in the machine.

Temporal throwing a Fatal("Missing associated machine for Timer(0)") error

It seems the issue is that when the machine will be rehydrated, an action will be triggered to cancel the last timer. This action comes the last state of the machine, just before continueAsNew is called. When rehydrating, clearTimeout will be called with an undefined timeout id, leading to the aforementioned error.

An action to cancel the last timer will be called when rehydrating

I figured out that a solution is to don't call clearTimeout when the timeout id is empty. Seems hacky, but prevents a fatal error from being thrown.

Do not call clearTimeout when timeout id is undefined

How to test?

You need to have a Temporal server running.

Start a worker:

npm run start.watch

Start a workflow and continueAsNew after 8 seconds:

npm run workflow

About

Use continueAsNew with XState


Languages

Language:TypeScript 75.0%Language:JavaScript 25.0%