diff --git a/src/app/components/editor/editor.component.ts b/src/app/components/editor/editor.component.ts index 07f941c..44c7e73 100644 --- a/src/app/components/editor/editor.component.ts +++ b/src/app/components/editor/editor.component.ts @@ -68,6 +68,12 @@ export class EditorComponent implements OnInit { ngOnInit(): void { // Appel à changeMode pour mettre à jour le contenu de l'éditeur et le mode this.changeMode(); + + this.codeExecutionService.getResult().subscribe((result) => { + if (result.type !== 'exit') { + this.resultContent += result.text; + } + }); } // Change le langage de l'éditeur @@ -160,8 +166,6 @@ export class EditorComponent implements OnInit { this.codeExecutionService.executeCode(codeToExecute, this.mode); - this.codeExecutionService.getResult().subscribe((result) => { - this.resultContent = result; - }); + this.resultContent = ''; } } diff --git a/src/app/services/codeExecution.service.ts b/src/app/services/codeExecution.service.ts index f7f1b97..4ec25ad 100644 --- a/src/app/services/codeExecution.service.ts +++ b/src/app/services/codeExecution.service.ts @@ -1,16 +1,20 @@ import { Injectable } from '@angular/core'; -import { HttpClient } from '@angular/common/http'; import { SSE } from 'sse.js'; import { Observable, Subject } from 'rxjs'; +export type ExecutionMessage = { + type: 'stdout' | 'stderr' | 'exit', + text: string +}; + @Injectable({ providedIn: 'root', }) export class CodeExecutionService { private apiUrl = 'http://localhost:3000/run'; - private resultSubject = new Subject(); + private resultSubject = new Subject(); - constructor(private http: HttpClient) {} + constructor() {} executeCode(code: string, language: string) { const sse = new SSE(this.apiUrl, { @@ -21,17 +25,21 @@ export class CodeExecutionService { }, payload: JSON.stringify({ code, language }), }); - + sse.addEventListener('message', (event: MessageEvent) => { const result = event.data; - // Émettre le résultat à tous les abonnés + // @ts-ignore + const type = event.id; const text = decodeURIComponent(result.replace(/%00/g, '')); - this.resultSubject.next(text); + if (type === 'end') { + sse.close(); + } + this.resultSubject.next({ type, text }); }); } - getResult(): Observable { + getResult(): Observable { return this.resultSubject.asObservable(); } }