Skip to content

Spawning new machines with independant complex objects on context #1595

Answered by Andarist
Asderex asked this question in Q&A
Discussion options

You must be logged in to vote

You should use a closure for this - Machine creates a "template" for a machine (of sorts). You could compare this problem to a difference between classes and instances. Machine creates a "class" so if you have put something into it it's shared across all instances that are later instantiated from it.

Here is a working example:
https://codesandbox.io/s/testing-independant-objects-on-xstate-context-forked-jsef0?file=/src/index.js

However, it's much more preferred for a context to be serializable etc. I'm not sure how do you plan to use this but I would recommend you looking into actors and encapsulating your BehaviorSubject into one.

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by Asderex
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants