Hiya...new qwik guy. I'm working on a library for qwik. The api is something like this:
const [state, send] = useMachine(
{
qrl: $(() =>
noSerialize(
machine({
onCount(count) {
context.count = count
},
}),
),
),
initialState: noSerialize(machine({ count: 10 }).getState()),
// Preferred way
machine: noSerialize(machine({ count: 10 })),
}
)
the machine function returns a class, I can't serialize it. So I currently use qrl to send it, then internally I await it, but my component needs to have a default value, so I use initialState to provide the default value.
I'd prefer to just have only the third key though, where I pass machine()
Now Internally in useMachine function:
const store = useStore({
service: null,
})
useVisibleTask$(async ({ cleanup }) => {
// Load the service
const service = await qrl()
service!.start()
store.service = noSerialize(service)
cleanup(() => {
service!.stop()
})
})
return useComputed$(() => store.service)
This is what works for me.
When I try just sending the machine() in the way I said I prefer. I want to do those tasks like start stop also, then I return it from here, I'm unable to use it outside.
E.g.
const send = $((event: TEvent | string) => {
service.value?.send(event)
})
This service here is the returned useComputed$(() => store.service) in previous function, but within the send method it's seen as undefined.
Apparently:
Non-Serializable Variables Become undefined in QRLs
I'm just trying to find the best way to make this work with that desired API
Thanks