forked from MiguelCastillo/Brackets-Ternific
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTernWorker.js
138 lines (104 loc) · 2.77 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
/**
* Ternific Copyright (c) 2014 Miguel Castillo.
*
* Licensed under MIT
*/
importScripts("tern/node_modules/acorn/acorn.js",
"tern/node_modules/acorn/acorn_loose.js",
"tern/node_modules/acorn/util/walk.js",
"tern/lib/signal.js",
"tern/lib/tern.js",
"tern/lib/def.js",
"tern/lib/infer.js",
"tern/lib/comment.js");
function Extender(/*target, [source]+ */) {
var sources = Array.prototype.slice.call(arguments),
target = sources.shift();
for ( var source in sources ) {
source = sources[source];
// Copy properties
for (var property in source) {
target[property] = source[property];
}
}
return target;
}
function LoadPlugins(settings) {
var plugins = [];
for ( var i in settings.plugins ) {
plugins.push("tern/plugin/" + i + ".js");
}
// Import plugins
importScripts.apply((void 0), plugins);
}
function Server(settings) {
var _self = this, nextId = 1;
_self.pending = {};
LoadPlugins(settings);
Extender(settings, {
getFile: function(file, c) {
_self.pending[nextId] = c;
postMessage({
type: "getFile",
name: file,
id: nextId
});
++nextId;
}
});
// Instantiate tern server.
_self.server = new tern.Server(settings);
postMessage({
type:"ready"
});
}
Server.prototype.clear = function() {
this.server.reset();
this.server.files = [];
};
Server.prototype.addFile = function(data) {
var _self = this;
if (data.id && _self.pending[data.id]){
var c = _self.pending[data.id];
delete _self.pending[data.id];
c(data.err, data.text);
}
else {
_self.server.addFile(data.name, data.text);
}
};
Server.prototype.deleteFile = function(data) {
this.server.delFile(data);
};
Server.prototype.request = function(data) {
this.server.request(data.body, function(err, reqData) {
postMessage({
id: data.id,
body: reqData,
err: err && String(err)
});
});
};
onmessage = function(e) {
var data = e.data;
var method = Server.instance && Server.instance[data.type];
if ( data.type === "init" ) {
Server.instance = new Server(data.body || {});
return;
}
if ( typeof method === 'function' ) {
return method.apply(Server.instance, [data || {}]);
}
switch (data.type) {
default:
throw new Error("Unknown message type: " + data.type);
}
};
var console = {
log: function(v) {
postMessage({
type: "debug",
message: arguments
});
}
};