geospatial-services-framework / gsf-js-client-sdk

GSF JavaScript Client SDK
MIT License
6 stars 5 forks source link

Closing a GSF client connection #68

Open gsf-elefebvre opened 3 months ago

gsf-elefebvre commented 3 months ago

Is there a reliable way to close all the event listeners to a GSF client connection? Based on what I've seen in the tests you can close the event listeners this way

        client.removeAllListeners('JobCompleted');
        client.removeAllListeners('JobAccepted');
        client.removeAllListeners('JobStarted');
        client.removeAllListeners('JobSucceeded');
        client.removeAllListeners('JobProgress');
        client.removeAllListeners('JobFailed');

But the nodejs process will still not exit when I do this. Maybe this is something worth investigating to help with closing the connection. We could maybe create some close() method, or make sure the Job event listeners are being cleaned up.

gsf-elefebvre commented 3 months ago

After some more research I found running the event source close() method will allow nodejs to exit gracefully.

client._events.close();

This command closes the EventSource and not the EventEmitter events. When looking at the Client.js code there are a couple of things to point out. The EventSource implementation appears to be different between the browser and nodejs engines. This means the behavior to closing the connnection could be different. The client._events private variable is also being shared by both the EventEmitter and the EventSource objects. The client extends the EventEmitter class and inherits the client._events object to store event handlers as event-name-key: handler-function. Right now there is only one event the client registers in 'JobCompleted'. This means when I do

client.removeAllListeners('JobCompleted')

This will not only remove the 'JobCompleted' handler from client._events but will detach all the events defined in the EventSource without properly closing the listeners.

I think the client connection needs some sort of close() method to handle all of this, and also make sure the client._events private variable is not being shared by both the EventEmitter and EventSource.