Mercurial > hg > ugly-duckling
comparison 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 |
comparison
equal
deleted
inserted
replaced
399:6fe8ef9687de | 400:e06b62d949de |
---|---|
4 import {Injectable, NgZone} from '@angular/core'; | 4 import {Injectable, NgZone} from '@angular/core'; |
5 import {AudioPlayerService} from '../audio-player/audio-player.service'; | 5 import {AudioPlayerService} from '../audio-player/audio-player.service'; |
6 import {Subscription} from 'rxjs/Subscription'; | 6 import {Subscription} from 'rxjs/Subscription'; |
7 import {OnSeekHandler} from '../../playhead/PlayHeadHelpers'; | 7 import {OnSeekHandler} from '../../playhead/PlayHeadHelpers'; |
8 | 8 |
9 export type TaskRemover = () => void; | |
10 type TaskId = number; | |
11 | |
9 @Injectable() | 12 @Injectable() |
10 export class RenderLoopService { | 13 export class RenderLoopService { |
11 private playingStateSubscription: Subscription; | 14 private playingStateSubscription: Subscription; |
12 private seekedSubscription: Subscription; | 15 private seekedSubscription: Subscription; |
13 private tasks: OnSeekHandler[]; | 16 private tasks: Map<TaskId, OnSeekHandler>; |
17 private countingId: TaskId; | |
14 | 18 |
15 constructor(private player: AudioPlayerService, | 19 constructor(private player: AudioPlayerService, |
16 private zone: NgZone) { | 20 private zone: NgZone) { |
17 this.tasks = []; | 21 this.countingId = 0; |
22 this.tasks = new Map(); | |
18 this.seekedSubscription = this.player.seeked$.subscribe(() => { | 23 this.seekedSubscription = this.player.seeked$.subscribe(() => { |
19 if (!this.player.isPlaying()) { | 24 if (!this.player.isPlaying()) { |
20 this.zone.runOutsideAngular(() => { | 25 this.zone.runOutsideAngular(() => { |
21 this.runTasks(); | 26 this.runTasks(); |
22 }); | 27 }); |
28 this.animate(); | 33 this.animate(); |
29 } | 34 } |
30 }); | 35 }); |
31 } | 36 } |
32 | 37 |
33 addPlayingTask(task: OnSeekHandler): void { | 38 addPlayingTask(task: OnSeekHandler): TaskRemover { |
34 this.tasks.push(task); | 39 const id = this.countingId++; |
40 this.tasks.set(id, task); | |
41 return () => { | |
42 this.tasks.delete(id); | |
43 }; | |
35 } | 44 } |
36 | 45 |
37 private animate(): void { | 46 private animate(): void { |
38 this.zone.runOutsideAngular(() => { | 47 this.zone.runOutsideAngular(() => { |
39 const animateNextFrame = () => { | 48 const animateNextFrame = () => { |
46 }); | 55 }); |
47 } | 56 } |
48 | 57 |
49 private runTasks(): void { | 58 private runTasks(): void { |
50 const currentTime = this.player.getCurrentTime(); | 59 const currentTime = this.player.getCurrentTime(); |
51 for (const task of this.tasks) { | 60 for (const task of this.tasks.values()) { |
52 task(currentTime); | 61 task(currentTime); |
53 } | 62 } |
54 } | 63 } |
55 } | 64 } |