Open ghost opened 8 months ago
Update: this API does a number of things i dont like; i have quarantined this it into its own worker thread which gives me an acceptable result.
I can just call em.removeAllListeners()
and reestablish at will without effecting other APIs because its within its own worker thread environment.
You should either elevate your event handler to an instanced function if you are using classes, or capture a reference to your event handler in a const, and importantly pass the SAME reference into on
and off
functions.
one way you could do this in your example is:-
const newEntityHandler = (entity) => {
let exist = 0;
...
espClient[x].on('newEntity', newEntityHandler);
const errorHandler = (error) => {
console.log(error);
log("ESP module went offline, resetting ESP system", 2, 2);
espClient[x].off('newEntity', newEntityHandler);
espClient[x].off('error', errorHandler);
reset(); // if there's a connection problem, start reset sequence
});
espClient[x].on('error', errorHandler);
You'll probably want to keep track of all the entity 'state' handlers you on
as well... e.g.
in espInit
after adding the client to espClient
you could:
handlers[x] = [];
where handlers is an empty correctly sized array (for the number of clients) and then when subscribing to state changes on an entity you can do something like this:
const stateHandler = ....
entity.on('state', stateHandler);
handlers[x].push(() => entity.off('state', stateHandler));
Then in your error handler you can do something like this:
handlers[x].forEach(f=>f());
Additionally to all that... if a client errors and disconnects, you don't need to make a new one... you can just call connect()
on the existing instance. The Client, Connection, and FrameHelper only get reset if YOU explicitly call disconnect
on the Client or Connection.
Please forgive my lack of understanding about this API and Javascript programming in general but im having pretty difficult time implementing this API in a real world project.
It was not clear in the beginning how to push state changes to the modules but then someone mentioned using
switchCommandService
rather thansetState
. So i got that to work by using an Emitter in theClient.on('newEntity'
code block but this is where things get a little weird.If the ESP client goes offline, resets or otherwise stops responding, then the problem is with the stagnant emitter that gets lost in the old client connection. I have tried many many ways to fix this issue but all attempts to use the
removeEventListener
function fail, even if i use global variables and functions. This leaves only one option and that is to use theremoveAllListeners()
function to kill all listeners (including those used by other APIs!!) and then disconnect all ESP client connections so that the listeners will be recreated and reconnect to all ESP devices. This is really undesirable because important updates or commands could be lost during this process.I hate this workaround but i dont see any other options, ive been at this for days, im ready to give up...