Skip to content

Commit

Permalink
Merge pull request #309 from thecodacus/fix-project-reload-execution-…
Browse files Browse the repository at this point in the history
…order

fix(project-reload): execution order is fix, this fixes the inconsistency on project reload
  • Loading branch information
thecodacus authored Nov 19, 2024
2 parents a101dc7 + 49f9d8b commit 87e9fc7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
11 changes: 9 additions & 2 deletions app/lib/runtime/action-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
})
Expand All @@ -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;
}
}
Expand Down
29 changes: 22 additions & 7 deletions app/lib/stores/workbench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -52,6 +52,10 @@ export class WorkbenchStore {
}
}

addToExecutionQueue(callback: () => Promise<void>) {
this.#globalExecutionQueue=this.#globalExecutionQueue.then(()=>callback())
}

get previews() {
return this.#previewsStore.previews;
}
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
}
}

Expand Down

0 comments on commit 87e9fc7

Please sign in to comment.