mirror of
https://github.com/pyscript/pyscript.git
synced 2025-12-21 19:25:35 -05:00
27 lines
791 B
JavaScript
27 lines
791 B
JavaScript
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);
|
|
}
|
|
};
|