Skip to content

Commit cf25207

Browse files
committed
100% Hooks Coverage
1 parent 1285ea2 commit cf25207

File tree

11 files changed

+145
-19
lines changed

11 files changed

+145
-19
lines changed

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ coverage/
33
node_modules/
44
cjs/*
55
!cjs/package.json
6-
core.js
7-
core.js.map
6+
core.*
87
!docs/core.js
98
!docs/core.js.map
109
pyscript.js

core.js.map

Lines changed: 0 additions & 1 deletion
This file was deleted.

docs/core.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/core.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

esm/index.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,24 @@ const mo = new MutationObserver((records) => {
4040
if (node.nodeType === 1) {
4141
addAllListeners(node);
4242
if (selector && node.matches(selector)) handle(node);
43-
else {
44-
if (selector) $$(selector, node).forEach(handle);
45-
if (!CUSTOM_SELECTORS.length) continue;
46-
handleCustomType(node);
47-
$$(CUSTOM_SELECTORS.join(','), node).forEach(
48-
handleCustomType,
49-
);
50-
}
43+
else bootstrap(selector, node, true);
5144
}
5245
}
5346
/* c8 ignore stop */
5447
}
5548
});
5649

50+
/* c8 ignore start */
51+
const bootstrap = (selector, node, shouldHandle) => {
52+
if (selector) $$(selector, node).forEach(handle);
53+
selector = CUSTOM_SELECTORS.join(',');
54+
if (selector) {
55+
if (shouldHandle) handleCustomType(node);
56+
$$(selector, node).forEach(handleCustomType);
57+
}
58+
};
59+
/* c8 ignore stop */
60+
5761
const observe = (root) => {
5862
mo.observe(root, { childList: true, subtree: true, attributes: true });
5963
return root;
@@ -69,6 +73,5 @@ assign(Element.prototype, {
6973
// give 3rd party a chance to apply changes before this happens
7074
queueMicrotask(() => {
7175
addAllListeners(observe(document));
72-
const selector = selectors.join(',');
73-
if (selector) $$(selector, document).forEach(handle);
76+
bootstrap(selectors.join(','), document, false);
7477
});

esm/worker/_template.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ add('message', ({ data: { options, config: baseURL, code, hooks } }) => {
8080
const { beforeRun, beforeRunAsync, afterRun, afterRunAsync } =
8181
hooks;
8282

83-
const after = afterRun || afterRunAsync;
84-
const before = beforeRun || beforeRunAsync;
83+
const after = isAsync ? afterRunAsync : afterRun;
84+
const before = isAsync ? beforeRunAsync : beforeRun;
8585

8686
// append code that should be executed *after* first
8787
if (after) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,6 @@
7070
"coincident": "^0.11.6"
7171
},
7272
"worker": {
73-
"blob": "sha256-1DM1q+m8AQYdQnBem2ugixNUGX1u/ZO0A7a+evjdrIg="
73+
"blob": "sha256-ymu4meyO3/xEFoZ1GF534LI+VCPKr7ELxnXOkS5hEIQ="
7474
}
7575
}

test/integration.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
66
<title>polyscript integration tests</title>
77
</head>
8-
<body><ul><li><strong>micropython</strong><ul><li><a href="/test/integration/interpreter/micropython/bootstrap.html">bootstrap</a></li><li><a href="/test/integration/interpreter/micropython/config-object.html">config-object</a></li><li><a href="/test/integration/interpreter/micropython/fetch.html">fetch</a></li><li><a href="/test/integration/interpreter/micropython/no-type.html">no-type</a></li><li><a href="/test/integration/interpreter/micropython/worker-attribute.html">worker-attribute</a></li><li><a href="/test/integration/interpreter/micropython/worker-bad.html">worker-bad</a></li><li><a href="/test/integration/interpreter/micropython/worker-empty-attribute.html">worker-empty-attribute</a></li><li><a href="/test/integration/interpreter/micropython/worker-error.html">worker-error</a></li><li><a href="/test/integration/interpreter/micropython/worker-lua.html">worker-lua</a></li><li><a href="/test/integration/interpreter/micropython/worker-tag.html">worker-tag</a></li><li><a href="/test/integration/interpreter/micropython/worker-window.html">worker-window</a></li><li><a href="/test/integration/interpreter/micropython/worker.html">worker</a></li></ul><li><strong>pyodide</strong><ul><li><a href="/test/integration/interpreter/pyodide/bootstrap.html">bootstrap</a></li><li><a href="/test/integration/interpreter/pyodide/button.html">button</a></li><li><a href="/test/integration/interpreter/pyodide/fetch.html">fetch</a></li><li><a href="/test/integration/interpreter/pyodide/sync.html">sync</a></li><li><a href="/test/integration/interpreter/pyodide/worker-error.html">worker-error</a></li><li><a href="/test/integration/interpreter/pyodide/worker-transform.html">worker-transform</a></li><li><a href="/test/integration/interpreter/pyodide/worker.html">worker</a></li></ul><li><strong>ruby-wasm-wasi</strong><ul><li><a href="/test/integration/interpreter/ruby-wasm-wasi/bootstrap.html">bootstrap</a></li></ul><li><strong>wasmoon</strong><ul><li><a href="/test/integration/interpreter/wasmoon/bootstrap.html">bootstrap</a></li><li><a href="/test/integration/interpreter/wasmoon/worker.html">worker</a></li></ul></ul></body>
8+
<body><ul><li><strong>micropython</strong><ul><li><a href="/test/integration/interpreter/micropython/bootstrap.html">bootstrap</a></li><li><a href="/test/integration/interpreter/micropython/config-object.html">config-object</a></li><li><a href="/test/integration/interpreter/micropython/custom-hooks.html">custom-hooks</a></li><li><a href="/test/integration/interpreter/micropython/fetch.html">fetch</a></li><li><a href="/test/integration/interpreter/micropython/no-type.html">no-type</a></li><li><a href="/test/integration/interpreter/micropython/worker-attribute.html">worker-attribute</a></li><li><a href="/test/integration/interpreter/micropython/worker-bad.html">worker-bad</a></li><li><a href="/test/integration/interpreter/micropython/worker-empty-attribute.html">worker-empty-attribute</a></li><li><a href="/test/integration/interpreter/micropython/worker-error.html">worker-error</a></li><li><a href="/test/integration/interpreter/micropython/worker-lua.html">worker-lua</a></li><li><a href="/test/integration/interpreter/micropython/worker-tag.html">worker-tag</a></li><li><a href="/test/integration/interpreter/micropython/worker-window.html">worker-window</a></li><li><a href="/test/integration/interpreter/micropython/worker.html">worker</a></li></ul><li><strong>pyodide</strong><ul><li><a href="/test/integration/interpreter/pyodide/bootstrap.html">bootstrap</a></li><li><a href="/test/integration/interpreter/pyodide/button.html">button</a></li><li><a href="/test/integration/interpreter/pyodide/fetch.html">fetch</a></li><li><a href="/test/integration/interpreter/pyodide/sync.html">sync</a></li><li><a href="/test/integration/interpreter/pyodide/worker-error.html">worker-error</a></li><li><a href="/test/integration/interpreter/pyodide/worker-transform.html">worker-transform</a></li><li><a href="/test/integration/interpreter/pyodide/worker.html">worker</a></li></ul><li><strong>ruby-wasm-wasi</strong><ul><li><a href="/test/integration/interpreter/ruby-wasm-wasi/bootstrap.html">bootstrap</a></li></ul><li><strong>wasmoon</strong><ul><li><a href="/test/integration/interpreter/wasmoon/bootstrap.html">bootstrap</a></li><li><a href="/test/integration/interpreter/wasmoon/worker.html">worker</a></li></ul></ul></body>
99
</html>

test/integration/_shared.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,55 @@ exports.python = {
4343
await expect(logs[0]).toBe('hello from A');
4444
},
4545

46+
customHooks: ({ expect }, baseURL) => async ({ page }) => {
47+
// Test that a config passed as object works out of the box.
48+
const logs = [];
49+
page.on('console', msg => logs.push(msg.text()));
50+
await page.goto(`${baseURL}/custom-hooks.html`);
51+
await page.waitForSelector('html.done');
52+
await expect(logs.join(',')).toBe([
53+
'onInterpreterReady',
54+
'onBeforeRun',
55+
'script',
56+
'onAfterRun',
57+
58+
'onInterpreterReady',
59+
'onBeforeRunAsync',
60+
'script-async',
61+
'onAfterRunAsync',
62+
63+
'onWorkerReady',
64+
'codeBeforeRunWorker',
65+
'script-worker',
66+
'codeAfterRunWorker',
67+
68+
'onWorkerReady',
69+
'codeBeforeRunWorkerAsync',
70+
'script-async-worker',
71+
'codeAfterRunWorkerAsync',
72+
73+
'onInterpreterReady',
74+
'onBeforeRun',
75+
'mpy',
76+
'onAfterRun',
77+
78+
'onInterpreterReady',
79+
'onBeforeRunAsync',
80+
'mpy-async',
81+
'onAfterRunAsync',
82+
83+
'onWorkerReady',
84+
'codeBeforeRunWorker',
85+
'mpy-worker',
86+
'codeAfterRunWorker',
87+
88+
'onWorkerReady',
89+
'codeBeforeRunWorkerAsync',
90+
'mpy-async-worker',
91+
'codeAfterRunWorkerAsync',
92+
].join(','));
93+
},
94+
4695
disabledUntilReady: ({ expect }, baseURL) => async ({ page }) => {
4796
await page.goto(`${baseURL}/button.html`);
4897
await page.waitForSelector('button[disabled]');
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<style>mpy-script{display:none}</style>
7+
<script type="module">
8+
import { define } from '/core.js';
9+
10+
define('mpy', {
11+
interpreter: 'micropython',
12+
onInterpreterReady: ({ run, runAsync }, element) => {
13+
console.log('onInterpreterReady');
14+
dispatchEvent(new Event('mpy:ready'));
15+
const exec = element.hasAttribute('async') ? runAsync : run;
16+
exec(element.textContent.trim());
17+
},
18+
onWorkerReady: () => console.log('onWorkerReady'),
19+
onBeforeRun: () => console.log('onBeforeRun'),
20+
onBeforeRunAsync: () => console.log('onBeforeRunAsync'),
21+
onAfterRun: () => console.log('onAfterRun'),
22+
onAfterRunAsync: () => console.log('onAfterRunAsync'),
23+
codeBeforeRunWorker: () => 'print("codeBeforeRunWorker")',
24+
codeBeforeRunWorkerAsync: () => 'print("codeBeforeRunWorkerAsync")',
25+
codeAfterRunWorker: () => 'print("codeAfterRunWorker")',
26+
codeAfterRunWorkerAsync: () => 'print("codeAfterRunWorkerAsync")',
27+
});
28+
29+
const cases = [
30+
{ type: 'script' },
31+
{ type: 'script', async: true},
32+
{ type: 'script', worker: true },
33+
{ type: 'script', async: true, worker: true },
34+
{ type: 'mpy' },
35+
{ type: 'mpy', async: true},
36+
{ type: 'mpy', worker: true },
37+
{ type: 'mpy', async: true, worker: true },
38+
];
39+
40+
(function inject() {
41+
if (cases.length) {
42+
addEventListener(
43+
'mpy:ready',
44+
() => {
45+
// give enough time to run the `print` statement
46+
// then invoke the inject function again
47+
setTimeout(inject, 100);
48+
},
49+
{ once: true }
50+
);
51+
const current = cases.shift();
52+
let type = current.type;
53+
const isScript = type === 'script';
54+
const tag = document.createElement(isScript ? 'script' : type + '-script');
55+
if (isScript) tag.type = 'mpy';
56+
if (current.async) {
57+
type += '-async';
58+
tag.setAttribute('async', '');
59+
}
60+
if (current.worker) {
61+
type += '-worker';
62+
tag.setAttribute('worker', '');
63+
}
64+
tag.textContent = `print("${type}")`
65+
document.body.append(tag);
66+
}
67+
else {
68+
document.documentElement.classList.add('done');
69+
}
70+
})();
71+
</script>
72+
</head>
73+
<body></body>
74+
</html>

0 commit comments

Comments
 (0)