-
Notifications
You must be signed in to change notification settings - Fork 20
/
TernWorker.js
165 lines (128 loc) · 3.6 KB
/
TernWorker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
* Ternific Copyright (c) 2014 Miguel Castillo.
*
* Licensed under MIT
*/
importScripts("node_modules/requirejs/require.js");
require.config({
baseUrl: "node_modules/",
packages: [{
name: "acorn",
main: "dist/acorn",
location: "tern/node_modules/acorn"
}]
});
require(["tern/lib/tern"], function(tern) {
var TERN_ROOT = "tern/";
var TERN_PLUGINS = TERN_ROOT + "plugin/";
function Extender(target /*, [source]+ */) {
var source, length, i;
target = target || {};
// Allow n params to be passed in to extend this object
for (i = 1, length = arguments.length; i < length; i++) {
source = arguments[i];
for (var property in source) {
if (source.hasOwnProperty(property)) {
target[property] = source[property];
}
}
}
return target;
}
function parsePlugins(settings) {
var paths = [], configs = {}, config, name;
for (var pluginKey in settings.plugins) {
config = settings.plugins[pluginKey];
//https://regex101.com/r/cJ7wX6/1
pluginKey = pluginKey.replace(/[\/\\]+/g, "/");
if (pluginKey.indexOf("/") !== -1) {
paths.push(pluginKey + ".js");
}
else {
paths.push(TERN_PLUGINS + pluginKey);
}
//https://regex101.com/r/nT6tR7/1
name = /\/?(\w+)(?:[.\w]+)?$/.exec(pluginKey)[1];
configs[name] = config;
}
return {
paths: paths,
configs: configs
};
}
function Server(settings) {
this.pending = {};
var nextId = 1;
var plugins = parsePlugins(settings);
require(plugins.paths, function() {
this.settings = Extender({}, settings, {
getFile: getFile.bind(this),
plugins: plugins.configs
});
this.server = new tern.Server(this.settings);
postMessage({ type: "ready" });
function getFile(file, c) {
this.pending[nextId] = c;
postMessage({
type: "getFile",
name: file,
id: nextId
});
++nextId;
}
}.bind(this));
}
Server.prototype.clear = function() {
this.server = new tern.Server(this.settings || {});
};
Server.prototype.addFile = function(data) {
if (data.id && this.pending[data.id]) {
var c = this.pending[data.id];
delete this.pending[data.id];
c(data.err, data.text);
}
else {
this.server.addFile(data.name, data.text);
}
};
Server.prototype.deleteFile = function(data) {
this.server.delFile(data);
};
Server.prototype.request = function(data) {
function done(err, reqData) {
postMessage({
id: data.id,
body: reqData,
err: err && String(err)
});
}
try {
this.server.request(data.body, done);
}
catch(ex) {
done(ex);
}
};
var serverInstance;
onmessage = function(e) {
var data = e.data;
var method = serverInstance && serverInstance[data.type];
if (data.type === "init") {
serverInstance = new Server(data.body || {});
}
else if (typeof method === "function") {
method.apply(serverInstance, [data || {}]);
}
else if (serverInstance) {
throw new TypeError("Unknown message type: " + data.type);
}
};
var logger = {
log: function() {
postMessage({
type: "debug",
message: arguments
});
}
};
});