Skip to content

Commit 513900c

Browse files
committed
updates
1 parent 65b3058 commit 513900c

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

index.html

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@ <h2>Output:</h2>
5555
state.set('gist', parameters.get('gist'));
5656
} else {
5757
state.set('code', LZString.compressToBase64(editor.getValue()));
58+
state.set('mode', mode.value);
59+
state.set('features', [...features].join(','));
5860
}
59-
state.set('features', [...features].join(','));
60-
state.set('mode', mode.value);
6161
document.location.hash = state.toString();
6262
}
6363

6464
let featuresPopulated = false;
65-
const features = new Set(parameters.has('features') ? parameters.get('features').split(',') : []);
65+
let features = new Set(parameters.has('features') ? parameters.get('features').split(',') : []);
6666
let worker;
6767

6868
if (parameters.has('mode')) {
@@ -158,16 +158,12 @@ <h2>Output:</h2>
158158
fetch(`https://api.github.com/gists/${parameters.get('gist')}`)
159159
.then((r) => r.json())
160160
.then((data) => {
161-
const fileName = Object.keys(data.files)[0];
162-
const file = data.files[fileName];
163-
editor.setValue(file.content);
164-
if (fileName.endsWith('.js')) {
165-
mode.value = 'script';
166-
} else if (fileName.endsWith('.mjs')) {
167-
mode.value = 'module';
168-
}
169-
saveState();
161+
const state = JSON.parse(data.files['state.json'].content);
162+
mode.value = state.mode;
163+
features = new Set(state.features);
164+
const file = data.files[`code.${state.mode === 'script' ? 'js' : 'mjs'}`];
170165
respawn();
166+
editor.setValue(file.content);
171167
});
172168
} else {
173169
editor.setValue('print(\'Hello, World!\');');

server/index.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,19 @@ const EXTENSIONS = {
1111
__proto__: null,
1212
};
1313

14-
function createGist(content, mode) {
15-
const name = `code.${EXTENSIONS[mode]}`;
14+
function createGist(content, state) {
15+
const name = `code.${EXTENSIONS[state.mode]}`;
1616
return fetch('https://api.github.com/gists', {
1717
method: 'POST',
1818
headers: {
1919
'Authorization': AUTH,
2020
'Content-Type': 'application/json',
2121
},
2222
body: JSON.stringify({
23-
files: { [name]: { content } },
23+
files: {
24+
[name]: { content },
25+
'state.json': { content: JSON.stringify(state) },
26+
},
2427
description: 'Code shared from https://engine262.js.org',
2528
public: false,
2629
}),
@@ -49,12 +52,14 @@ const server = http.createServer((req, res) => {
4952
body += chunk;
5053
});
5154
req.on('end', () => {
52-
const { content, mode } = JSON.parse(body);
53-
createGist(content, mode)
55+
Promise.resolve()
56+
.then(() => JSON.parse(body))
57+
.then(({ content, state }) => createGist(content, state))
5458
.then((data) => {
5559
res.writeHead(200, { 'Content-Type': 'application/json' });
5660
res.end(JSON.stringify(data));
57-
}, (e) => {
61+
})
62+
.catch((e) => {
5863
console.error(e); // eslint-disable-line no-console
5964
res.writeHead(500);
6065
res.end('500');

0 commit comments

Comments
 (0)