chore: Cleanup SessionManager interface
This commit is contained in:
2 files changed
+15
-31
No files matched your search
@@ -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();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in new issue
Block a user