I have simple example here to replicate my problem. I'm trying to create kind of generic client handler. When client connects via TCP server i invoke clientFx effect to create the client instance.
Let's say i will have different TCP servers they all have common clientFx, but different way to handle incoming data. Thats why i have handleData function, which returns 2 events, input event onData and output event onPayload, which returns parsed payload.
I ended here in client.done.watch function, becouse i feel that define handling dataflow inside watch is not correct way to do so.
Is this good way to go or am I totally wrong?
import * as net from 'net';
import { createDomain, createEffect, forward } from "effector";
function handleData() {
const domain = createDomain();
const onData = domain.createEvent();
const onPayload = domain.createEvent();
const $store = domain.createStore<number>(0);
$store.on(onData, x => x + 1);
forward({ from: $store, to: onPayload });
return {
onData,
onPayload
};
}
const clientFx = createEffect((socket: net.Socket) => {
const { onData, onPayload } = handleData();
socket.on('data', onData);
return {
clientId: Math.floor(Math.random() * 100),
onPayload
}
});
clientFx.done.watch(({ result: client }) => {
client.onPayload.watch((state) => {
console.log('Store', client.clientId, 'changed to', state);
});
forward({ from: client.onPayload, to: saveToDbFx });
});
net.createServer(clientFx).listen(5555);
I did not catch the context of your question but I'll give it a try. You could ask for additional questions