forked from MiguelCastillo/Brackets-Ternific
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLocalServer.js
207 lines (169 loc) · 5.62 KB
/
LocalServer.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/**
* Ternific Copyright (c) 2014 Miguel Castillo.
*
* Licensed under MIT
*/
define(function(require, exports, module) {
"use strict";
var spromise = require("libs/js/spromise");
var TernDemo = require("TernDemo"),
Settings = require("Settings"),
globalSettings = require("text!./tern/.tern-project");
var projectSettings = Settings();
globalSettings = JSON.parse(globalSettings || "{}");
function getWorker( provider ) {
if ( getWorker.worker ) {
return getWorker.worker;
}
// Create worker thread to process tern requests.
var worker = getWorker.worker = new Worker( module.uri.substr(0, module.uri.lastIndexOf("/")) + "/TernWorker.js" );
var current = null,
pending = null;
worker.onmessage = function(e) {
var data = e.data;
// If tern requests a file, then we will load it and then send it back to
// tern as an addFile action.
if (data.type == "getFile") {
provider.getFile(data.name)
.done(function(file) {
worker.send({
type: "addFile",
text: file.content,
id: data.id
});
})
.fail(function(error) {
worker.send({
type: "addFile",
err: String(error),
id: data.id
});
});
}
else if (data.type == "debug") {
console.log(data.message);
}
else if (current) {
current.deferred.resolve(data.body);
}
};
worker.onerror = function(e) {
if ( current ) {
current.deferred.reject(e);
}
};
worker.send = function send(data, callback) {
// Some requests to tern dont need to be waited on... So, just send tern
// the message and exit out.
if (!callback) {
worker.postMessage(data);
return;
}
// New request
var request = {
data: data,
deferred: spromise.defer()
};
if (!current) {
current = request;
worker.postMessage(request.data);
}
else {
if (pending) {
// Resolve with null to trigger a cancellation... This happens when we
// can safely skip requests before they are sent for processing to tern.
pending.deferred.resolve(null);
}
pending = request;
}
return request.deferred.done(processResponse);
};
function processResponse(response) {
if (response !== null) {
current = pending;
if (pending) {
pending = null;
worker.postMessage(current.data);
}
}
}
return worker;
}
function init(provider) {
var worker = getWorker(provider);
var instance = {
send: worker.send,
loadSettings: LocalServer.loadSettings,
clear: function() {
worker.send({
type: "clear"
});
},
addFile: function(name, text) {
worker.send({
type: "addFile",
name: name,
text: text
});
},
deleteFile: function(name) {
worker.send({
type: "deleteFile",
name: name
});
},
request: function(body) {
return worker.send({
type: "request",
body: body
}, true);
},
};
// Init with empty settings. Until a document is open that can give us
// some context...
worker.send({
type: "init",
body: {}
});
// Call TernDemo and extend the instance.
return $.extend(instance, TernDemo(instance));
}
function loadSettings(settings) {
var worker = getWorker(LocalServer.provider);
settings = settings || $.extend({}, globalSettings);
if (settings === currentSettings) {
return;
}
currentSettings = settings;
settings.libs = $.map(settings.libs || [], function(item) {
return "text!./tern/defs/" + item + ".json";
});
require(settings.libs, function() {
settings.defs = $.map(arguments, function(item) {
return JSON.parse(item);
});
// Post an init message to load settings
worker.send({
type: "init",
body: settings
});
});
}
var currentSettings;
LocalServer.loadSettings = function(fullPath) {
projectSettings
.load(".tern-project", fullPath)
.done(loadSettings)
.fail(function() {loadSettings();});
};
/**
* Bridge to communicate into tern worker thread.
*/
function LocalServer(provider) {
LocalServer.provider = provider;
return spromise(function(resolve) {
resolve(init(provider));
});
}
return LocalServer;
});