Handle stdout and stderr result
continuous-integration/drone/push Build is passing Details

pull/6/head
Bastien OLLIER 1 year ago
parent a2f06ac993
commit 09bdc6abff

@ -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 = '';
}
}

@ -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<string>();
private resultSubject = new Subject<ExecutionMessage>();
constructor(private http: HttpClient) {}
constructor() {}
executeCode(code: string, language: string) {
const sse = new SSE(this.apiUrl, {
@ -25,13 +29,17 @@ export class CodeExecutionService {
sse.addEventListener('message', (event: MessageEvent<string>) => {
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<string> {
getResult(): Observable<ExecutionMessage> {
return this.resultSubject.asObservable();
}
}

Loading…
Cancel
Save