Skip to content

Commit dd95bff

Browse files
authored
Tech preview sync (#47)
1 parent 853b251 commit dd95bff

28 files changed

+3899
-55
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"env": {
3+
"browser": true,
4+
"es2022": true
5+
},
6+
"extends": "eslint:recommended",
7+
"parserOptions": {
8+
"ecmaVersion": 12,
9+
"sourceType": "module"
10+
},
11+
"rules": {}
12+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# This workflow will do a clean install of node dependencies, cache/restore them, build the source code and run tests across different versions of node
2+
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
3+
4+
name: build
5+
6+
on: [push, pull_request]
7+
8+
jobs:
9+
build:
10+
runs-on: ubuntu-latest
11+
12+
strategy:
13+
matrix:
14+
node-version: [20]
15+
16+
steps:
17+
- uses: actions/checkout@v2
18+
- name: Use Node.js ${{ matrix.node-version }}
19+
uses: actions/setup-node@v2
20+
with:
21+
node-version: ${{ matrix.node-version }}
22+
cache: "npm"
23+
- run: npm ci
24+
- run: npm run build --if-present
25+
- run: npm test
26+
- run: npm run coverage --if-present
27+
- name: Coveralls
28+
uses: coverallsapp/github-action@master
29+
with:
30+
github-token: ${{ secrets.GITHUB_TOKEN }}

tech-preview/pyscript.core/.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.DS_Store
2+
coverage/
3+
node_modules/
4+
cjs/
5+
!cjs/package.json
6+
core.js
7+
esm/worker/xworker.js
8+
types/

tech-preview/pyscript.core/.npmignore

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
.DS_Store
2+
.nyc_output
3+
.eslintrc.json
4+
.github/
5+
.travis.yml
6+
.eslintrc.json
7+
*.log
8+
coverage/
9+
micropython/
10+
node_modules/
11+
pyscript/
12+
rollup/
13+
test/
14+
index.html
15+
node.importmap
16+
sw.js
17+
tsconfig.json
18+
cjs/worker/_template.js
19+
esm/worker/_template.js

tech-preview/pyscript.core/.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=true

tech-preview/pyscript.core/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ This project requires some automatic artifact creation to:
1919

2020
* create a _Worker_ as a _Blob_ based on the same code used by this repo
2121
* create automatically the list of runtimes available via the module
22-
* create the `min.js` file used by most integration tests
22+
* create the `core.js` file used by most integration tests
2323
* create a sha256 version of the Blob content for CSP cases
2424

2525
Accordingly, to build latest project:

tech-preview/pyscript.core/cjs/custom.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const {
1212
const { getRuntimeID } = require("./loader.js");
1313
const { io } = require("./interpreter/_utils.js");
1414
const { addAllListeners } = require("./listeners.js");
15+
const { Hook } = require("./worker/hooks.js");
1516

1617
const CUSTOM_SELECTORS = [];
1718
exports.CUSTOM_SELECTORS = CUSTOM_SELECTORS;
@@ -66,18 +67,9 @@ const handleCustomType = (node) => {
6667
onBeforeRunAsync,
6768
onAfterRun,
6869
onAfterRunAsync,
69-
codeBeforeRunWorker,
70-
codeBeforeRunWorkerAsync,
71-
codeAfterRunWorker,
72-
codeAfterRunWorkerAsync,
7370
} = options;
7471

75-
const hooks = {
76-
beforeRun: codeBeforeRunWorker?.(),
77-
beforeRunAsync: codeBeforeRunWorkerAsync?.(),
78-
afterRun: codeAfterRunWorker?.(),
79-
afterRunAsync: codeAfterRunWorkerAsync?.(),
80-
};
72+
const hooks = new Hook(options);
8173

8274
const XWorker = function XWorker(...args) {
8375
return Worker.apply(hooks, args);

tech-preview/pyscript.core/cjs/custom/pyscript.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,10 @@ document.head.appendChild(document.createElement("style")).textContent = `
9191
env: "py-script",
9292
interpreter: "pyodide",
9393
codeBeforeRunWorker() {
94-
const { codeBeforeRunWorker: set } = hooks;
95-
const prefix = 'print("codeBeforeRunWorker")';
96-
return [prefix].concat(...set).join("\n");
94+
return [...hooks.codeBeforeRunWorker].join("\n");
9795
},
9896
codeAfterRunWorker() {
99-
const { codeAfterRunWorker: set } = hooks;
100-
const prefix = 'print("codeAfterRunWorker")';
101-
return [prefix].concat(...set).join("\n");
97+
return [...hooks.codeAfterRunWorker].join("\n");
10298
},
10399
onBeforeRun(pyodide, element) {
104100
bootstrapNodeAndPlugins(pyodide, element, before, "onBeforeRun");

tech-preview/pyscript.core/cjs/worker/class.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const coincident = (m => /* c8 ignore start */ m.__esModule ? m.default : m /* c
44
const xworker = (m => /* c8 ignore start */ m.__esModule ? m.default : m /* c8 ignore stop */)(require("./xworker.js"));
55
const { assign, defineProperties, absoluteURL } = require("../utils.js");
66
const { getText } = require("../fetch-utils.js");
7+
const { Hook } = require("./hooks.js");
78

89
/**
910
* @typedef {Object} WorkerOptions custom configuration
@@ -22,7 +23,6 @@ module.exports = (...args) =>
2223
function XWorker(url, options) {
2324
const worker = xworker();
2425
const { postMessage } = worker;
25-
const hooks = this instanceof XWorker ? void 0 : this;
2626
if (args.length) {
2727
const [type, version] = args;
2828
options = assign({}, options || { type, version });
@@ -31,7 +31,10 @@ module.exports = (...args) =>
3131
if (options?.config) options.config = absoluteURL(options.config);
3232
const bootstrap = fetch(url)
3333
.then(getText)
34-
.then((code) => postMessage.call(worker, { options, code, hooks }));
34+
.then((code) => {
35+
const hooks = this instanceof Hook ? this : void 0;
36+
postMessage.call(worker, { options, code, hooks });
37+
});
3538
return defineProperties(worker, {
3639
postMessage: {
3740
value: (data, ...rest) =>
Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,17 @@
11
'use strict';
2-
module.exports = new WeakMap();
2+
// REQUIRES INTEGRATION TEST
3+
/* c8 ignore start */
4+
const workerHooks = [
5+
["beforeRun", "codeBeforeRunWorker"],
6+
["beforeRunAsync", "codeBeforeRunWorkerAsync"],
7+
["afterRun", "codeAfterRunWorker"],
8+
["afterRunAsync", "codeAfterRunWorkerAsync"],
9+
];
10+
11+
class Hook {
12+
constructor(fields) {
13+
for (const [key, value] of workerHooks) this[key] = fields[value]?.();
14+
}
15+
}
16+
exports.Hook = Hook
17+
/* c8 ignore stop */

0 commit comments

Comments
 (0)