Skip to content

Commit b468c2f

Browse files
committed
converted devtools/ to es5 syntax
1 parent e3bfbde commit b468c2f

File tree

2 files changed

+102
-94
lines changed

2 files changed

+102
-94
lines changed

devtools/regl_codegen/devtools.js

Lines changed: 60 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -37,35 +37,33 @@ var Tabs = {
3737

3838
// Plot a mock by name (without .json) to the default or specified container
3939
plotMock: function(mockName, id) {
40-
return new Promise(function (res, rej) {
41-
40+
return new Promise(function(res) {
4241
var mockURL = '/test/image/mocks/' + mockName + '.json';
4342

4443
console.warn('Plotting:', mockURL);
45-
44+
4645
var request = new XMLHttpRequest();
4746
request.open('GET', mockURL, true);
4847
request.responseType = '';
4948
request.send();
50-
49+
5150
request.onreadystatechange = function() {
5251
if(this.readyState === 4) {
5352
if(this.status === 200) {
5453
var fig = JSON.parse(this.responseText);
5554
var graphDiv = Tabs.fresh(id);
56-
55+
5756
Plotly.newPlot(graphDiv, fig);
58-
57+
5958
graphDiv.on('plotly_afterplot', function() {
6059
res(graphDiv);
6160
});
62-
6361
} else {
6462
console.error(this.statusText);
6563
}
6664
}
6765
};
68-
})
66+
});
6967
},
7068
};
7169

