Difference between reading serialized state #1695
-
Hi, im storing state in the db and planned to read it like this: const userState = await Users.findOneById(userId).state; // string!
const service = interpret(createMachine({ user: this.user }));
service.start(userState); Now i noticed that there is an example how to read in from localStorage, here i just changed it to db: const machine = interpret(createMachine{( user: this.user }));
// storing
machine.onTransition((state, event) => await Users.findOneAndUpdate({ id: this.user.id }, {$set: state}))
// reading:
const stateDefinition = JSON.parse(await Users.findOneById(this.user.id).state) || machine.initialState
const previousState = State.create(stateDefinition);
const resolvedState = myMachine.resolveState(previousState);
const service = interpret(myMachine).start(resolvedState); What is the difference between these approaches? Both will work? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Inspect both The |
Beta Was this translation helpful? Give feedback.
Inspect both
state
objects; that's where the difference will be (if any). Choose the one that suits your needs.The
service.start(someInitialState)
call will internally resolvesomeInitialState
fromthis.machine.resolveState(State.from(someInitialState))
.