@pyscript/core 0.2.3 → 0.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/core.js +2 -2
- package/dist/core.js.map +1 -1
- package/package.json +4 -3
- package/src/all-done.js +62 -0
- package/src/core.js +10 -27
- package/src/hooks.js +21 -0
- package/types/all-done.d.ts +1 -0
- package/types/core.d.ts +2 -12
- package/types/hooks.d.ts +12 -0
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@pyscript/core",
|
3
|
-
"version": "0.2.
|
3
|
+
"version": "0.2.4",
|
4
4
|
"type": "module",
|
5
5
|
"description": "PyScript",
|
6
6
|
"module": "./index.js",
|
@@ -20,7 +20,7 @@
|
|
20
20
|
},
|
21
21
|
"scripts": {
|
22
22
|
"server": "npx static-handler --cors --coep --coop --corp .",
|
23
|
-
"build": "node rollup/stdlib.cjs && node rollup/plugins.cjs && rm -rf dist && rollup --config rollup/core.config.js && npm run ts",
|
23
|
+
"build": "node rollup/stdlib.cjs && node rollup/plugins.cjs && rm -rf dist && rollup --config rollup/core.config.js && eslint src/ && npm run ts",
|
24
24
|
"size": "echo -e \"\\033[1mdist/*.js file size\\033[0m\"; for js in $(ls dist/*.js); do echo -e \"\\033[2m$js:\\033[0m $(cat $js | brotli | wc -c) bytes\"; done",
|
25
25
|
"ts": "tsc -p ."
|
26
26
|
},
|
@@ -33,11 +33,12 @@
|
|
33
33
|
"dependencies": {
|
34
34
|
"@ungap/with-resolvers": "^0.1.0",
|
35
35
|
"basic-devtools": "^0.1.6",
|
36
|
-
"polyscript": "^0.4.
|
36
|
+
"polyscript": "^0.4.6"
|
37
37
|
},
|
38
38
|
"devDependencies": {
|
39
39
|
"@rollup/plugin-node-resolve": "^15.2.1",
|
40
40
|
"@rollup/plugin-terser": "^0.4.3",
|
41
|
+
"eslint": "^8.50.0",
|
41
42
|
"rollup": "^3.29.3",
|
42
43
|
"rollup-plugin-postcss": "^4.0.2",
|
43
44
|
"rollup-plugin-string": "^3.0.0",
|
package/src/all-done.js
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
import TYPES from "./types.js";
|
2
|
+
import hooks from "./hooks.js";
|
3
|
+
|
4
|
+
const DONE = "py:all-done";
|
5
|
+
|
6
|
+
const {
|
7
|
+
onAfterRun,
|
8
|
+
onAfterRunAsync,
|
9
|
+
codeAfterRunWorker,
|
10
|
+
codeAfterRunWorkerAsync,
|
11
|
+
} = hooks;
|
12
|
+
|
13
|
+
const waitForIt = [];
|
14
|
+
const codes = [];
|
15
|
+
|
16
|
+
const codeFor = (element) => {
|
17
|
+
const isAsync = element.hasAttribute("async");
|
18
|
+
const { promise, resolve } = Promise.withResolvers();
|
19
|
+
const type = `${DONE}:${waitForIt.push(promise)}`;
|
20
|
+
|
21
|
+
// resolve each promise once notified
|
22
|
+
addEventListener(type, resolve, { once: true });
|
23
|
+
|
24
|
+
if (element.hasAttribute("worker")) {
|
25
|
+
const code = `
|
26
|
+
from pyscript import window as _w
|
27
|
+
_w.dispatchEvent(_w.Event.new("${type}"))
|
28
|
+
`;
|
29
|
+
if (isAsync) codeAfterRunWorkerAsync.add(code);
|
30
|
+
else codeAfterRunWorker.add(code);
|
31
|
+
return code;
|
32
|
+
}
|
33
|
+
|
34
|
+
// dispatch only once the ready element is the same
|
35
|
+
const code = (_, el) => {
|
36
|
+
if (el === element) dispatchEvent(new Event(type));
|
37
|
+
};
|
38
|
+
|
39
|
+
if (isAsync) onAfterRunAsync.add(code);
|
40
|
+
else onAfterRun.add(code);
|
41
|
+
return code;
|
42
|
+
};
|
43
|
+
|
44
|
+
const selector = [];
|
45
|
+
for (const [TYPE] of TYPES)
|
46
|
+
selector.push(`script[type="${TYPE}"]`, `${TYPE}-script`);
|
47
|
+
|
48
|
+
// loop over all known scripts and elements
|
49
|
+
for (const element of document.querySelectorAll(selector.join(",")))
|
50
|
+
codes.push(codeFor(element));
|
51
|
+
|
52
|
+
// wait for all the things then cleanup
|
53
|
+
Promise.all(waitForIt).then(() => {
|
54
|
+
// cleanup unnecessary hooks
|
55
|
+
for (const code of codes) {
|
56
|
+
onAfterRun.delete(code);
|
57
|
+
onAfterRunAsync.delete(code);
|
58
|
+
codeAfterRunWorker.delete(code);
|
59
|
+
codeAfterRunWorkerAsync.delete(code);
|
60
|
+
}
|
61
|
+
dispatchEvent(new Event(DONE));
|
62
|
+
});
|
package/src/core.js
CHANGED
@@ -1,16 +1,21 @@
|
|
1
1
|
/*! (c) PyScript Development Team */
|
2
2
|
|
3
3
|
import "@ungap/with-resolvers";
|
4
|
-
import { INVALID_CONTENT, define, XWorker } from "polyscript";
|
5
4
|
|
6
|
-
//
|
7
|
-
|
5
|
+
// These imports can hook more than usual and help debugging possible polyscript issues
|
6
|
+
import {
|
7
|
+
INVALID_CONTENT,
|
8
|
+
define,
|
9
|
+
XWorker,
|
10
|
+
} from "../node_modules/polyscript/esm/index.js";
|
8
11
|
import { queryTarget } from "../node_modules/polyscript/esm/script-handler.js";
|
9
12
|
import { dedent, dispatch } from "../node_modules/polyscript/esm/utils.js";
|
10
13
|
import { Hook } from "../node_modules/polyscript/esm/worker/hooks.js";
|
11
14
|
|
15
|
+
import "./all-done.js";
|
12
16
|
import TYPES from "./types.js";
|
13
17
|
import configs from "./config.js";
|
18
|
+
import hooks from "./hooks.js";
|
14
19
|
import sync from "./sync.js";
|
15
20
|
import stdlib from "./stdlib.js";
|
16
21
|
import { ErrorCode } from "./exceptions.js";
|
@@ -66,28 +71,6 @@ const registerModule = ({ XWorker: $XWorker, interpreter, io }) => {
|
|
66
71
|
interpreter.runPython(stdlib, { globals: interpreter.runPython("{}") });
|
67
72
|
};
|
68
73
|
|
69
|
-
export const hooks = {
|
70
|
-
/** @type {Set<function>} */
|
71
|
-
onBeforeRun: new Set(),
|
72
|
-
/** @type {Set<function>} */
|
73
|
-
onBeforeRunAsync: new Set(),
|
74
|
-
/** @type {Set<function>} */
|
75
|
-
onAfterRun: new Set(),
|
76
|
-
/** @type {Set<function>} */
|
77
|
-
onAfterRunAsync: new Set(),
|
78
|
-
/** @type {Set<function>} */
|
79
|
-
onInterpreterReady: new Set(),
|
80
|
-
|
81
|
-
/** @type {Set<string>} */
|
82
|
-
codeBeforeRunWorker: new Set(),
|
83
|
-
/** @type {Set<string>} */
|
84
|
-
codeBeforeRunWorkerAsync: new Set(),
|
85
|
-
/** @type {Set<string>} */
|
86
|
-
codeAfterRunWorker: new Set(),
|
87
|
-
/** @type {Set<string>} */
|
88
|
-
codeAfterRunWorkerAsync: new Set(),
|
89
|
-
};
|
90
|
-
|
91
74
|
const workerHooks = {
|
92
75
|
codeBeforeRunWorker: () =>
|
93
76
|
[stdlib, ...hooks.codeBeforeRunWorker].map(dedent).join("\n"),
|
@@ -100,7 +83,7 @@ const workerHooks = {
|
|
100
83
|
};
|
101
84
|
|
102
85
|
const exportedConfig = {};
|
103
|
-
export { exportedConfig as config };
|
86
|
+
export { exportedConfig as config, hooks };
|
104
87
|
|
105
88
|
for (const [TYPE, interpreter] of TYPES) {
|
106
89
|
const { config, plugins, error } = configs.get(TYPE);
|
@@ -206,7 +189,7 @@ for (const [TYPE, interpreter] of TYPES) {
|
|
206
189
|
} = element;
|
207
190
|
const hasTarget = !!target?.value;
|
208
191
|
const show = hasTarget
|
209
|
-
? queryTarget(target.value)
|
192
|
+
? queryTarget(element, target.value)
|
210
193
|
: document.createElement("script-py");
|
211
194
|
|
212
195
|
if (!hasTarget) {
|
package/src/hooks.js
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
export default {
|
2
|
+
/** @type {Set<function>} */
|
3
|
+
onBeforeRun: new Set(),
|
4
|
+
/** @type {Set<function>} */
|
5
|
+
onBeforeRunAsync: new Set(),
|
6
|
+
/** @type {Set<function>} */
|
7
|
+
onAfterRun: new Set(),
|
8
|
+
/** @type {Set<function>} */
|
9
|
+
onAfterRunAsync: new Set(),
|
10
|
+
/** @type {Set<function>} */
|
11
|
+
onInterpreterReady: new Set(),
|
12
|
+
|
13
|
+
/** @type {Set<string>} */
|
14
|
+
codeBeforeRunWorker: new Set(),
|
15
|
+
/** @type {Set<string>} */
|
16
|
+
codeBeforeRunWorkerAsync: new Set(),
|
17
|
+
/** @type {Set<string>} */
|
18
|
+
codeAfterRunWorker: new Set(),
|
19
|
+
/** @type {Set<string>} */
|
20
|
+
codeAfterRunWorkerAsync: new Set(),
|
21
|
+
};
|
@@ -0,0 +1 @@
|
|
1
|
+
export {};
|
package/types/core.d.ts
CHANGED
@@ -10,17 +10,7 @@ export function PyWorker(file: string, options?: {
|
|
10
10
|
}): Worker & {
|
11
11
|
sync: ProxyHandler<object>;
|
12
12
|
};
|
13
|
-
export namespace hooks {
|
14
|
-
let onBeforeRun: Set<Function>;
|
15
|
-
let onBeforeRunAsync: Set<Function>;
|
16
|
-
let onAfterRun: Set<Function>;
|
17
|
-
let onAfterRunAsync: Set<Function>;
|
18
|
-
let onInterpreterReady: Set<Function>;
|
19
|
-
let codeBeforeRunWorker: Set<string>;
|
20
|
-
let codeBeforeRunWorkerAsync: Set<string>;
|
21
|
-
let codeAfterRunWorker: Set<string>;
|
22
|
-
let codeAfterRunWorkerAsync: Set<string>;
|
23
|
-
}
|
24
|
-
export { exportedConfig as config };
|
25
13
|
import sync from "./sync.js";
|
26
14
|
declare const exportedConfig: {};
|
15
|
+
import hooks from "./hooks.js";
|
16
|
+
export { exportedConfig as config, hooks };
|
package/types/hooks.d.ts
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
declare namespace _default {
|
2
|
+
let onBeforeRun: Set<Function>;
|
3
|
+
let onBeforeRunAsync: Set<Function>;
|
4
|
+
let onAfterRun: Set<Function>;
|
5
|
+
let onAfterRunAsync: Set<Function>;
|
6
|
+
let onInterpreterReady: Set<Function>;
|
7
|
+
let codeBeforeRunWorker: Set<string>;
|
8
|
+
let codeBeforeRunWorkerAsync: Set<string>;
|
9
|
+
let codeAfterRunWorker: Set<string>;
|
10
|
+
let codeAfterRunWorkerAsync: Set<string>;
|
11
|
+
}
|
12
|
+
export default _default;
|