-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Improved py:all-done event #1778
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
WebReflection
merged 1 commit into
pyscript:main
from
WebReflection:test-weird-display-cases
Oct 2, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Improved py:all-done event
- Loading branch information
commit 233eb758cd549b0eefa2e1236768efa1c7abce42
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,17 @@ | ||
import TYPES from "./types.js"; | ||
import hooks from "./hooks.js"; | ||
|
||
const DONE = "py:all-done"; | ||
|
||
const { | ||
onAfterRun, | ||
onAfterRunAsync, | ||
codeAfterRunWorker, | ||
codeAfterRunWorkerAsync, | ||
} = hooks; | ||
|
||
const waitForIt = []; | ||
const codes = []; | ||
|
||
const codeFor = (element) => { | ||
const isAsync = element.hasAttribute("async"); | ||
const { promise, resolve } = Promise.withResolvers(); | ||
const type = `${DONE}:${waitForIt.push(promise)}`; | ||
|
||
// resolve each promise once notified | ||
addEventListener(type, resolve, { once: true }); | ||
|
||
if (element.hasAttribute("worker")) { | ||
const code = ` | ||
from pyscript import window as _w | ||
_w.dispatchEvent(_w.Event.new("${type}")) | ||
`; | ||
if (isAsync) codeAfterRunWorkerAsync.add(code); | ||
else codeAfterRunWorker.add(code); | ||
return code; | ||
for (const [TYPE] of TYPES) { | ||
const selectors = [`script[type="${TYPE}"]`, `${TYPE}-script`]; | ||
for (const element of document.querySelectorAll(selectors.join(","))) { | ||
const { promise, resolve } = Promise.withResolvers(); | ||
waitForIt.push(promise); | ||
element.addEventListener(`${TYPE}:done`, resolve, { once: true }); | ||
} | ||
|
||
// dispatch only once the ready element is the same | ||
const code = (_, el) => { | ||
if (el === element) dispatchEvent(new Event(type)); | ||
}; | ||
|
||
if (isAsync) onAfterRunAsync.add(code); | ||
else onAfterRun.add(code); | ||
return code; | ||
}; | ||
|
||
const selector = []; | ||
for (const [TYPE] of TYPES) | ||
selector.push(`script[type="${TYPE}"]`, `${TYPE}-script`); | ||
|
||
// loop over all known scripts and elements | ||
for (const element of document.querySelectorAll(selector.join(","))) | ||
codes.push(codeFor(element)); | ||
} | ||
|
||
// wait for all the things then cleanup | ||
Promise.all(waitForIt).then(() => { | ||
// cleanup unnecessary hooks | ||
for (const code of codes) { | ||
onAfterRun.delete(code); | ||
onAfterRunAsync.delete(code); | ||
codeAfterRunWorker.delete(code); | ||
codeAfterRunWorkerAsync.delete(code); | ||
} | ||
dispatchEvent(new Event(DONE)); | ||
dispatchEvent(new Event("py:all-done")); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>PyScript Next</title> | ||
<script> | ||
addEventListener("py:all-done", ({ type }) => console.log(type)); | ||
</script> | ||
<link rel="stylesheet" href="../dist/core.css"> | ||
<script type="module" src="../dist/core.js"></script> | ||
</head> | ||
<body> | ||
<script type="py" worker async> | ||
from pyscript import display | ||
display('hello 1') | ||
|
||
import js | ||
import time | ||
js.console.log('sleeping...') | ||
time.sleep(2) | ||
js.console.log('...done') | ||
</script> | ||
<p>hello 2</p> | ||
<script type="py" worker async> | ||
from pyscript import display | ||
display('hello 3') | ||
</script> | ||
</body> | ||
</html> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should become a proper integration test
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we could do a follow up but the feature is from polyscript so eventually tests should be done there, imho.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
actually, never mind, these probably should be done in both places because custom types events are not polyscript matter anymore ... that being said, we currently have
time
working in either sync or async scripts/elements ... not sure how to test this best as async and time don't really use async feature there. If you'd like to add a better test, please go ahead, thank you!