-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathpluginLoader.js
179 lines (139 loc) · 4.88 KB
/
pluginLoader.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
/**
* Interactive Linter Copyright (c) 2015 Miguel Castillo.
*
* Licensed under MIT
*/
define(function(require, exports, module){
"use strict";
var _ = brackets.getModule("thirdparty/lodash"),
Promise = require("libs/js/spromise"),
requireUID = 1;
function embeddedPluginLoader(pluginsMeta) {
if (!pluginsMeta.directories.length) {
return Promise.resolve();
}
var requirePlugin = requirejs.config({
"context": "interactive-linter-plugins-" + (requireUID++),
"paths": {
"text": "../libs/js/text",
"libs": "libs/js"
},
"baseUrl": pluginsMeta.path,
"packages": pluginsMeta.directories
});
// Api for plugin linters
function api(plugin) {
var _lint = plugin.lint;
function lint(text, settings) {
var results = _lint(text, settings,pluginsMeta);
if(results.promise !== undefined && results.promise !== null){
return results;
} else {
return Promise.resolve(results);
}
}
return {
lint: lint
};
}
return new Promise(function(resolve) {
requirePlugin(pluginsMeta.directories, function() {
var plugins = {};
_.toArray(arguments).forEach(function(plugin, index) {
plugin.name = pluginsMeta.directories[index];
plugins[plugin.name] = plugin;
// Add a lint interface that will be just posting a message
// to the worker thread
_.merge(plugin, api(plugin));
});
resolve(plugins);
});
});
}
function workerThreadPluginLoader(pluginsMeta) {
if (!pluginsMeta.directories.length) {
return Promise.resolve();
}
var currentRequest, lastMessage;
var pendingRequest = Promise.defer();
var worker = new Worker(module.uri.substring(0, module.uri.lastIndexOf("/")) + "/pluginWorker.js");
// Process worker thread messages
worker.onmessage = function onmessage(evt) {
var data = evt.data;
if (data.type === "debug") {
console.log(data);
return;
}
if (currentRequest.state() === "pending") {
currentRequest.resolve(data);
}
};
worker.onerror = function onerror(evt) {
console.error("error", evt);
};
function postMessage(message) {
lastMessage = message;
if (currentRequest && currentRequest.isPending()) {
pendingRequest.resolve(); // Skip whatever is pending and queue another request
return (pendingRequest = Promise.defer()).then(resolveRequest.bind(null, message));
}
else {
worker.postMessage(message);
return (currentRequest = Promise.defer()).then(resolveRequest.bind(null, message));
}
}
function resolveRequest(message, response) {
if (!response) {
return;
}
// If there is data pending, then send it
if (message !== lastMessage) {
console.log("send queued message");
currentRequest = pendingRequest;
worker.postMessage(message);
}
return response.data;
}
function loadPlugins(plugins) {
var plugin;
for (var iplugin in plugins) {
plugin = plugins[iplugin];
// Add a lint interface that will be just posting a message to the worker thread
_.merge(plugin, api(plugin));
}
return plugins;
}
// Api for plugin linters
function api(plugin) {
function lint(text, settings) {
return postMessage({
type: "lint",
data: {
name: plugin.name,
text: text,
settings: settings
}
})
.then(function(response) {
return response.result;
});
}
return {
lint: lint
};
}
// Send request to init
return postMessage({
"type": "init",
"data": {
"baseUrl": pluginsMeta.path,
"packages": pluginsMeta.directories
}
})
.then(loadPlugins);
}
return {
workerThreadPluginLoader: workerThreadPluginLoader,
embeddedPluginLoader: embeddedPluginLoader
};
});