Skip to content
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

Worker caveats #158

Open
WebReflection opened this issue Dec 15, 2024 · 2 comments
Open

Worker caveats #158

WebReflection opened this issue Dec 15, 2024 · 2 comments

Comments

@WebReflection
Copy link
Contributor

WebReflection commented Dec 15, 2024

I just realized, out of a one-off example in Discord, that if a worker has listeners added after some reference is created or populated with values, the synchronous dance might mislead because that reference on the main might have been already bootstrapped or triggered/resolved.

The explicit issue I am talking about is this one:

image = window.Image.new()
# if this is before, we're all good
image.onload = lambda e: document.body.append(image)
# if that is after though, setting an inline source of the image
# will resolve it before the next `.onload = ...` or even
# `.addEventListener` is attached
image.src = f'data:image/svg+xml;utf8,{svg}'

The demo in case is this one https://pyscript.com/@agiammarchi/svg-image/latest?files=main.py,index.html

I had to double-check why, once added the worker attribute, that would not work.

Due inability to hook into listeners happening on the main, I think it's worth it to document the best way to be sure a listener is triggered, either on main or worker thread, is to define listeners related to such bootstrap AOT (Ahead of Time) and not later.

This is an important detail I could see myself failing at remembering on occasions, so I think it should be part, at least, of our F.A.Q.s section.

@Neon22
Copy link
Contributor

Neon22 commented Mar 26, 2025

Interestingly was trying to use this on svgs and all styling was ignored. I.e. image is always in B&W.
Had to change from utf-8 to base64 like so: (using the ltk here)

(the dwg is an svg text description)

img = ltk.Image(src=f"data:image/svg+xml;base64,{btoa(dwg.outerHTML)}")

The ltk has the nice side effect of not having to do the on.load and then trigger that by setting the image.src. It all happens in one step. Maybe that's the jQuery interior... I don't know for sure.

examples in main.py here: - https://pyscript.com/@neon22/satin-generator/latest?files=main.py

@WebReflection
Copy link
Contributor Author

to clarify, ltk on main uses MicroPython (AFAIK) so on main this issue does not exist, it exists on workers if you set the listener after, which is never the case for libraries so this is something to document as it cannot be fixed because we don't get to decide when listeners on the main are triggered and if we add those listeners after there's, by specs, no listener triggered.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants