diff --git a/src/app/game/game.component.ts b/src/app/game/game.component.ts index 65c33a4..c64f046 100644 --- a/src/app/game/game.component.ts +++ b/src/app/game/game.component.ts @@ -26,6 +26,7 @@ import { CheatingDetectionService } from './shared/cheating-detection.service'; import { TypescriptService } from './shared/typescript.service'; import { OnChange } from 'property-watch-decorator'; import { MatDialog } from '@angular/material/dialog'; +import { Observable } from 'rxjs'; @Component({ selector: 'app-game', @@ -382,24 +383,15 @@ export class GameComponent implements OnInit { } private runLevelWithUserCode(): void { - const conveyorBeltSubject = new Subject(); + this.startConveyorBeltAnimation(); + const conveyorBeltSubject = new Subject(); function toConveyorBelt(fruit: string) { conveyorBeltSubject.next(fruit); } - this.startConveyorBeltAnimation(); - try { - conveyorBeltSubject - .pipe( - concatMap(item => of(item).pipe(delay(1000))), - takeWhile(() => this.isRunActive), - tap(x => console.log('Into the pipe: ' + x)), - tap(fruit => this.pushFruitToPipe(fruit)), - tap((fruit: string) => this.addFruitToView(fruit)) - ).subscribe(this.exerciseService.assertExerciseOutput()); - + this.observeConveyorBelt(conveyorBeltSubject.asObservable()); this.executeCode(toConveyorBelt); } catch (error) { this.notifyAboutErrorInCode(error); @@ -409,6 +401,18 @@ export class GameComponent implements OnInit { } } + private observeConveyorBelt(conveyorBelt: Observable) { + const addDelayBetweenEachFruit = durationMs => concatMap((fruit: string) => of(fruit).pipe(delay(durationMs))); + conveyorBelt + .pipe( + addDelayBetweenEachFruit(1000), + takeWhile(() => this.isRunActive), + tap(x => console.log('Into the pipe: ' + x)), + tap(fruit => this.pushFruitToPipe(fruit)), + tap((fruit: string) => this.addFruitToView(fruit)) + ).subscribe(this.exerciseService.assertExerciseOutput()); + } + private pushFruitToPipe(fruit: string) { this.fruitsInPipe.push(fruit);