Skip to content

Commit d100f1d

Browse files
committed
Added __export__ automatic resolution via named workers
1 parent 1d2e468 commit d100f1d

File tree

8 files changed

+27
-6
lines changed

8 files changed

+27
-6
lines changed

docs/index.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/index.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.

docs/toml-V4Savzlc.js

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/toml-V4Savzlc.js.map

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

esm/worker/_template.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ add('message', ({ data: { options, config: baseURL, configURL, code, hooks } })
203203
if (list.length) {
204204
interpreter.runPython([
205205
`from ${polyscript} import xworker as ${workers}`,
206+
`${workers}.sync.__export__ = lambda:${JSON.stringify(list)}`,
206207
...list.map(util => `${workers}.sync.${util} = ${util}`),
207208
`del ${workers}`,
208209
].join('\n'));

esm/workers.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,18 @@ export const workers = new Proxy(new Map, {
1414
// @issue https://github.com/pyscript/pyscript/issues/2106
1515
const ignore = new Set(['__dict__', 'constructor', 'get', 'has', 'includes', 'next', 'set', 'then']);
1616

17+
const exportHandler = {
18+
get: (target, prop) => {
19+
const method = target[prop];
20+
return prop === '__export__' ? method() : method;
21+
}
22+
};
23+
1724
export const workersHandler = new Proxy(Object.freeze({}), {
1825
// guard against forever pending Promises in Pyodide
1926
// @issue https://github.com/pyscript/pyscript/issues/2106
2027
get: (_, name) => (typeof name === 'string' && !ignore.has(name)) ?
21-
workers[name].promise.then(w => w.sync) :
28+
workers[name].promise.then(w => new Proxy(w.sync, exportHandler)) :
2229
void 0,
2330
});
2431
/* c8 ignore stop */

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,6 @@
9898
"to-json-callback": "^0.1.1"
9999
},
100100
"worker": {
101-
"blob": "sha256-ApE1J3NhEObM1H24FVV9LhqLlLGKcfe+iUy+8s/CKJE="
101+
"blob": "sha256-GAb/WDsbp8Z85OBm+d3VtcOBoZ2mlbuzQKJsre1kTAs="
102102
}
103103
}

test/worker/named.html

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,27 @@
1212

1313
await (await workers["mpy"]).greetings()
1414
await (await workers["py"]).greetings()
15+
16+
# print(await (await workers["mpy"]).__export__)
17+
print(list(await (await workers["mpy"]).test()))
1518
</script>
1619
<script type="micropython" worker name="mpy">
1720
def greetings():
1821
print("micropython")
1922

20-
__export__ = ['greetings']
23+
def test():
24+
return __export__
25+
26+
__export__ = ['greetings', 'test']
2127
</script>
2228
<script type="pyodide" worker name="py">
2329
def greetings():
2430
print("pyodide")
2531

26-
__export__ = ['greetings']
32+
def test():
33+
return __export__
34+
35+
__export__ = ['greetings', 'test']
2736
</script>
2837
</body>
2938
</html>

0 commit comments

Comments
 (0)