chore: Cleanup SessionManager interface

This commit is contained in:
Susmitha Girumala committed 2021-05-12 11:40:50 -04:00
1 parent ee748de5c9
commit 3ef7436f3f
2 files changed
+15 -31

No files matched your search

+10 -13
View File
@@ -48,16 +48,7 @@ class Session {
* @return {boolean}
*/
get isActive() {
return this.#process.connected;
}
/**
* Returns session proxy details.
* Only available after successful start of a session.
* @return {object}
*/
get proxyDetails() {
return this.#proxyDetails;
return !this.#process.killed;
}
/**
@@ -71,6 +62,7 @@ class Session {
this.#proxyDetails = spawnedSession.response;
this.#process = spawnedSession.childProcess;
this.#id = this.#proxyDetails.session_id;
return this.#proxyDetails;
});
}
@@ -79,9 +71,14 @@ class Session {
*/
stop() {
return new Promise((resolve, reject) => {
this.#process.on('close', () => resolve());
this.#process.on('error', (e) => reject(e));
this.#process.kill();
if (this.isActive) {
this.#process.on('close', () => resolve());
this.#process.on('error', (e) => reject(e));
this.#process.kill();
} else {
// Do nothing when process isn't active
resolve();
}
});
}
+5 -18
View File
@@ -10,7 +10,6 @@ class SessionManager {
/**
* Start a session and track it.
* Returns session proxy details if successfully started.
* Only successfully started sessions will be tracked.
* @param {string} addr
* @param {string} target_id
* @param {string} token
@@ -18,33 +17,21 @@ class SessionManager {
*/
async start(addr, target_id, token, host_id) {
const session = new Session(addr, target_id, token, host_id);
return session.start().then(() => {
this.#sessions.push(session);
return session.proxyDetails;
});
this.#sessions.push(session);
return session.start();
}
/**
* Stop a session and remove it from tracked sessions.
* Stop a session using identifier.
* @param {string} session_id
*/
async stopById(session_id) {
const index = this.#sessions.findIndex(
(session) => session.id === session_id
);
if (index === -1) return;
const session = this.#sessions[index];
return session.stop().then(() => {
this.#sessions.splice(index, 1);
});
const session = this.#sessions.find((session) => session.id === session_id);
return session.stop();
}
stopAll() {
}
get sessions() {
return this.#sessions;
}
}
const sessionManager = new SessionManager();