codacus commited on
Commit
3584871
·
1 Parent(s): b7d609d

fix: global execution queue added

Browse files
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,10 @@ 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 +271,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 +307,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.addToExecutionQueue(()=>this._addAction(data))
264
+ }
265
+ async _addAction(data: ActionCallbackData) {
266
  const { messageId } = data;
267
 
268
  const artifact = this.#getArtifact(messageId);
 
271
  unreachable('Artifact not found');
272
  }
273
 
274
+ return artifact.runner.addAction(data);
275
  }
276
 
277
+ runAction(data: ActionCallbackData, isStreaming: boolean = false) {
278
+ if(isStreaming) {
279
+ this._runAction(data, isStreaming)
280
+ }
281
+ else{
282
+ this.addToExecutionQueue(()=>this._runAction(data, isStreaming))
283
+ }
284
+ }
285
+ async _runAction(data: ActionCallbackData, isStreaming: boolean = false) {
286
  const { messageId } = data;
287
 
288
  const artifact = this.#getArtifact(messageId);
 
307
  this.#editorStore.updateFile(fullPath, data.action.content);
308
 
309
  if (!isStreaming) {
 
310
  await artifact.runner.runAction(data);
311
+ this.resetCurrentDocument();
312
  }
313
  } else {
314
+ await artifact.runner.runAction(data);
315
  }
316
  }
317
 
diff.txt ADDED
File without changes