Skip to content

Commit

Permalink
refactor: move conveyor belt observing code into own method
Browse files Browse the repository at this point in the history
  • Loading branch information
hwanders committed Apr 27, 2021
1 parent 2c6bbdc commit aad1981
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions src/app/game/game.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -382,24 +383,15 @@ export class GameComponent implements OnInit {
}

private runLevelWithUserCode(): void {
const conveyorBeltSubject = new Subject<string>();
this.startConveyorBeltAnimation();

const conveyorBeltSubject = new Subject<string>();
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);
Expand All @@ -409,6 +401,18 @@ export class GameComponent implements OnInit {
}
}

private observeConveyorBelt(conveyorBelt: Observable<string>) {
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);

Expand Down

0 comments on commit aad1981

Please sign in to comment.