How to communicate between component and timer-worker ?
See original GitHub issueHi dude.
I’m pretty sure this is not an issues…anyway.
Speaking about CPU , I tried your worker-timers and I realized that is more cheaper than normal setTimeout() , this is great.
But I can not connect a component (…or probably I’m not capable) , let me explain.
I have an app-component that use a timer-service that implement your timer-workers.
I want to recive the tick from the timer-service in the app-component and assign it to a variable.
In my timer-service i have this method that I partially copied from the web :
private scheduleNote() { let contextPlayTime; let currentTime = this.audioContext.currentTime; currentTime -= this.startTime; while (this.noteTime < currentTime + 0.200) { contextPlayTime = this.noteTime + this.startTime; this.changeStateTrack(contextPlayTime); this.nextNote(); } this.timer = workerTimers.setTimeout(this.scheduleNote.bind(this), 0); }
I normally use a BehaviorSubject in timer-service and a Subscription in the app-component so i can comunicate between this 2 friends .Using this metohd , without worker-timers everything is alright … excluding the cpu.
So in my app-component:
ngOnInit(): void { this.subscription = this.timerService.trackStateItem$ .subscribe(res => { this.timePosition = res.timePosition; }); }
It happens ,using worker-timers, that the variable( this.timePosition) in the app-component does not update. Using the console I realized that the timer-workers works … so , what am I doing wrong?
Issue Analytics
- State:
- Created 6 years ago
- Comments:6 (3 by maintainers)
Top GitHub Comments
No worries, I’m not a native speaker myself.
I think I know what could be causing your problem. Angular is using zone.js to patch all native APIs. It does for example patch the native setTimeout function. Whenever the callback of a setTimeout function gets executed, Angular knows that it will have to execute its change detection once that function completes.
Angular (or zone.js) doesn’t know anything about worker-timers and it does therefore not run its change detection algorithm in response to an executed callback. If you want to achieve a similar behavior you have to tell Angular manually about the changes.
There is an article by the guys from thoughtram which explains the concept of zones very nicely. It also shows how to manually trigger the change detection.
Thanks for the advice @chrisguttandin . I will try to do as you say. Actually I had already tried requestAnimationFrame but it did not work, but now I can try again. In practice, 2 parallel timers, one for graphics(requestAnimationFrame) and one for sound(workersTimer). Thanks for everything, people like you make better world.