January 2018
Intermediate to advanced
332 pages
7h 36m
English
Now that the app component is set and ready to send messages, the worker needs to be enabled to receive the messages from the main thread. To do that, add the following code to your worker.js file:
init();function init() { self.addEventListener('message', function(e) { var code = e.data; if(typeof code !== 'string' || code.match(/.*[a-zA-Z]+.*/g)) { respond('Error! Cannot evaluate complex expressions yet. Please try again later'); } else { respond(evaluate(convert(code))); } });}
As you can see, we added the capability of listening for any message that might be sent to the worker and then the worker simply takes that data and applies some basic validation on it before trying to evaluate and return any value ...
Read now
Unlock full access