Skip to content

Commit 250ab7f

Browse files
Merge pull request #33 from pyscript/better-dispatch
Better dispatch with id check
2 parents fdee268 + 69cfe07 commit 250ab7f

File tree

7 files changed

+24
-17
lines changed

7 files changed

+24
-17
lines changed

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.

docs/core.js

Lines changed: 2 additions & 2 deletions
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/utils.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,15 @@ const nodeInfo = (node, type) => ({
1717
tag: node.tagName
1818
});
1919

20+
/**
21+
* Notify the main thread about element "readiness".
22+
* @param {HTMLScriptElement | HTMLElement} target the script or custom-type element
23+
* @param {string} type the custom/type as event prefix
24+
* @param {boolean} worker `true` if dispatched form a worker, `false` if in main
25+
* @param {globalThis.CustomEvent} CustomEvent the right global to use
26+
*/
2027
const dispatch = (target, type, worker, CustomEvent) => {
21-
if (target) target.dispatchEvent(
28+
target.dispatchEvent(
2229
new CustomEvent(`${type}:ready`, {
2330
bubbles: true,
2431
detail: { worker },

esm/worker/_template.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,15 @@ add('message', ({ data: { options, config: baseURL, code, hooks } }) => {
9999
}
100100

101101
const { CustomEvent, document } = window;
102-
const element = document.getElementById(id);
102+
const element = id && document.getElementById(id) || null;
103103

104104
let target = '';
105105

106106
// set the `xworker` global reference once
107107
details.registerJSModule(interpreter, 'polyscript', {
108108
xworker,
109109
get target() {
110-
if (!target) {
110+
if (!target && element) {
111111
if (tag === 'SCRIPT') {
112112
element.after(assign(
113113
document.createElement(`script-${custom || type}`),
@@ -130,7 +130,7 @@ add('message', ({ data: { options, config: baseURL, code, hooks } }) => {
130130
// allows transforming arguments with sync
131131
transform = details.transform.bind(details, interpreter);
132132

133-
dispatch(element, custom || type, true, CustomEvent);
133+
if (element) dispatch(element, custom || type, true, CustomEvent);
134134

135135
// run either sync or async code in the worker
136136
await details[name](interpreter, code);

package-lock.json

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

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "polyscript",
3-
"version": "0.3.4",
3+
"version": "0.3.5",
44
"description": "PyScript single core to rule them all",
55
"main": "./cjs/index.js",
66
"types": "./types/polyscript/esm/index.d.ts",
@@ -67,9 +67,9 @@
6767
"@ungap/with-resolvers": "^0.1.0",
6868
"basic-devtools": "^0.1.6",
6969
"codedent": "^0.1.2",
70-
"coincident": "^0.11.5"
70+
"coincident": "^0.11.6"
7171
},
7272
"worker": {
73-
"blob": "sha256-DIHUuInfI4tdn+DpgcTAEaslvvsvwq51ZU18P3a6bnk="
73+
"blob": "sha256-YCjLBQpKRjsu6QAzGWRouTXHL9/8bvDnOFrZ2RIb6lM="
7474
}
7575
}

0 commit comments

Comments
 (0)