You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

worker.js 1.2KB

2 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/LICENSE
  3. // declare global: tern, server
  4. var server;
  5. this.onmessage = function(e) {
  6. var data = e.data;
  7. switch (data.type) {
  8. case "init": return startServer(data.defs, data.plugins, data.scripts);
  9. case "add": return server.addFile(data.name, data.text);
  10. case "del": return server.delFile(data.name);
  11. case "req": return server.request(data.body, function(err, reqData) {
  12. postMessage({id: data.id, body: reqData, err: err && String(err)});
  13. });
  14. case "getFile":
  15. var c = pending[data.id];
  16. delete pending[data.id];
  17. return c(data.err, data.text);
  18. default: throw new Error("Unknown message type: " + data.type);
  19. }
  20. };
  21. var nextId = 0, pending = {};
  22. function getFile(file, c) {
  23. postMessage({type: "getFile", name: file, id: ++nextId});
  24. pending[nextId] = c;
  25. }
  26. function startServer(defs, plugins, scripts) {
  27. if (scripts) importScripts.apply(null, scripts);
  28. server = new tern.Server({
  29. getFile: getFile,
  30. async: true,
  31. defs: defs,
  32. plugins: plugins
  33. });
  34. }
  35. this.console = {
  36. log: function(v) { postMessage({type: "debug", message: v}); }
  37. };