Skip to content

Commit

Permalink
Fix memory leak on Ux4iot initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Rollmann committed Oct 4, 2023
1 parent fb2c19c commit c6c07fd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/library/Ux4iotContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export const Ux4iotContextProvider: ComponentType<Ux4iotProviderProps> = ({
initialize();

return () => {
Ux4iot.destroyInitialization();
if (ux4iot) {
ux4iot.destroy();
setUx4iot(undefined);
Expand Down
13 changes: 10 additions & 3 deletions src/library/base/Ux4iot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export class Ux4iot {
retryTimeout: number;
maxRetryTimeout: number;
retryTimeoutAfterError?: NodeJS.Timeout;
static initializationTimeout?: NodeJS.Timeout;
onSocketConnectionUpdate?: ConnectionUpdateFunction;
onSessionId?: (sessionId: string) => void;

Expand Down Expand Up @@ -101,11 +102,10 @@ export class Ux4iot {
const timeout = reconnectTimeout ?? RECONNECT_TIMEOUT;
const maxTimeout = maxReconnectTimeout ?? MAX_RECONNECT_TIMEOUT;
const api = new Ux4iotApi(options);
let initializationTimeout: NodeJS.Timeout | undefined;
try {
const sessionId = await api.getSessionId();
api.setSessionId(sessionId);
clearTimeout(initializationTimeout);
clearTimeout(Ux4iot.initializationTimeout);
return new Ux4iot(options, sessionId, api, onSessionId);
} catch (error) {
const [reason, description] = NETWORK_STATES.UX4IOT_OFFLINE;
Expand All @@ -122,13 +122,20 @@ export class Ux4iot {
};

return new Promise((resolve, reject) => {
initializationTimeout = setTimeout(() => {
Ux4iot.initializationTimeout = setTimeout(() => {
return resolve(Ux4iot.create(nextOptions, onSessionId));
}, timeout);
});
}
}

public static destroyInitialization() {
if (Ux4iot.initializationTimeout) {
console.warn('Initialization interval canceled');
clearTimeout(Ux4iot.initializationTimeout);
}
}

private initializeSocket() {
const socketURI = this.api.getSocketURL(this.sessionId);
this.socket = io(socketURI, { transports: ['websocket', 'polling'] });
Expand Down

0 comments on commit c6c07fd

Please sign in to comment.