Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(project-reload): execution order is fix, this fixes the inconsistency on project reload #309

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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