Merge pull request #754 from mattico/playpen-api

Use stable rust playground API
This commit is contained in:
Matt Ickstadt 2018-08-01 15:22:06 -05:00 committed by GitHub
commit 436c084b9e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 15 deletions

View File

@ -19,9 +19,16 @@ function playpen_text(playpen) {
// Hide Rust code lines prepended with a specific character // Hide Rust code lines prepended with a specific character
var hiding_character = "#"; var hiding_character = "#";
function fetch_with_timeout(url, options, timeout = 6000) {
return Promise.race([
fetch(url, options),
new Promise((_, reject) => setTimeout(() => reject(new Error('timeout')), timeout))
]);
}
var playpens = Array.from(document.querySelectorAll(".playpen")); var playpens = Array.from(document.querySelectorAll(".playpen"));
if (playpens.length > 0) { if (playpens.length > 0) {
fetch("https://play.rust-lang.org/meta/crates", { fetch_with_timeout("https://play.rust-lang.org/meta/crates", {
headers: { headers: {
'Content-Type': "application/json", 'Content-Type': "application/json",
}, },
@ -96,33 +103,28 @@ function playpen_text(playpen) {
let text = playpen_text(code_block); let text = playpen_text(code_block);
var params = { var params = {
channel: "stable", version: "stable",
mode: "debug", optimize: "0",
crateType: "bin", code: text
tests: false,
code: text,
backtrace: false,
}; };
if (text.indexOf("#![feature") !== -1) { if (text.indexOf("#![feature") !== -1) {
params.channel = "nightly"; params.version = "nightly";
} }
result_block.innerText = "Running..."; result_block.innerText = "Running...";
var request = fetch("https://play.rust-lang.org/execute", { fetch_with_timeout("https://play.rust-lang.org/evaluate.json", {
headers: { headers: {
'Content-Type': "application/json", 'Content-Type': "application/json",
}, },
method: 'POST', method: 'POST',
mode: 'cors', mode: 'cors',
body: JSON.stringify(params) body: JSON.stringify(params)
}); })
.then(response => response.json())
request .then(response => result_block.innerText = response.result)
.then(function (response) { return response.json(); }) .catch(error => result_block.innerText = "Playground Communication: " + error.message);
.then(function (response) { result_block.innerText = response.success ? response.stdout : response.stderr; })
.catch(function (error) { result_block.innerText = "Playground communication" + error.message; });
} }
// Syntax highlighting Configuration // Syntax highlighting Configuration