From 67cf59b9c3c8ade4f792de4073fbcb74b6d5a584 Mon Sep 17 00:00:00 2001 From: zBrick20 Date: Tue, 6 Aug 2024 20:50:58 +0700 Subject: [PATCH 1/2] Make AutoRun runs multiple sessions --- src/extension.ts | 20 +++++++++++--------- src/state.ts | 21 +++++++++++---------- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/src/extension.ts b/src/extension.ts index 3869401..9fbd550 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -37,15 +37,17 @@ export async function activate(context: vscode.ExtensionContext) { state.show() if (config.autoRun()) { - const session = new RestorableSession( - context.workspaceState.get("lastSession"), - ) - - if (session.isRestorable()) { - menu.restoreSession(session, state) - - if (config.autoLaunchStudio()) { - argon.studio(true) + const lastSessions = context.workspaceState.get("lastSessions") + if (Array.isArray(lastSessions)) { + for (const lastSession of lastSessions) { + const session = new RestorableSession(lastSession) + if (session.isRestorable()) { + menu.restoreSession(session, state) + + if (config.autoLaunchStudio()) { + argon.studio(true) + } + } } } } diff --git a/src/state.ts b/src/state.ts index a83e892..e70bab9 100644 --- a/src/state.ts +++ b/src/state.ts @@ -37,28 +37,29 @@ export class State { this.sessions.push(session) this.updateItem() - this.context.workspaceState.update("lastSession", session) + this.context.workspaceState.update("lastSessions", this.sessions) } public removeSessions(ids: number[]) { - const lastSession = this.context.workspaceState.get("lastSession") + const lastSessions = this.context.workspaceState.get("lastSessions") this.sessions = this.sessions.filter((session) => { const matches = ids.includes(session.id) - if ( - matches && - lastSession instanceof Session && - session.equals(lastSession) - ) { - this.context.workspaceState.update("lastSession", undefined) + if (matches && Array.isArray(lastSessions)) { + const index = lastSessions.findIndex( + (value: Session) => value === session, + ) + if (index > -1) { + lastSessions[index] = undefined + } } return !matches }) - if (this.sessions[0]) { - this.context.workspaceState.update("lastSession", this.sessions[0]) + if (this.sessions.length === 0) { + this.context.workspaceState.update("lastSessions", undefined) } this.updateItem() From 32e752fc2a2188b2d1e5f96ecbcdb65c6f9d744e Mon Sep 17 00:00:00 2001 From: Dervex Date: Tue, 6 Aug 2024 21:48:03 -0400 Subject: [PATCH 2/2] Fix `workspaceState` updating, update changelog --- CHANGELOG.md | 4 ++++ src/extension.ts | 4 +++- src/session.ts | 6 +++++- src/state.ts | 12 ++++-------- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 112973b..ec2c4da 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), ## [Unreleased] +### Added + +- `AutoRun` option can now restore multiple sessions ([#28](https://github.com/argon-rbx/argon-vscode/pull/28)) + ## [2.0.8] - 2024-07-19 ### Added diff --git a/src/extension.ts b/src/extension.ts index 9fbd550..c15ebb0 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -38,13 +38,15 @@ export async function activate(context: vscode.ExtensionContext) { if (config.autoRun()) { const lastSessions = context.workspaceState.get("lastSessions") + if (Array.isArray(lastSessions)) { for (const lastSession of lastSessions) { const session = new RestorableSession(lastSession) + if (session.isRestorable()) { menu.restoreSession(session, state) - if (config.autoLaunchStudio()) { + if (session.needsStudio() && config.autoLaunchStudio()) { argon.studio(true) } } diff --git a/src/session.ts b/src/session.ts index 07c7bfb..ce0a9f1 100644 --- a/src/session.ts +++ b/src/session.ts @@ -23,7 +23,7 @@ export class Session { return this } - public equals(other: Session) { + public isSimilar(other: Session) { return this.type === other.type && this.project === other.project } } @@ -58,4 +58,8 @@ export class RestorableSession { public isRestorable() { return this.isComplete } + + public needsStudio() { + return this.type === "Serve" || this.type === "Build" + } } diff --git a/src/state.ts b/src/state.ts index e70bab9..50ec9d4 100644 --- a/src/state.ts +++ b/src/state.ts @@ -28,7 +28,7 @@ export class State { } public addSession(session: Session) { - if (this.sessions.find((s) => session.equals(s))) { + if (this.sessions.find((s) => session.isSimilar(s))) { logger.warn( `Session with type: ${session.type} and project: ${session.project} is already running. Ignore this message if this is desired behavior`, ) @@ -47,9 +47,8 @@ export class State { const matches = ids.includes(session.id) if (matches && Array.isArray(lastSessions)) { - const index = lastSessions.findIndex( - (value: Session) => value === session, - ) + const index = lastSessions.findIndex((s) => s === session) + if (index > -1) { lastSessions[index] = undefined } @@ -58,10 +57,7 @@ export class State { return !matches }) - if (this.sessions.length === 0) { - this.context.workspaceState.update("lastSessions", undefined) - } - + this.context.workspaceState.update("lastSessions", lastSessions) this.updateItem() }