65 lines
2.1 KiB
JavaScript
65 lines
2.1 KiB
JavaScript
/*
|
|
* ISC License
|
|
*
|
|
* Copyright (c) 2024 idnovate.com
|
|
* idnovate is a Registered Trademark & Property of idnovate.com, innovación y desarrollo SCP
|
|
*
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
* copyright notice and this permission notice appear in all copies.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
* PERFORMANCE OF THIS SOFTWARE.
|
|
*
|
|
* @author idnovate
|
|
* @copyright 2024 idnovate
|
|
* @license https://www.isc.org/licenses/ https://opensource.org/licenses/ISC ISC License
|
|
*/
|
|
|
|
// declare global: tern, server
|
|
|
|
var server;
|
|
|
|
this.onmessage = function(e) {
|
|
var data = e.data;
|
|
switch (data.type) {
|
|
case "init": return startServer(data.defs, data.plugins, data.scripts);
|
|
case "add": return server.addFile(data.name, data.text);
|
|
case "del": return server.delFile(data.name);
|
|
case "req": return server.request(data.body, function(err, reqData) {
|
|
postMessage({id: data.id, body: reqData, err: err && String(err)});
|
|
});
|
|
case "getFile":
|
|
var c = pending[data.id];
|
|
delete pending[data.id];
|
|
return c(data.err, data.text);
|
|
default: throw new Error("Unknown message type: " + data.type);
|
|
}
|
|
};
|
|
|
|
var nextId = 0, pending = {};
|
|
function getFile(file, c) {
|
|
postMessage({type: "getFile", name: file, id: ++nextId});
|
|
pending[nextId] = c;
|
|
}
|
|
|
|
function startServer(defs, plugins, scripts) {
|
|
if (scripts) importScripts.apply(null, scripts);
|
|
|
|
server = new tern.Server({
|
|
getFile: getFile,
|
|
async: true,
|
|
defs: defs,
|
|
plugins: plugins
|
|
});
|
|
}
|
|
|
|
this.console = {
|
|
log: function(v) { postMessage({type: "debug", message: v}); }
|
|
};
|