diff --git a/app/lib/runtime/action-runner.ts b/app/lib/runtime/action-runner.ts index f94390be9..e38a8ce9e 100644 --- a/app/lib/runtime/action-runner.ts +++ b/app/lib/runtime/action-runner.ts @@ -94,7 +94,7 @@ export class ActionRunner { this.#updateAction(actionId, { ...action, ...data.action, executed: !isStreaming }); - this.#currentExecutionPromise = this.#currentExecutionPromise + return this.#currentExecutionPromise = this.#currentExecutionPromise .then(() => { return this.#executeAction(actionId, isStreaming); }) @@ -119,7 +119,14 @@ export class ActionRunner { break; } case 'start': { - await this.#runStartAction(action) + // making the start app non blocking + + this.#runStartAction(action).then(()=>this.#updateAction(actionId, { status: 'complete' })) + .catch(()=>this.#updateAction(actionId, { status: 'failed', error: 'Action failed' })) + // adding a delay to avoid any race condition between 2 start actions + // i am up for a better approch + await new Promise(resolve=>setTimeout(resolve,2000)) + return break; } } diff --git a/app/lib/stores/workbench.ts b/app/lib/stores/workbench.ts index 8589391c8..1dbfc5b5b 100644 --- a/app/lib/stores/workbench.ts +++ b/app/lib/stores/workbench.ts @@ -42,7 +42,7 @@ export class WorkbenchStore { modifiedFiles = new Set(); artifactIdList: string[] = []; #boltTerminal: { terminal: ITerminal; process: WebContainerProcess } | undefined; - + #globalExecutionQueue=Promise.resolve(); constructor() { if (import.meta.hot) { import.meta.hot.data.artifacts = this.artifacts; @@ -52,6 +52,10 @@ export class WorkbenchStore { } } + addToExecutionQueue(callback: () => Promise) { + this.#globalExecutionQueue=this.#globalExecutionQueue.then(()=>callback()) + } + get previews() { return this.#previewsStore.previews; } @@ -255,8 +259,11 @@ export class WorkbenchStore { this.artifacts.setKey(messageId, { ...artifact, ...state }); } - - async addAction(data: ActionCallbackData) { + addAction(data: ActionCallbackData) { + this._addAction(data) + // this.addToExecutionQueue(()=>this._addAction(data)) + } + async _addAction(data: ActionCallbackData) { const { messageId } = data; const artifact = this.#getArtifact(messageId); @@ -265,10 +272,18 @@ export class WorkbenchStore { unreachable('Artifact not found'); } - artifact.runner.addAction(data); + return artifact.runner.addAction(data); } - async runAction(data: ActionCallbackData, isStreaming: boolean = false) { + runAction(data: ActionCallbackData, isStreaming: boolean = false) { + if(isStreaming) { + this._runAction(data, isStreaming) + } + else{ + this.addToExecutionQueue(()=>this._runAction(data, isStreaming)) + } + } + async _runAction(data: ActionCallbackData, isStreaming: boolean = false) { const { messageId } = data; const artifact = this.#getArtifact(messageId); @@ -293,11 +308,11 @@ export class WorkbenchStore { this.#editorStore.updateFile(fullPath, data.action.content); if (!isStreaming) { - this.resetCurrentDocument(); await artifact.runner.runAction(data); + this.resetAllFileModifications(); } } else { - artifact.runner.runAction(data); + await artifact.runner.runAction(data); } }