question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to communicate between component and timer-worker ?

See original GitHub issue

Hi 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:closed
  • Created 6 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
chrisguttandincommented, Mar 22, 2018

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.

0reactions
davvozcommented, Mar 24, 2018

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular — Four Ways for Communication Between Components
1. Binding (@Input & @Output). Data binding is the most basic way to communicate between components. @Component({ selector: ' ...
Read more >
How to communicate between Components - freeCodeCamp
Before analyzing how to communicate between components, we need understand that components are organized in a tree structure.
Read more >
Communicating Between Components in React - Pluralsight
Let's take a look at the directional communication and data flow that React enables between components. From Parent to Child with Props. The ......
Read more >
How to communicate between component in Angular?
If you are trying to communicate from a parent component to a child component, this is pretty clearly described using @Input and EventEmitters...
Read more >
Angular Component Communication - 6 Ways to Choose From
Learn 6 different ways for data sharing and component communication in Angular. · using the @Input decorator to define input properties and pass...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found