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