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

[discussion] Is there any way to post progress reports back to main thread? #4

Closed
kurtlawrence opened this issue Jul 3, 2023 · 4 comments
Labels
enhancement New feature or request question Further information is requested

Comments

@kurtlawrence
Copy link

Is there a method to post progress messages back to the main thread?

I do not think I can use postMessage, maybe there is a way through events?

@852Kerfunkle
Copy link
Owner

Hi @kurtlawrence,

there's no dedicated way to post arbitrary messages (or events) to the main thread and receive them.

Currently, you could pass a stream to the thread (see transfer-streams.worker.ts) and write the progress to it periodically.

Another option is to query the worker state (via a new method in your worker). This will probably work just fine (note: I did not try this :) ) if the work done in the thread isn't blocking.

Other than that, it maybe makes sense to add a new WorkerMessageType for progress or even arbitrary events.

@852Kerfunkle 852Kerfunkle added enhancement New feature or request question Further information is requested labels Jul 3, 2023
@852Kerfunkle
Copy link
Owner

I suppose another (not to dissimilar, but maybe more general) way to add this functionality, would be through observable promises (you know... pub-sub). threads.js does have those, but I chose not to add them to threads-es to begin with.

And there was something else on my mind for a while.... there's threads-esm, which allows bidirectional communication (the worker can call methods on the main thread).

Generally, I would prefer to only add functionality if really needed, and the most versatile at that. Which would probably be observable promises.

Please let me know if the methods in the prev comment would do the trick for you (I know safari has issues with streams not being transferable - altho maybe they fixed this by now).

@kurtlawrence
Copy link
Author

Hi @852Kerfunkle, WritableStream worked perfectly for my use case, thanks for pointing it out. Maybe an example in the README might help future users, or maybe just pin this issue.

@852Kerfunkle 852Kerfunkle pinned this issue Jul 4, 2023
@852Kerfunkle
Copy link
Owner

I'll add an example to the docs next time I touch this.

Maybe I'll even add observable promises, who knows :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants