Skip to content

Commit 32b920c

Browse files
Merge pull request #112 from pyscript/async-by-default
Make interpreters code async by default
2 parents 98813a8 + ecd6888 commit 32b920c

File tree

10 files changed

+36
-26
lines changed

10 files changed

+36
-26
lines changed

docs/index.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/index.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

esm/custom.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import '@ungap/with-resolvers';
22
import { $$ } from 'basic-devtools';
33

4-
import { JSModules, assign, create, createOverload, createResolved, dedent, defineProperty, nodeInfo, registerJSModules } from './utils.js';
4+
import { JSModules, isSync, assign, create, createOverload, createResolved, dedent, defineProperty, nodeInfo, registerJSModules } from './utils.js';
55
import { getDetails } from './script-handler.js';
66
import { registry as defaultRegistry, prefixes, configs } from './interpreters.js';
77
import { getRuntimeID } from './loader.js';
@@ -69,7 +69,7 @@ export const handleCustomType = async (node) => {
6969
type: runtime,
7070
custom: type,
7171
config: node.getAttribute('config') || config || {},
72-
async: node.hasAttribute('async'),
72+
async: !isSync(node),
7373
serviceWorker: node.getAttribute('service-worker'),
7474
});
7575
defineProperty(node, 'xworker', { value: xworker });

esm/script-handler.js

+15-14
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import $xworker from './worker/class.js';
66
import workerURL from './worker/url.js';
77
import { getRuntime, getRuntimeID } from './loader.js';
88
import { registry } from './interpreters.js';
9-
import { JSModules, all, dispatch, resolve, defineProperty, nodeInfo, registerJSModules } from './utils.js';
9+
import { JSModules, isSync, all, dispatch, resolve, defineProperty, nodeInfo, registerJSModules } from './utils.js';
1010

1111
const getRoot = (script) => {
1212
let parent = script;
@@ -55,12 +55,6 @@ const execute = async (currentScript, source, XWorker, isAsync) => {
5555
source,
5656
]);
5757
try {
58-
// temporarily override inherited document.currentScript in a non writable way
59-
// but it deletes it right after to preserve native behavior (as it's sync: no trouble)
60-
defineProperty(document, 'currentScript', {
61-
configurable: true,
62-
get: () => currentScript,
63-
});
6458
registerJSModules(type, module, interpreter, JSModules);
6559
module.registerJSModule(interpreter, 'polyscript', {
6660
XWorker,
@@ -69,10 +63,16 @@ const execute = async (currentScript, source, XWorker, isAsync) => {
6963
workers: workersHandler,
7064
});
7165
dispatch(currentScript, type, 'ready');
72-
const result = module[isAsync ? 'runAsync' : 'run'](interpreter, content);
66+
// temporarily override inherited document.currentScript in a non writable way
67+
// but it deletes it right after to preserve native behavior (as it's sync: no trouble)
68+
defineProperty(document, 'currentScript', {
69+
configurable: true,
70+
get: () => currentScript,
71+
});
7372
const done = dispatch.bind(null, currentScript, type, 'done');
74-
if (isAsync) result.then(done);
75-
else done();
73+
let result = module[isAsync ? 'runAsync' : 'run'](interpreter, content);
74+
if (isAsync) result = await result;
75+
done();
7676
return result;
7777
} finally {
7878
delete document.currentScript;
@@ -125,7 +125,6 @@ export const handle = async (script) => {
125125
// and/or source code with different config or interpreter
126126
const {
127127
attributes: {
128-
async: isAsync,
129128
config,
130129
env,
131130
name: wn,
@@ -137,19 +136,21 @@ export const handle = async (script) => {
137136
type,
138137
} = script;
139138

139+
/* c8 ignore start */
140+
const isAsync = !isSync(script);
141+
140142
const versionValue = version?.value;
141143
const name = getRuntimeID(type, versionValue);
142144
let configValue = getValue(config, '|');
143145
const id = getValue(env, '') || `${name}${configValue}`;
144146
configValue = configValue.slice(1);
145147

146-
/* c8 ignore start */
147148
const url = workerURL(script);
148149
if (url) {
149150
const XWorker = $xworker(type, versionValue);
150151
const xworker = new XWorker(url, {
151152
...nodeInfo(script, type),
152-
async: !!isAsync,
153+
async: isAsync,
153154
config: configValue,
154155
serviceWorker: sw?.value,
155156
});
@@ -176,7 +177,7 @@ export const handle = async (script) => {
176177
// start fetching external resources ASAP
177178
const source = src ? fetch(src).text() : script.textContent;
178179
details.queue = details.queue.then(() =>
179-
execute(script, source, details.XWorker, !!isAsync),
180+
execute(script, source, details.XWorker, isAsync),
180181
);
181182
}
182183
};

esm/utils.js

+4
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ export const importCSS = href => new Promise((onload, onerror) => {
124124
});
125125

126126
export const isCSS = source => /\.css$/i.test(new URL(source).pathname);
127+
128+
export const isSync = element =>
129+
/^(?:false|0|no)$/i.test(element.getAttribute('async'));
130+
127131
/* c8 ignore stop */
128132

129133
export {

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "polyscript",
3-
"version": "0.14.5",
3+
"version": "0.15.0",
44
"description": "PyScript single core to rule them all",
55
"main": "./esm/index.js",
66
"types": "./types/polyscript/esm/index.d.ts",

test/integration/interpreter/micropython/custom-hooks.html

+4-5
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
hooks: {
1313
main: {
1414
onReady: ({ run, runAsync }, element) => {
15+
const isAsync = element.getAttribute('async') !== 'false';
1516
console.log('onMainReady');
1617
dispatchEvent(new Event('mpy:ready'));
17-
const exec = element.hasAttribute('async') ? runAsync : run;
18+
const exec = isAsync ? runAsync : run;
1819
exec(element.textContent.trim());
1920
},
2021
onWorker: () => console.log('onWorkerMain'),
@@ -70,10 +71,8 @@
7071
const isScript = type === 'script';
7172
const tag = document.createElement(isScript ? 'script' : type + '-script');
7273
if (isScript) tag.type = 'mpy';
73-
if (current.async) {
74-
type += '-async';
75-
tag.setAttribute('async', '');
76-
}
74+
if (current.async) type += '-async';
75+
else tag.setAttribute('async', 'false');
7776
if (current.worker) {
7877
type += '-worker';
7978
tag.setAttribute('worker', '');

test/mocked/micropython.mjs

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ export const loadMicroPython = () => ({
1919
python.target = document.currentScript.target;
2020
}
2121
},
22+
runPythonAsync(content) {
23+
return this.runPython(content);
24+
},
2225
globals: {
2326
set(name, value) {
2427
globalThis[name] = value;

test/mocked/pyodide.mjs

+3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ export const loadPyodide = () => ({
2626
}
2727
python.target = document.currentScript.target;
2828
},
29+
runPythonAsync(content) {
30+
return this.runPython(content);
31+
},
2932
globals: {
3033
set(name, value) {
3134
globalThis[name] = value;

0 commit comments

Comments
 (0)