-
Notifications
You must be signed in to change notification settings - Fork 903
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
On web, allow recreating event loop when using spawn
#2720
Conversation
cc @kchibisov this is the approach you mentioned on #2344 (comment) |
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.
I think that's not exactly what I've suggested.
What I meant was more like .with_allow_multiple_loops(true)
in the new EventLoopBuilderExtWeb
.
Can't you create multiple event loops at once on the web and then use spawn
? If you can't and you must use spawn
before creating the second loop, then it should be static in a comment.
You should also update the docs for the spawn
that it allows multiple event loop creation.
Though, I guess you can't have 2 loops before spawn, since you need to consume the loop?
I'd be happy to change the approach here but I'm not sure I follow the approach you described. In case the use case isn't clear, my use case works like this:
The flow works the same way as run/spawn normally would, except I need to clear the static flag that's set at the wasm module level because it prevents run/spawn from being called again. I'm not trying to run multiple event loops simultaneously or similar. |
No, what I mean is could you do something like that on wasm let evl1 = EventLoop::new();
let evl2 = EventLoop::new();
// And spawn later on. Right now you won't be able to do so, because you clear only on spawn. What I suggested is that once you build you pass inside the builder a flag that you must not restrict additional creation, so instead of clearing on |
Sure, we could do that instead. I don't have a strong preference though it feels like we should just handle it internally instead of having it be opt-in on the builder. Is it valid to recreate the event loop multiple times without consuming it in run/spawn? |
Not on any other platform, buy maybe you can do that on Wasm? That's what I'm asking you, if you can create multiple events loop at the same time on wasm, we should allow that with the extension to the builder, on any other platform it's impossible. |
I don't think this is possible in Winit, there are event handlers registered on the window itself, like the scale change observer, which would be overwritten by a new event loop. It's possible to augment these event handlers to handle multiple event loops, but I don't think that's the goal here. |
Thanks, that's what I wanted to know. |
Wouldn't it be possible to avoid resetting the event handlers by using |
I didn't know this, I thought Just checked our code, we aren't using any setters as far as I can see. Also noticed that we are using the deprecated |
5bfa6f1
to
5d1f497
Compare
Thank you for the review! I think everything should be addressed now. I don't think it's worth trying to support multiple event loops right now (at least to solve this immediate issue) since it opens up a lot of other questions about how we might want to extend support for the web in general. For example:
I'd be happy to consider ways we could extend this later on, but the current change feels like a simple enough that we could land initially. |
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.
LGTM.
I think we shouldn't try to support multiple event loops until we know what a use-case for this even looks like, in any case I agree with @grovesNL this is out-of-scope for this PR.
@grovesNL I'm happy to merge this after a rebase. |
I'm gonna close this as it's rather old now and needs a rebase. In general this issue is tracked in #2885. |
CHANGELOG.md
if knowledge of this change could be valuable to usersAs described in #2344 (comment), it's useful to be able to recreate event loops on the web when using
spawn
, since the wasm module remains loaded. e.g., switching between tabs on a single page app and recreating a canvas doesn't currently work without some kind of workaround like this.This won't change the behavior of
run
, so that will still panic when attempting to create multiple event loops.I know there are probably still closures leaked when using this approach but don't expect this to be an issue in practice. We can also deal with any leaked closures later. At least anecdotally I've been using this approach for a while now and haven't run into any problems.