Skip to content

Commit ab85c31

Browse files
committed
load wasm only when running code
1 parent 3624847 commit ab85c31

1 file changed

Lines changed: 54 additions & 39 deletions

File tree

js/interactive-examples.js

Lines changed: 54 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,77 @@
11
import phpBinary from "/js/php-web.mjs";
22

33
function createOutput(output) {
4-
const container = document.createElement('div');
5-
container.classList.add('screen', 'example-contents');
6-
const title = document.createElement('p');
7-
title.innerText = 'Output: ';
8-
container.appendChild(title)
9-
const div = document.createElement('div');
10-
div.classList.add('examplescode');
4+
const container = document.createElement("div");
5+
container.classList.add("screen", "example-contents");
6+
const title = document.createElement("p");
7+
title.innerText = "Output: ";
8+
container.appendChild(title);
9+
const div = document.createElement("div");
10+
div.classList.add("examplescode");
1111
container.appendChild(div);
12-
const pre = document.createElement('pre');
13-
pre.classList.add('examplescode');
12+
const pre = document.createElement("pre");
13+
pre.classList.add("examplescode");
1414
pre.innerText = output;
15-
div.appendChild(pre)
15+
div.appendChild(pre);
1616
return container;
1717
}
1818

19-
async function main() {
20-
const buffer = [];
21-
const { ccall } = await phpBinary({
22-
print(data) {
23-
if (!data) {
24-
return;
25-
}
26-
27-
if (buffer.length) {
28-
buffer.push('\n');
29-
}
30-
buffer.push(data);
19+
class PHP {
20+
static buffer = [];
21+
static runPhp = null;
22+
static async loadPhp() {
23+
if (PHP.runPhp) {
24+
return PHP.runPhp;
3125
}
32-
})
3326

34-
console.log("PHP wasm %s loaded.", ccall("phpw_exec", "string", ["string"], ["phpversion();"]));
35-
let lastOutput = null
27+
const { ccall } = await phpBinary({
28+
print(data) {
29+
if (!data) {
30+
return;
31+
}
32+
33+
if (PHP.buffer.length) {
34+
PHP.buffer.push("\n");
35+
}
36+
PHP.buffer.push(data);
37+
},
38+
});
3639

37-
document.querySelectorAll('.example').forEach((example) => {
38-
const button = document.createElement('button');
39-
button.setAttribute('type', 'button')
40-
const phpcode = example.querySelector('.phpcode');
40+
console.log(
41+
"PHP wasm %s loaded.",
42+
ccall("phpw_exec", "string", ["string"], ["phpversion();"]),
43+
);
44+
PHP.runPhp = (code) => ccall("phpw_run", null, ["string"], ["?>" + code]);
45+
return PHP.runPhp;
46+
}
47+
}
48+
49+
async function main() {
50+
let lastOutput = null;
4151

42-
const code = phpcode.querySelector('pre code')
43-
code.setAttribute('contentEditable', true)
52+
document.querySelectorAll(".example").forEach((example) => {
53+
const button = document.createElement("button");
54+
button.setAttribute("type", "button");
55+
const phpcode = example.querySelector(".phpcode");
4456

45-
button.innerText = 'Run code';
46-
button.onclick = function() {
57+
const code = phpcode.querySelector("code");
58+
code.setAttribute("contentEditable", true);
59+
60+
button.innerText = "Run code";
61+
button.onclick = async function () {
4762
if (lastOutput && lastOutput.parentNode) {
48-
lastOutput.remove()
63+
lastOutput.remove();
4964
}
5065

51-
ccall("phpw_run", null, ["string"], ['?>' + phpcode.innerText]);
52-
lastOutput = createOutput(buffer.join(''))
66+
const runPhp = await PHP.loadPhp();
67+
runPhp(phpcode.innerText);
68+
lastOutput = createOutput(PHP.buffer.join(""));
5369
phpcode.parentNode.appendChild(lastOutput);
54-
buffer.length = 0;
70+
PHP.buffer.length = 0;
5571
};
5672

5773
phpcode.before(button);
5874
});
59-
6075
}
6176

62-
main()
77+
main();

0 commit comments

Comments
 (0)