-
Hi, I was wondering if there was a way to save unique complex objects across multiple spawned actors of the same type. More specifically I am trying to save an rxjs BehaviourSubject to context (set as initial context) however, when I do it appears that the object reference is shared across all instances of spawned machines. i.e. Using as well as using a closure:
but both of these are throwing up new funky errors so I thought I'd just check whether this (perhaps unorthodox) approach is possible? At a higher level I am trying to allow two independant actors to subscribe to specific event streams on one another with the subscription managed on the subscriber side, not the publisher side... so if anyones got any ideas on a better way this could be achieved I would love to hear it. Minimum working example of my challenge can be seen here |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
You should use a closure for this - Here is a working example: 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. |
Beta Was this translation helpful? Give feedback.
-
Thanks for the help Mateusz. I will use the closure approach for now and look into actors to see if I can find a cleaner approach. |
Beta Was this translation helpful? Give feedback.
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.