- 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.
- Python for Secret Agents:Volume II
- iOS 9 Game Development Essentials
- Java加密與解密的藝術(shù)(第2版)
- 數(shù)據(jù)庫系統(tǒng)原理及MySQL應(yīng)用教程
- 深入理解Java7:核心技術(shù)與最佳實(shí)踐
- 新編Premiere Pro CC從入門到精通
- Learn WebAssembly
- OpenShift在企業(yè)中的實(shí)踐:PaaS DevOps微服務(wù)(第2版)
- 編程數(shù)學(xué)
- 從0到1:HTML5 Canvas動畫開發(fā)
- Java程序設(shè)計實(shí)用教程(第2版)
- PHP動態(tài)網(wǎng)站開發(fā)實(shí)踐教程
- Python數(shù)據(jù)預(yù)處理技術(shù)與實(shí)踐
- 基于JavaScript的WebGIS開發(fā)
- MonoTouch應(yīng)用開發(fā)實(shí)踐指南:使用C#和.NET開發(fā)iOS應(yīng)用