Skip to content

Restoring state docs clarification #467

@seadeep42

Description

@seadeep42

Hi,

I'm trying to use a state machine in NodeJS.
I init the state machine. On server up, I'd like to hydrate the actor with existing data.
There's a typescript error and a runtime error if I override it.
The snapshot below is the same as getPersistedSnapshot. Is there a change in the restoring code that differs from documentation?

const stateMachine = setup({
    types: {...},
}).createMachine({
    context: ({ input }) => (...),
    id: 'state-machine',
    initial: 'incomplete,
    on: {...},
    states: {...}
});

const actor = createActor(stateMachine, {
    snapshot: {
        status: 'active',
        value: 'progress',
        context: {...},
    },
});

Typescript error:

Object literal may only specify known properties, and 'value' does not exist in type '{ status: "active"; output: undefined; error: undefined; }'.

Runtime error:

TypeError: Cannot convert undefined or null to object
    at Function.keys (<anonymous>)
    at StateMachine.restoreSnapshot (/server/node_modules/xstate/dist/xstate.cjs.js:520:12)
    at Actor._initState (/server/node_modules/xstate/dist/raise-5872b9e8.cjs.js:599:81)
    at new Actor (/server/node_modules/xstate/dist/raise-5872b9e8.cjs.js:592:10)
    at createActor (/server/node_modules/xstate/dist/raise-5872b9e8.cjs.js:1085:10)
    at setStateMachineStateForPerson (/server/src/oncocare/oncocare.service.ts:506:58)
    at OncocareService.auditStateMachine (/server/src/oncocare/oncocare.service.ts:115:28)
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
    at async OncocareService.onApplicationBootstrap (/server/src/oncocare/oncocare.service.ts:86:9)
    at async Promise.all (index 0)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions