fix: global execution queue added

This commit is contained in:
Anirban Kar 2024-11-17 13:26:09 +05:30
parent b7d609d315
commit 358487135f
3 changed files with 30 additions and 9 deletions

View File

@ -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;
}
}

View File

@ -42,7 +42,7 @@ export class WorkbenchStore {
modifiedFiles = new Set<string>();
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<void>) {
this.#globalExecutionQueue=this.#globalExecutionQueue.then(()=>callback())
}
get previews() {
return this.#previewsStore.previews;
}
@ -255,8 +259,10 @@ export class WorkbenchStore {
this.artifacts.setKey(messageId, { ...artifact, ...state });
}
async addAction(data: ActionCallbackData) {
addAction(data: ActionCallbackData) {
this.addToExecutionQueue(()=>this._addAction(data))
}
async _addAction(data: ActionCallbackData) {
const { messageId } = data;
const artifact = this.#getArtifact(messageId);
@ -265,10 +271,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 +307,11 @@ export class WorkbenchStore {
this.#editorStore.updateFile(fullPath, data.action.content);
if (!isStreaming) {
this.resetCurrentDocument();
await artifact.runner.runAction(data);
this.resetCurrentDocument();
}
} else {
artifact.runner.runAction(data);
await artifact.runner.runAction(data);
}
}

0
diff.txt Normal file
View File