Skip to content

Commit

Permalink
Permitir interromper a transpilação do código
Browse files Browse the repository at this point in the history
  • Loading branch information
dgadelha committed Mar 8, 2024
1 parent ebd6ffe commit 37f4876
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/ide/src/app/tab-editor/tab-editor.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
type="button"
class="sidebar-button"
(click)="stopCode()"
[disabled]="!executor.running || transpiling"
[disabled]="!executor.running && !transpiling"
title="Parar execução"
>
<svg-icon src="assets/icon-stop.svg"></svg-icon>
Expand Down
6 changes: 6 additions & 0 deletions packages/ide/src/app/tab-editor/tab-editor.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,12 @@ export class TabEditorComponent implements OnInit, OnDestroy {
stopCode() {
this.gaService.event("editor_stop_execution", "Editor", "Botão de Parar Execução");
this.executor.stop();

if (this.transpiling) {
this.worker.abortTranspilation();
this.transpiling = false;
}

this.stdOutEditorCursorEnd();
}

Expand Down
11 changes: 11 additions & 0 deletions packages/ide/src/app/worker.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { PortugolCodeError } from "@portugol-webstudio/antlr";
@Injectable({ providedIn: "root" })
export class WorkerService {
worker?: Worker;
busy = false;

init() {
this.worker = new Worker("assets/portugol-worker/worker.js");
Expand Down Expand Up @@ -44,6 +45,8 @@ export class WorkerService {
this.init();
}

this.busy = true;

return new Promise(resolve => {
const id = Math.random().toString(36).substring(2, 9);
const now = Date.now();
Expand All @@ -52,6 +55,7 @@ export class WorkerService {
if (e.data.id === id) {
console.log("Transpiler Result", e.data, `${Date.now() - now}ms`);
this.worker?.removeEventListener("message", listener);
this.busy = false;
resolve(e.data);
}
};
Expand All @@ -60,4 +64,11 @@ export class WorkerService {
this.worker?.postMessage({ code, id, action: "transpile" });
});
}

async abortTranspilation() {
if (this.worker && this.busy) {
this.worker.terminate();
this.worker = undefined;
}
}
}

0 comments on commit 37f4876

Please sign in to comment.