How to handle the side effect from Service(singleton) which is not serializable? #1539
Unanswered
WendellLiu
asked this question in
Q&A
Replies: 1 comment
-
You can communicate with a (defined externally) singleton like this: const machine = createMachine({
// ...
invoke: {
id: 'single',
src: () => (sendBack, receive) => {
receive(event => {
if (event.type === 'INIT') {
someSingleton.init();
}
// ...
}
}
},
// ...
on: {
INIT_SINGLETON: {
actions: send('INIT', { to: 'single' })
}
}
}); |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Let's say I have a webRTC client with lifecycle methods(for instance,
init
,play
,close
) I want to take into the state machine and it should be a singleton in the web app.The first thoughts coming up in my mind is
invoke
. Since I don't want to set the singleton as a global variable, the only way I could invoke some side effect with myClient
is by storing it in context. However, This client(third-party SDK) cannot be serialized since it's a circular structure.Any good practice for this scenario?
Beta Was this translation helpful? Give feedback.
All reactions