Skip to content

Commit ec1e765

Browse files
committed
add plugin example
1 parent b23d807 commit ec1e765

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

docs/user-guide/plugins.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,42 @@ core itself.
1212
Here's an example of how a PyScript plugin looks like:
1313

1414
```js
15+
// import the hooks from PyScript first...
16+
import { hooks } from "<path_to_pyscript>/core.js";
1517

18+
// Use the `main` attribute on hooks do define plugins that run on the main thread
19+
hooks.main.onReady.add((wrap, element) => {
20+
console.log("main", "onReady");
21+
if (location.search === '?debug') {
22+
console.debug("main", "wrap", wrap);
23+
console.debug("main", "element", element);
24+
}
25+
});
26+
hooks.main.onBeforeRun.add(() => {
27+
console.log("main", "onBeforeRun");
28+
});
29+
hooks.main.codeBeforeRun.add('print("main", "codeBeforeRun")');
30+
hooks.main.codeAfterRun.add('print("main", "codeAfterRun")');
31+
hooks.main.onAfterRun.add(() => {
32+
console.log("main", "onAfterRun");
33+
});
34+
35+
// Use the `worker` attribute on hooks do define plugins that run on workers
36+
hooks.worker.onReady.add((wrap, xworker) => {
37+
console.log("worker", "onReady");
38+
if (location.search === '?debug') {
39+
console.debug("worker", "wrap", wrap);
40+
console.debug("worker", "xworker", xworker);
41+
}
42+
});
43+
44+
hooks.worker.onBeforeRun.add(() => {
45+
console.log("worker", "onBeforeRun");
46+
});
47+
48+
hooks.worker.codeBeforeRun.add('print("worker", "codeBeforeRun")');
49+
hooks.worker.codeAfterRun.add('print("worker", "codeAfterRun")');
50+
hooks.worker.onAfterRun.add(() => {
51+
console.log("worker", "onAfterRun");
52+
});
1653
```

0 commit comments

Comments
 (0)