worker.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import Prism from './prism';
  2. export default function rainbowWorker(e) {
  3. const message = e.data;
  4. const prism = new Prism(message.options);
  5. const result = prism.refract(message.code, message.lang);
  6. function _reply() {
  7. self.postMessage({
  8. id: message.id,
  9. lang: message.lang,
  10. result
  11. });
  12. }
  13. // I realized down the road I might look at this and wonder what is going on
  14. // so probably it is not a bad idea to leave a comment.
  15. //
  16. // This is needed because right now the node library for simulating web
  17. // workers “webworker-threads” will keep the worker open and it causes
  18. // scripts running from the command line to hang unless the worker is
  19. // explicitly closed.
  20. //
  21. // This means for node we will spawn a new thread for every asynchronous
  22. // block we are highlighting, but in the browser we will keep a single
  23. // worker open for all requests.
  24. if (message.isNode) {
  25. _reply();
  26. self.close();
  27. return;
  28. }
  29. setTimeout(() => {
  30. _reply();
  31. }, message.options.delay * 1000);
  32. }