# HG changeset patch # User Lucas Thompson # Date 1496426922 -3600 # Node ID e06b62d949de5a86d3b5d8281c9fdf031e6571e5 # Parent 6fe8ef9687deafa8a79d1debb18effbda2ab9a35 Return a callback when adding an animation task, allowing it to be removed. diff -r 6fe8ef9687de -r e06b62d949de src/app/services/render-loop/render-loop.service.ts --- a/src/app/services/render-loop/render-loop.service.ts Fri Jun 02 17:24:33 2017 +0100 +++ b/src/app/services/render-loop/render-loop.service.ts Fri Jun 02 19:08:42 2017 +0100 @@ -6,15 +6,20 @@ import {Subscription} from 'rxjs/Subscription'; import {OnSeekHandler} from '../../playhead/PlayHeadHelpers'; +export type TaskRemover = () => void; +type TaskId = number; + @Injectable() export class RenderLoopService { private playingStateSubscription: Subscription; private seekedSubscription: Subscription; - private tasks: OnSeekHandler[]; + private tasks: Map; + private countingId: TaskId; constructor(private player: AudioPlayerService, private zone: NgZone) { - this.tasks = []; + this.countingId = 0; + this.tasks = new Map(); this.seekedSubscription = this.player.seeked$.subscribe(() => { if (!this.player.isPlaying()) { this.zone.runOutsideAngular(() => { @@ -30,8 +35,12 @@ }); } - addPlayingTask(task: OnSeekHandler): void { - this.tasks.push(task); + addPlayingTask(task: OnSeekHandler): TaskRemover { + const id = this.countingId++; + this.tasks.set(id, task); + return () => { + this.tasks.delete(id); + }; } private animate(): void { @@ -48,7 +57,7 @@ private runTasks(): void { const currentTime = this.player.getCurrentTime(); - for (const task of this.tasks) { + for (const task of this.tasks.values()) { task(currentTime); } }