diff src/app/services/render-loop/render-loop.service.ts @ 400:e06b62d949de

Return a callback when adding an animation task, allowing it to be removed.
author Lucas Thompson <dev@lucas.im>
date Fri, 02 Jun 2017 19:08:42 +0100
parents 308ea1c2612e
children
line wrap: on
line diff
--- 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<TaskId, OnSeekHandler>;
+  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);
     }
   }