debt - more promise cleanup

This commit is contained in:
Johannes Rieken 2019-02-08 15:19:17 +01:00
parent a404c91e22
commit 70e48eeb52
5 changed files with 386 additions and 392 deletions

View file

@ -60,7 +60,7 @@ monaco.editor.create(document.getElementById("container"), {
function xhr(url) { function xhr(url) {
var req = null; var req = null;
return new monaco.Promise(function(c,e,p) { return new Promise(function(c,e) {
req = new XMLHttpRequest(); req = new XMLHttpRequest();
req.onreadystatechange = function () { req.onreadystatechange = function () {
if (req._canceled) { return; } if (req._canceled) { return; }
@ -72,8 +72,6 @@ function xhr(url) {
e(req); e(req);
} }
req.onreadystatechange = function () { }; req.onreadystatechange = function () { };
} else {
p(req);
} }
}; };

View file

@ -1,40 +1,40 @@
/// <reference path="../node_modules/monaco-editor-core/monaco.d.ts" /> /// <reference path="../node_modules/monaco-editor-core/monaco.d.ts" />
define(['./samples-all.generated'], function(ALL_SAMPLES) { define(['./samples-all.generated'], function (ALL_SAMPLES) {
var XHR_SAMPLES = {}; var XHR_SAMPLES = {};
ALL_SAMPLES.forEach(function(sample) { ALL_SAMPLES.forEach(function (sample) {
XHR_SAMPLES[sample.name] = sample.content; XHR_SAMPLES[sample.name] = sample.content;
}); });
var samples = []; var samples = [];
var modesIds = monaco.languages.getLanguages().map(function(language) { return language.id; }); var modesIds = monaco.languages.getLanguages().map(function (language) { return language.id; });
modesIds.sort(); modesIds.sort();
modesIds.forEach(function(modeId) { modesIds.forEach(function (modeId) {
samples.push({ samples.push({
name: 'sample - ' + modeId, name: 'sample - ' + modeId,
mimeType: modeId, mimeType: modeId,
loadText: function() { loadText: function () {
return monaco.Promise.as(XHR_SAMPLES['sample.' + modeId + '.txt']); return Promise.resolve(XHR_SAMPLES['sample.' + modeId + '.txt']);
} }
}); });
}); });
function addXHRSample(name, modelUrl, mimeType, textModifier) { function addXHRSample(name, modelUrl, mimeType, textModifier) {
textModifier = textModifier || function(text) { return text; }; textModifier = textModifier || function (text) { return text; };
samples.push({ samples.push({
name: name, name: name,
mimeType: mimeType, mimeType: mimeType,
loadText: function() { loadText: function () {
return monaco.Promise.as(XHR_SAMPLES[modelUrl]).then(textModifier); return Promise.resolve(XHR_SAMPLES[modelUrl]).then(textModifier);
} }
}); });
} }
function addStringPowerXHRSample(name, modelUrl, mimeType, power) { function addStringPowerXHRSample(name, modelUrl, mimeType, power) {
addXHRSample(name, modelUrl, mimeType, function(text) { addXHRSample(name, modelUrl, mimeType, function (text) {
var result = text; var result = text;
for (var i = 0; i < power; ++i) { for (var i = 0; i < power; ++i) {
result += "\n" + result; result += "\n" + result;
@ -47,8 +47,8 @@ define(['./samples-all.generated'], function(ALL_SAMPLES) {
samples.push({ samples.push({
name: name, name: name,
mimeType: mimeType, mimeType: mimeType,
loadText: function() { loadText: function () {
return monaco.Promise.as(modelText); return Promise.resolve(modelText);
} }
}); });
} }
@ -65,7 +65,7 @@ define(['./samples-all.generated'], function(ALL_SAMPLES) {
addXHRSample('Z___jquery-min.js', 'run-editor-jquery-min-js.txt', 'text/javascript'); addXHRSample('Z___jquery-min.js', 'run-editor-jquery-min-js.txt', 'text/javascript');
addXHRSample('Z___scrolling-strategy.js', 'run-editor-sample-js.txt', 'text/plain', function(text) { addXHRSample('Z___scrolling-strategy.js', 'run-editor-sample-js.txt', 'text/plain', function (text) {
console.log('here I am'); console.log('here I am');
var lines = text.split('\n'); var lines = text.split('\n');
var newLines = lines.slice(0); var newLines = lines.slice(0);
@ -98,7 +98,7 @@ define(['./samples-all.generated'], function(ALL_SAMPLES) {
'\uDBFF\uDFFF' '\uDBFF\uDFFF'
].join('\n')); ].join('\n'));
addSample('Z___easy-debug.js', 'text/plain', (function() { addSample('Z___easy-debug.js', 'text/plain', (function () {
var myValue = "Line1"; var myValue = "Line1";
for (var i = 2; i < 50; i++) { for (var i = 2; i < 50; i++) {
myValue += "\nLine" + i; myValue += "\nLine" + i;
@ -106,7 +106,7 @@ define(['./samples-all.generated'], function(ALL_SAMPLES) {
return myValue; return myValue;
})()); })());
addSample('Z___copy-paste.txt', 'text/plain', (function() { addSample('Z___copy-paste.txt', 'text/plain', (function () {
var i = 0, sampleCopyPasteLine = ''; var i = 0, sampleCopyPasteLine = '';
while (sampleCopyPasteLine.length < 1000) { while (sampleCopyPasteLine.length < 1000) {
i++; i++;
@ -119,7 +119,7 @@ define(['./samples-all.generated'], function(ALL_SAMPLES) {
return sampleCopyPaste; return sampleCopyPaste;
})()); })());
addSample('Z___xss', 'text/html', (function() { addSample('Z___xss', 'text/html', (function () {
var xssRepresentations = [ var xssRepresentations = [
'<', '<',
'BAD\u2028CHARACTER', 'BAD\u2028CHARACTER',
@ -196,7 +196,7 @@ define(['./samples-all.generated'], function(ALL_SAMPLES) {
return xssRepresentations.length + ':\n' + xssRepresentations.join('\n'); return xssRepresentations.length + ':\n' + xssRepresentations.join('\n');
})()); })());
addSample('Z___many-links.js', 'text/javascript', (function() { addSample('Z___many-links.js', 'text/javascript', (function () {
var result = "bla bla a url: https://microsoft.com some more bla bla"; var result = "bla bla a url: https://microsoft.com some more bla bla";
for (var i = 0; i < 13; ++i) { for (var i = 0; i < 13; ++i) {
result += "\n" + result; result += "\n" + result;
@ -204,7 +204,7 @@ define(['./samples-all.generated'], function(ALL_SAMPLES) {
return "/*" + result + "\n*/"; return "/*" + result + "\n*/";
})()); })());
addSample('Z___line-separators.js', 'text/javascript', (function() { addSample('Z___line-separators.js', 'text/javascript', (function () {
return [ return [
"var x = '1'; // And\u2028 here I have a nice comment.", "var x = '1'; // And\u2028 here I have a nice comment.",
"", "",
@ -216,7 +216,7 @@ define(['./samples-all.generated'], function(ALL_SAMPLES) {
addXHRSample('Z___intellisense.js', 'run-editor-intellisense-js.txt', 'text/javascript'); addXHRSample('Z___intellisense.js', 'run-editor-intellisense-js.txt', 'text/javascript');
addSample('Z___recursion attack', 'text/html', (function() { addSample('Z___recursion attack', 'text/html', (function () {
var arr = []; var arr = [];
for (var i = 0; i < 10000; i++) { for (var i = 0; i < 10000; i++) {
arr.push('\n<script type="text/html">'); arr.push('\n<script type="text/html">');
@ -238,7 +238,7 @@ define(['./samples-all.generated'], function(ALL_SAMPLES) {
} }
}); });
addXHRSample('Z___f12___css','run-editor-sample-f12-css.txt','text/css'); addXHRSample('Z___f12___css', 'run-editor-sample-f12-css.txt', 'text/css');
return samples; return samples;
}); });

View file

@ -69,7 +69,7 @@
} }
require(['./samples'], function(SAMPLES) { require(['./samples'], function(SAMPLES) {
monaco.Promise.join( Promise.all(
SAMPLES.map(resolveSample) SAMPLES.map(resolveSample)
).then(function(samples) { ).then(function(samples) {
renderSamples(samples); renderSamples(samples);

View file

@ -2,8 +2,8 @@
monaco.languages.register({ id: 'mySpecialLanguage' }); monaco.languages.register({ id: 'mySpecialLanguage' });
monaco.languages.registerHoverProvider('mySpecialLanguage', { monaco.languages.registerHoverProvider('mySpecialLanguage', {
provideHover: function(model, position) { provideHover: function (model, position) {
return xhr('../playground.html').then(function(res) { return xhr('../playground.html').then(function (res) {
return { return {
range: new monaco.Range(1, 1, model.getLineCount(), model.getLineMaxColumn(model.getLineCount())), range: new monaco.Range(1, 1, model.getLineCount(), model.getLineMaxColumn(model.getLineCount())),
contents: [ contents: [
@ -22,7 +22,7 @@ monaco.editor.create(document.getElementById("container"), {
function xhr(url) { function xhr(url) {
var req = null; var req = null;
return new monaco.Promise(function(c,e,p) { return new Promise(function (c, e) {
req = new XMLHttpRequest(); req = new XMLHttpRequest();
req.onreadystatechange = function () { req.onreadystatechange = function () {
if (req._canceled) { return; } if (req._canceled) { return; }
@ -34,12 +34,10 @@ function xhr(url) {
e(req); e(req);
} }
req.onreadystatechange = function () { }; req.onreadystatechange = function () { };
} else {
p(req);
} }
}; };
req.open("GET", url, true ); req.open("GET", url, true);
req.responseType = ""; req.responseType = "";
req.send(null); req.send(null);

View file

@ -1,12 +1,12 @@
/// <reference path="../../release/monaco.d.ts" /> /// <reference path="../../release/monaco.d.ts" />
(function() { (function () {
'use strict'; 'use strict';
window.onload = function() { window.onload = function () {
require(['vs/editor/editor.main'], function() { require(['vs/editor/editor.main'], function () {
xhr('playground/monaco.d.ts.txt').then(function(response) { xhr('playground/monaco.d.ts.txt').then(function (response) {
monaco.languages.typescript.javascriptDefaults.addExtraLib(response.responseText, 'monaco.d.ts'); monaco.languages.typescript.javascriptDefaults.addExtraLib(response.responseText, 'monaco.d.ts');
monaco.languages.typescript.javascriptDefaults.addExtraLib([ monaco.languages.typescript.javascriptDefaults.addExtraLib([
'declare var require: {', 'declare var require: {',
@ -24,10 +24,10 @@ window.onload = function() {
load(); load();
}); });
}; };
var editor = null; var editor = null;
var data = { var data = {
js: { js: {
model: null, model: null,
state: null state: null
@ -40,9 +40,9 @@ var data = {
model: null, model: null,
state: null state: null
} }
}; };
function load() { function load() {
function layout() { function layout() {
var GLOBAL_PADDING = 20; var GLOBAL_PADDING = 20;
@ -135,32 +135,32 @@ function load() {
var typingContainer = document.createElement('div'); var typingContainer = document.createElement('div');
typingContainer.className = 'typingContainer'; typingContainer.className = 'typingContainer';
var tabArea = (function() { var tabArea = (function () {
var tabArea = document.createElement('div'); var tabArea = document.createElement('div');
tabArea.className = 'tabArea'; tabArea.className = 'tabArea';
var jsTab = document.createElement('span'); var jsTab = document.createElement('span');
jsTab.className = 'tab active'; jsTab.className = 'tab active';
jsTab.appendChild(document.createTextNode('JavaScript')); jsTab.appendChild(document.createTextNode('JavaScript'));
jsTab.onclick = function() { changeTab(jsTab, 'js'); }; jsTab.onclick = function () { changeTab(jsTab, 'js'); };
tabArea.appendChild(jsTab); tabArea.appendChild(jsTab);
var cssTab = document.createElement('span'); var cssTab = document.createElement('span');
cssTab.className = 'tab'; cssTab.className = 'tab';
cssTab.appendChild(document.createTextNode('CSS')); cssTab.appendChild(document.createTextNode('CSS'));
cssTab.onclick = function() { changeTab(cssTab, 'css'); }; cssTab.onclick = function () { changeTab(cssTab, 'css'); };
tabArea.appendChild(cssTab); tabArea.appendChild(cssTab);
var htmlTab = document.createElement('span'); var htmlTab = document.createElement('span');
htmlTab.className = 'tab'; htmlTab.className = 'tab';
htmlTab.appendChild(document.createTextNode('HTML')); htmlTab.appendChild(document.createTextNode('HTML'));
htmlTab.onclick = function() { changeTab(htmlTab, 'html'); }; htmlTab.onclick = function () { changeTab(htmlTab, 'html'); };
tabArea.appendChild(htmlTab); tabArea.appendChild(htmlTab);
var runBtn = document.createElement('span'); var runBtn = document.createElement('span');
runBtn.className = 'action run'; runBtn.className = 'action run';
runBtn.appendChild(document.createTextNode('Run')); runBtn.appendChild(document.createTextNode('Run'));
runBtn.onclick = function() { run(); }; runBtn.onclick = function () { run(); };
tabArea.appendChild(runBtn); tabArea.appendChild(runBtn);
return tabArea; return tabArea;
@ -224,10 +224,10 @@ function load() {
samplePath = 'playground/new-samples/' + samplePath; samplePath = 'playground/new-samples/' + samplePath;
var js = xhr(samplePath + '/sample.js').then(function(response) { return response.responseText}); var js = xhr(samplePath + '/sample.js').then(function (response) { return response.responseText });
var css = xhr(samplePath + '/sample.css').then(function(response) { return response.responseText}); var css = xhr(samplePath + '/sample.css').then(function (response) { return response.responseText });
var html = xhr(samplePath + '/sample.html').then(function(response) { return response.responseText}); var html = xhr(samplePath + '/sample.html').then(function (response) { return response.responseText });
monaco.Promise.join([js, css, html]).then(function(_) { Promise.all([js, css, html]).then(function (_) {
var js = _[0]; var js = _[0];
var css = _[1]; var css = _[1];
var html = _[2]; var html = _[2];
@ -238,12 +238,12 @@ function load() {
html: html html: html
}); });
return callback(null, findLoadedSample(sampleId)); return callback(null, findLoadedSample(sampleId));
}, function(err) { }, function (err) {
callback(err, null); callback(err, null);
}); });
} }
sampleSwitcher.onchange = function() { sampleSwitcher.onchange = function () {
var sampleId = sampleSwitcher.options[sampleSwitcher.selectedIndex].value; var sampleId = sampleSwitcher.options[sampleSwitcher.selectedIndex].value;
window.location.hash = sampleId; window.location.hash = sampleId;
}; };
@ -286,7 +286,7 @@ function load() {
} }
var myToken = (++currentToken); var myToken = (++currentToken);
loadSample(sampleId, function(err, sample) { loadSample(sampleId, function (err, sample) {
if (err) { if (err) {
alert('Sample not found! ' + err.message); alert('Sample not found! ' + err.message);
return; return;
@ -307,10 +307,10 @@ function load() {
function run() { function run() {
doRun(runContainer); doRun(runContainer);
} }
} }
var runIframe = null, runIframeHeight = 0; var runIframe = null, runIframeHeight = 0;
function doRun(runContainer) { function doRun(runContainer) {
if (runIframe) { if (runIframe) {
// Unload old iframe // Unload old iframe
runContainer.removeChild(runIframe); runContainer.removeChild(runIframe);
@ -328,35 +328,35 @@ function doRun(runContainer) {
runIframe.frameborder = '0'; runIframe.frameborder = '0';
runContainer.appendChild(runIframe); runContainer.appendChild(runIframe);
var getLang = function(lang) { var getLang = function (lang) {
return data[lang].model.getValue(); return data[lang].model.getValue();
}; };
runIframe.addEventListener('load', function(e) { runIframe.addEventListener('load', function (e) {
runIframe.contentWindow.load(getLang('js'), getLang('html'), getLang('css')); runIframe.contentWindow.load(getLang('js'), getLang('html'), getLang('css'));
}); });
} }
var preloaded = {}; var preloaded = {};
(function() { (function () {
var elements = Array.prototype.slice.call(document.querySelectorAll('pre[data-preload]'), 0); var elements = Array.prototype.slice.call(document.querySelectorAll('pre[data-preload]'), 0);
elements.forEach(function(el) { elements.forEach(function (el) {
var path = el.getAttribute('data-preload'); var path = el.getAttribute('data-preload');
preloaded[path] = el.innerText || el.textContent; preloaded[path] = el.innerText || el.textContent;
el.parentNode.removeChild(el); el.parentNode.removeChild(el);
}); });
})(); })();
function xhr(url) { function xhr(url) {
if (preloaded[url]) { if (preloaded[url]) {
return monaco.Promise.as({ return Promise.resolve({
responseText: preloaded[url] responseText: preloaded[url]
}); });
} }
var req = null; var req = null;
return new monaco.Promise(function(c,e,p) { return new Promise(function (c, e) {
req = new XMLHttpRequest(); req = new XMLHttpRequest();
req.onreadystatechange = function () { req.onreadystatechange = function () {
if (req._canceled) { return; } if (req._canceled) { return; }
@ -368,12 +368,10 @@ function xhr(url) {
e(req); e(req);
} }
req.onreadystatechange = function () { }; req.onreadystatechange = function () { };
} else {
p(req);
} }
}; };
req.open("GET", url, true ); req.open("GET", url, true);
req.responseType = ""; req.responseType = "";
req.send(null); req.send(null);
@ -381,6 +379,6 @@ function xhr(url) {
req._canceled = true; req._canceled = true;
req.abort(); req.abort();
}); });
} }
})(); })();