@@ -82,72 +80,81 @@ setInterval(function() {
8280

8381
var mocksList = document.getElementById('mocks-list');
8482

85-
async function handleOnLoad() {
83+
function handleOnLoad() {
8684
var mocksByReglTrace = {};
8785

88-
for (var trace of reglTraces) {
86+
reglTraces.forEach(function(trace) {
8987
mocksByReglTrace[trace] = [];
90-
for (var mock of mocks) {
91-
if (mock.keywords.indexOf(trace) !== -1) {
88+
mocks.forEach(function(mock) {
89+
if(mock.keywords.indexOf(trace) !== -1) {
9290
mocksByReglTrace[trace].push(mock);
9391
}
94-
}
95-
}
92+
});
93+
});
9694

97-
for (var trace of Object.keys(mocksByReglTrace)) {
95+
Object.keys(mocksByReglTrace).forEach(function(trace) {
9896
var thisMocks = mocksByReglTrace[trace];
9997
var div = document.createElement('div');
10098
div.className = 'mock-group';
10199
div.innerHTML = '<h3>' + trace + '</h3>';
102100
mocksList.appendChild(div);
103-
for (var mock of thisMocks) {
101+
thisMocks.forEach(function(mock) {
104102
var a = document.createElement('a');
105103
a.className = 'mock-link';
106104
a.innerHTML = mock.name;
107105
a.href = '#' + mock.name;
108106
a.onclick = function() {
109-
Tabs.plotMock(this.innerHTML)
107+
Tabs.plotMock(this.innerHTML);
110108
};
111109
div.appendChild(a);
112110
div.appendChild(document.createElement('br'));
113-
}
114-
}
111+
});
112+
});
115113

116114
// visit the mocks one by one.
117-
for (var trace of Object.keys(mocksByReglTrace)) {
118-
var thisMocks = mocksByReglTrace[trace];
119-
var generated = {}
120-
for (var mock of thisMocks) {
121-
var gd = await Tabs.plotMock(mock.name);
122-
var fullLayout = gd._fullLayout;
123-
fullLayout._glcanvas.each(function (d) {
124-
if (d.regl) {
125-
console.log("found regl", d.regl);
126-
var cachedCode = d.regl.getCachedCode();
127-
Object.entries(cachedCode).forEach(([hash, code]) => {
128-
generated[hash] = code.toString();
115+
return Object.keys(mocksByReglTrace).reduce(function(p, trace) {
116+
return p.then(function() {
117+
var thisMocks = mocksByReglTrace[trace];
118+
var generated = {};
119+
120+
return thisMocks.reduce(function(p, mock) {
121+
return p.then(function() {
122+
return Tabs.plotMock(mock.name).then(function(gd) {
123+
var fullLayout = gd._fullLayout;
124+
fullLayout._glcanvas.each(function(d) {
125+
if(d.regl) {
126+
console.log('found regl', d.regl);
127+
var cachedCode = d.regl.getCachedCode();
128+
Object.entries(cachedCode).forEach(function(kv) {
129+
generated[kv[0]] = kv[1].toString();
130+
});
131+
console.log('merging entries', Object.keys(cachedCode));
132+
}
133+
});
129134
});
130-
console.log("merging entries", Object.keys(cachedCode));
131-
}
132-
})
133-
}
134-
135-
console.log(window.__regl_codegen_cache);
136-
var body = JSON.stringify({
137-
generated,
138-
trace: trace
135+
});
136+
}, Promise.resolve())
137+
.then(function() {
138+
console.log(window.__regl_codegen_cache);
139+
var body = JSON.stringify({
140+
generated: generated,
141+
trace: trace
142+
});
143+
window.__regl_codegen_cache = {};
144+
return fetch('/api/submit-code', {
145+
method: 'POST',
146+
headers: {
147+
'Content-Type': 'application/json'
148+
},
149+
body: body
150+
});
151+
});
139152
});
140-
window.__regl_codegen_cache = {};
141-
await fetch("/api/submit-code", {
142-
method: "POST",
143-
headers: {
144-
"Content-Type": "application/json"
145-
},
146-
body
147-
});
148-
}
149-
150-
// close
151-
await fetch("/api/codegen-done");
152-
window.close();
153+
}, Promise.resolve())
154+
.then(function() {
155+
return fetch('/api/codegen-done');
156+
})
157+
.then(function() {
158+
window.close();
159+
});
153160
}

devtools/regl_codegen/server.js

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ var minimist = require('minimist');
99
var constants = require('../../tasks/util/constants');
1010
var makeWatchifiedBundle = require('../../tasks/util/watchified_bundle');
1111
var shortcutPaths = require('../../tasks/util/shortcut_paths');
12-
var { exit } = require('process');
1312

1413
var args = minimist(process.argv.slice(2), {});
1514
var PORT = args.port || 3000;
@@ -21,39 +20,39 @@ var static = ecstatic({
2120
cache: 0,
2221
gzip: true,
2322
cors: true
24-
})
23+
});
2524

2625
var tracesReceived = [];
2726

28-
var server = http.createServer(function (req, res) {
29-
if (req.method === 'POST' && req.url === '/api/submit-code') {
27+
var server = http.createServer(function(req, res) {
28+
if(req.method === 'POST' && req.url === '/api/submit-code') {
3029
var body = '';
31-
req.on('data', function (data) {
30+
req.on('data', function(data) {
3231
body += data;
3332
});
34-
req.on('end', function () {
33+
req.on('end', function() {
3534
var data = JSON.parse(body);
3635

3736
tracesReceived.push(data.trace);
3837
handleCodegen(data);
3938
res.statusCode = 200;
4039
res.end();
41-
})
42-
} else if (req.method === 'GET' && req.url === '/api/codegen-done') {
40+
});
41+
} else if(req.method === 'GET' && req.url === '/api/codegen-done') {
4342
console.log('Codegen complete');
4443
console.log('Traces received:', tracesReceived);
4544

4645
res.statusCode = 200;
4746
res.end();
48-
setTimeout(() => exit(), 1000);
47+
setTimeout(process.exit, 1000);
4948
} else {
5049
static(req, res);
5150
}
5251
});
5352

5453

5554
// Make watchified bundle for plotly.js
56-
var bundlePlotly = makeWatchifiedBundle(strict, function () {
55+
var bundlePlotly = makeWatchifiedBundle(strict, function() {
5756
// open up browser window on first bundle callback
5857
open('http://localhost:' + PORT + '/devtools/regl_codegen/index' + (strict ? '-strict' : '') + '.html');
5958
});
@@ -81,9 +80,9 @@ getMockFiles()
8180

8281

8382
function getMockFiles() {
84-
return new Promise(function (resolve, reject) {
85-
fs.readdir(constants.pathToTestImageMocks, function (err, files) {
86-
if (err) {
83+
return new Promise(function(resolve, reject) {
84+
fs.readdir(constants.pathToTestImageMocks, function(err, files) {
85+
if(err) {
8786
reject(err);
8887
} else {
8988
resolve(files);
@@ -93,26 +92,26 @@ function getMockFiles() {
9392
}
9493

9594
function getReglTraces() {
96-
return constants.allTraces.filter(function (trace) {
95+
return constants.allTraces.filter(function(trace) {
9796
var indexPath = constants.pathToSrc + '/traces/' + trace + '/index.js';
9897

9998
// get categories
10099
var indexContents = fs.readFileSync(indexPath, 'utf8');
101100
var categories = indexContents.match(/^\s*categories:\s*\[([^\]]+)\]/m);
102-
if (categories) {
103-
categories = categories[1].split(',').map(function (c) {
101+
if(categories) {
102+
categories = categories[1].split(',').map(function(c) {
104103
return c.trim().replace(/^['"]|['"]$/g, '');
105104
});
106105
}
107106

108-
if (categories && categories.indexOf('regl') !== -1) {
107+
if(categories && categories.indexOf('regl') !== -1) {
109108
return true;
110109
}
111-
})
110+
});
112111
}
113112

114113
function readFiles(files) {
115-
var promises = files.map(function (file) {
114+
var promises = files.map(function(file) {
116115
var filePath = path.join(constants.pathToTestImageMocks, file);
117116
return readFilePromise(filePath);
118117
});
@@ -122,18 +121,18 @@ function readFiles(files) {
122121

123122
function createMocksList(files) {
124123
// eliminate pollutants (e.g .DS_Store) that can accumulate in the mock directory
125-
var jsonFiles = files.filter(function (file) {
124+
var jsonFiles = files.filter(function(file) {
126125
return file.name.substr(-5) === '.json';
127126
});
128127

129-
var mocksList = jsonFiles.map(function (file) {
128+
var mocksList = jsonFiles.map(function(file) {
130129
var contents = JSON.parse(file.contents);
131130

132131
// get plot type keywords from mocks
133-
var types = contents.data.map(function (trace) {
132+
var types = contents.data.map(function(trace) {
134133
return trace.type || 'scatter';
135-
}).reduce(function (acc, type, i, arr) {
136-
if (arr.lastIndexOf(type) === i) {
134+
}).reduce(function(acc, type, i, arr) {
135+
if(arr.lastIndexOf(type) === i) {
137136
acc.push(type);
138137
}
139138
return acc;
@@ -166,9 +165,9 @@ function saveReglTracesToFile(traces) {
166165
}
167166

168167
function readFilePromise(file) {
169-
return new Promise(function (resolve, reject) {
170-
fs.readFile(file, { encoding: 'utf-8' }, function (err, contents) {
171-
if (err) {
168+
return new Promise(function(resolve, reject) {
169+
fs.readFile(file, { encoding: 'utf-8' }, function(err, contents) {
170+
if(err) {
172171
reject(err);
173172
} else {
174173
resolve({
@@ -181,9 +180,9 @@ function readFilePromise(file) {
181180
}
182181

183182
function writeFilePromise(path, contents) {
184-
return new Promise(function (resolve, reject) {
185-
fs.writeFile(path, contents, function (err) {
186-
if (err) {
183+
return new Promise(function(resolve, reject) {
184+
fs.writeFile(path, contents, function(err) {
185+
if(err) {
187186
reject(err);
188187
} else {
189188
resolve(path);
@@ -193,9 +192,9 @@ function writeFilePromise(path, contents) {
193192
}
194193

195194
function bundleDevtools() {
196-
return new Promise(function (resolve, reject) {
197-
devtools.bundle(function (err) {
198-
if (err) {
195+
return new Promise(function(resolve, reject) {
196+
devtools.bundle(function(err) {
197+
if(err) {
199198
console.error('Error while bundling!', JSON.stringify(String(err)));
200199
return reject(err);
201200
} else {
@@ -212,21 +211,23 @@ function handleCodegen(data) {
212211
var pathToReglCodegenSrc = constants.pathToReglCodegenSrc;
213212
var pathToReglPrecompiledSrc = path.join(constants.pathToSrc, 'traces', trace, 'regl_precompiled.js');
214213

215-
var header = "'use strict';\n";
214+
var header = '\'use strict\';\n';
216215
var imports = '';
217216
var exports = '\nmodule.exports = {\n';
218217
var varId = 0;
219218

220-
Object.entries(generated).forEach(function ([key, value], i) {
219+
Object.entries(generated).forEach(function(kv) {
220+
var key = kv[0];
221+
var value = kv[1];
221222
var filePath = path.join(pathToReglCodegenSrc, key);
222223
fs.writeFileSync(filePath, 'module.exports = ' + value);
223224

224-
imports += 'var v' + varId + " = require('../../" + path.join(constants.reglCodegenSubdir, key) + "');\n";
225-
exports += " '" + key + "': v" + varId + ',\n';
225+
imports += 'var v' + varId + ' = require(\'../../' + path.join(constants.reglCodegenSubdir, key) + '\');\n';
226+
exports += ' \'' + key + '\': v' + varId + ',\n';
226227
varId++;
227228
});
228229

229-
if (varId > 0) {
230+
if(varId > 0) {
230231
exports = exports.slice(0, -2) + '\n};\n';
231232
} else {
232233
exports = 'module.exports = {};\n';
@@ -241,12 +242,12 @@ function purgeGeneratedCode(traces) {
241242
var pathToReglCodegenSrc = constants.pathToReglCodegenSrc;
242243

243244
var files = fs.readdirSync(pathToReglCodegenSrc);
244-
files.forEach(function (file) {
245+
files.forEach(function(file) {
245246
fs.unlinkSync(path.join(pathToReglCodegenSrc, file));
246247
});
247248

248-
traces.forEach(function (trace) {
249+
traces.forEach(function(trace) {
249250
var pathToReglPrecompiledSrc = path.join(constants.pathToSrc, 'traces', trace, 'regl_precompiled.js');
250251
fs.writeFileSync(pathToReglPrecompiledSrc, 'module.exports = {};\n');
251252
});
252-
}
253+
}

0 commit comments

Comments
 (0)