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