codacus commited on
Commit
87e9fc7
·
unverified ·
2 Parent(s): a101dc7 49f9d8b

Merge pull request #309 from thecodacus/fix-project-reload-execution-order

Browse files

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

app/lib/runtime/action-runner.ts CHANGED
@@ -94,7 +94,7 @@ export class ActionRunner {
94
 
95
  this.#updateAction(actionId, { ...action, ...data.action, executed: !isStreaming });
96
 
97
- this.#currentExecutionPromise = this.#currentExecutionPromise
98
  .then(() => {
99
  return this.#executeAction(actionId, isStreaming);
100
  })
@@ -119,7 +119,14 @@ export class ActionRunner {
119
  break;
120
  }
121
  case 'start': {
122
- await this.#runStartAction(action)
 
 
 
 
 
 
 
123
  break;
124
  }
125
  }
 
94
 
95
  this.#updateAction(actionId, { ...action, ...data.action, executed: !isStreaming });
96
 
97
+ return this.#currentExecutionPromise = this.#currentExecutionPromise
98
  .then(() => {
99
  return this.#executeAction(actionId, isStreaming);
100
  })
 
119
  break;
120
  }
121
  case 'start': {
122
+ // making the start app non blocking
123
+
124
+ this.#runStartAction(action).then(()=>this.#updateAction(actionId, { status: 'complete' }))
125
+ .catch(()=>this.#updateAction(actionId, { status: 'failed', error: 'Action failed' }))
126
+ // adding a delay to avoid any race condition between 2 start actions
127
+ // i am up for a better approch
128
+ await new Promise(resolve=>setTimeout(resolve,2000))
129
+ return
130
  break;
131
  }
132
  }
app/lib/stores/workbench.ts CHANGED
@@ -42,7 +42,7 @@ export class WorkbenchStore {
42
  modifiedFiles = new Set<string>();
43
  artifactIdList: string[] = [];
44
  #boltTerminal: { terminal: ITerminal; process: WebContainerProcess } | undefined;
45
-
46
  constructor() {
47
  if (import.meta.hot) {
48
  import.meta.hot.data.artifacts = this.artifacts;
@@ -52,6 +52,10 @@ export class WorkbenchStore {
52
  }
53
  }
54
 
 
 
 
 
55
  get previews() {
56
  return this.#previewsStore.previews;
57
  }
@@ -255,8 +259,11 @@ export class WorkbenchStore {
255
 
256
  this.artifacts.setKey(messageId, { ...artifact, ...state });
257
  }
258
-
259
- async addAction(data: ActionCallbackData) {
 
 
 
260
  const { messageId } = data;
261
 
262
  const artifact = this.#getArtifact(messageId);
@@ -265,10 +272,18 @@ export class WorkbenchStore {
265
  unreachable('Artifact not found');
266
  }
267
 
268
- artifact.runner.addAction(data);
269
  }
270
 
271
- async runAction(data: ActionCallbackData, isStreaming: boolean = false) {
 
 
 
 
 
 
 
 
272
  const { messageId } = data;
273
 
274
  const artifact = this.#getArtifact(messageId);
@@ -293,11 +308,11 @@ export class WorkbenchStore {
293
  this.#editorStore.updateFile(fullPath, data.action.content);
294
 
295
  if (!isStreaming) {
296
- this.resetCurrentDocument();
297
  await artifact.runner.runAction(data);
 
298
  }
299
  } else {
300
- artifact.runner.runAction(data);
301
  }
302
  }
303
 
 
42
  modifiedFiles = new Set<string>();
43
  artifactIdList: string[] = [];
44
  #boltTerminal: { terminal: ITerminal; process: WebContainerProcess } | undefined;
45
+ #globalExecutionQueue=Promise.resolve();
46
  constructor() {
47
  if (import.meta.hot) {
48
  import.meta.hot.data.artifacts = this.artifacts;
 
52
  }
53
  }
54
 
55
+ addToExecutionQueue(callback: () => Promise<void>) {
56
+ this.#globalExecutionQueue=this.#globalExecutionQueue.then(()=>callback())
57
+ }
58
+
59
  get previews() {
60
  return this.#previewsStore.previews;
61
  }
 
259
 
260
  this.artifacts.setKey(messageId, { ...artifact, ...state });
261
  }
262
+ addAction(data: ActionCallbackData) {
263
+ this._addAction(data)
264
+ // this.addToExecutionQueue(()=>this._addAction(data))
265
+ }
266
+ async _addAction(data: ActionCallbackData) {
267
  const { messageId } = data;
268
 
269
  const artifact = this.#getArtifact(messageId);
 
272
  unreachable('Artifact not found');
273
  }
274
 
275
+ return artifact.runner.addAction(data);
276
  }
277
 
278
+ runAction(data: ActionCallbackData, isStreaming: boolean = false) {
279
+ if(isStreaming) {
280
+ this._runAction(data, isStreaming)
281
+ }
282
+ else{
283
+ this.addToExecutionQueue(()=>this._runAction(data, isStreaming))
284
+ }
285
+ }
286
+ async _runAction(data: ActionCallbackData, isStreaming: boolean = false) {
287
  const { messageId } = data;
288
 
289
  const artifact = this.#getArtifact(messageId);
 
308
  this.#editorStore.updateFile(fullPath, data.action.content);
309
 
310
  if (!isStreaming) {
 
311
  await artifact.runner.runAction(data);
312
+ this.resetAllFileModifications();
313
  }
314
  } else {
315
+ await artifact.runner.runAction(data);
316
  }
317
  }
318