-
Notifications
You must be signed in to change notification settings - Fork 20
/
LocalServer.js
152 lines (115 loc) · 3.95 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
/**
* Ternific Copyright (c) 2014 Miguel Castillo.
*
* Licensed under MIT
*/
define(function(require, exports, module) {
"use strict";
var _ = brackets.getModule("thirdparty/lodash");
var extensionUtils = brackets.getModule("utils/ExtensionUtils");
var Promise = require("node_modules/spromise/dist/spromise.min");
var workerFactory = require("workerFactory");
var TernApi = require("TernApi");
var Settings = require("Settings");
var globalSettings = Settings.create(".tern-project", false);
var projectSettings = Settings.create(".tern-project");
var TERN_ROOT = "node_modules/tern/";
var TERN_DEFINITIONS = TERN_ROOT + "defs/";
var WORKER_SCRIPT = extensionUtils.getModulePath(module, "TernWorker.js");
// Initialize global settings
globalSettings.load(extensionUtils.getModulePath(module));
function parseJSON(data) {
try {
return JSON.parse(data);
}
catch(ex) {
return undefined;
}
}
/**
* Bridge to communicate into tern worker thread.
*/
function LocalServer(documenProvider) {
this.settings = {};
this.documenProvider = documenProvider;
this.worker = workerFactory.create(this, WORKER_SCRIPT);
// Call tern api to set the rest of the stuff up.
TernApi.call(this);
var processSettings = createSettingsProcessor(this, globalSettings, projectSettings);
globalSettings.on("change", processSettings);
projectSettings.on("change", processSettings);
processSettings();
}
LocalServer.prototype = new TernApi();
LocalServer.prototype.constructor = LocalServer;
LocalServer.prototype.request = function(body) {
return this.worker.deferredSend({
type: "request",
body: body
}, true);
};
LocalServer.prototype.clear = function() {
this.worker.send({
type: "clear"
});
};
LocalServer.prototype.addFile = function(name, text) {
this.worker.send({
type: "addFile",
name: name,
text: text
});
};
LocalServer.prototype.deleteFile = function(name) {
this.worker.send({
type: "deleteFile",
name: name
});
};
LocalServer.prototype.loadSettings = function(fullPath) {
projectSettings.load(fullPath);
};
LocalServer.create = function(provider) {
return new Promise(function(resolve) {
var localServer = new LocalServer(provider);
initialize(localServer);
resolve(localServer);
});
};
function initialize(localServer, settings) {
localServer.worker.send({
type: "init",
body: settings
});
}
function createSettingsProcessor(localServer, global, project) {
return function() {
var settings = _.assign({}, global.data || {}, project.data || {});
getDefinitions(settings.libs).then(function(defs) {
settings.defs = defs;
initialize(localServer, settings);
});
};
}
function getDefinitions(libs) {
libs = _.toArray(libs).map(function(item) {
if (item.indexOf("/") !== -1 || item.indexOf("\\") !== -1) {
return "text!" + item + ".json";
}
return "text!" + TERN_DEFINITIONS + item + ".json";
});
return new Promise(function(resolve) {
require(libs, function() {
var defs = _.toArray(arguments).reduce(function(result, item) {
item = parseJSON(item);
if (item) {
result.push(item);
}
return result;
}, []);
resolve(defs);
});
});
}
return LocalServer;
});