mirror of
https://github.com/pyscript/pyscript.git
synced 2025-12-22 03:35:31 -05:00
Breaking: new Polyscript Hooks mechanism (#1811)
* Breaking: new Polyscript Hooks mechanism * Added proper smoke test
This commit is contained in:
committed by
GitHub
parent
28d37cdead
commit
e67eb06d8b
26
pyscript.core/src/plugins-helper.js
Normal file
26
pyscript.core/src/plugins-helper.js
Normal file
@@ -0,0 +1,26 @@
|
||||
import { defineProperty } from "polyscript/exports";
|
||||
|
||||
// helper for all script[type="py"] out there
|
||||
const before = (script) => {
|
||||
defineProperty(document, "currentScript", {
|
||||
configurable: true,
|
||||
get: () => script,
|
||||
});
|
||||
};
|
||||
|
||||
const after = () => {
|
||||
delete document.currentScript;
|
||||
};
|
||||
|
||||
// common life-cycle handlers for any node
|
||||
export default async (main, wrap, element, hook) => {
|
||||
const isAsync = hook.endsWith("Async");
|
||||
const isBefore = hook.startsWith("onBefore");
|
||||
// make it possible to reach the current target node via Python
|
||||
// or clean up for other scripts executing around this one
|
||||
(isBefore ? before : after)(element);
|
||||
for (const fn of main(hook)) {
|
||||
if (isAsync) await fn(wrap, element);
|
||||
else fn(wrap, element);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user