- Hands-On Data Structures and Algorithms with JavaScript
- Kashyap Mukkamala
- 212字
- 2021-06-30 19:12:11
Basic web worker communication
As the component loads, we will want to set up the worker so that it is not something that we have to repeat several times. So, imagine if there were a way in which you can set up something conditionally and perform an action only when you want it to. In our case, you can add it to the constructor or to any of the lifecycle hooks that denote what phase the component is in such as OnInit, OnContentInit, OnViewInit and so on, which are provided by Angular as follows:
this.worker = new Worker('scripts.bundle.js');
this.worker.addEventListener('message', (e) => {
this.result = e.data;
});
Once initialized, we then use the addEventListener() method to listen for any new messages—that is, results coming from our worker.
Any time the code is changed, we simply pass that data to the worker that we have now set up. The implementation for this looks as follows:
codeChange() {
this.worker.postMessage(this.code);
}
As you can note, the main application component is intentionally lean. We are leveraging workers for the sole reason that CPU-intensive operations can be kept away from the main thread. In this case, we can move all the logic including the validations into the worker, which is exactly what we have done.
- Spring 5企業級開發實戰
- Mastering Selenium WebDriver
- 單片機C語言程序設計實訓100例:基于STC8051+Proteus仿真與實戰
- Python測試開發入門與實踐
- Java深入解析:透析Java本質的36個話題
- Learning Laravel 4 Application Development
- Oracle從入門到精通(第5版)
- Linux Device Drivers Development
- Mathematica Data Analysis
- AppInventor實踐教程:Android智能應用開發前傳
- 運用后端技術處理業務邏輯(藍橋杯軟件大賽培訓教材-Java方向)
- PHP+MySQL+Dreamweaver動態網站開發從入門到精通(第3版)
- Learning AngularJS for .NET Developers
- Android項目實戰:手機安全衛士開發案例解析
- 小程序,巧應用:微信小程序開發實戰(第2版)