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

Send the Connected message when the public worker is loaded #944

Closed
bacongobbler opened this issue Feb 12, 2020 · 2 comments · Fixed by #1007
Closed

Send the Connected message when the public worker is loaded #944

bacongobbler opened this issue Feb 12, 2020 · 2 comments · Fixed by #1007
Labels

Comments

@bacongobbler
Copy link
Contributor

// TODO Send `Connected` message

@bacongobbler bacongobbler added the feature-request A feature request label Feb 12, 2020
@TheNeikos
Copy link
Contributor

What exactly needs to be done here? I'd gladly help!

@jstarry
Copy link
Member

jstarry commented Mar 8, 2020

Thanks for offering!

So, whenever someone connects to an agent, Yew should send a Connected message to the agent so that it can track who's connected. Each connection has a unique HandlerId.

I think we can take advantage of the existing msg queue that was setup not too long ago. Yew allows sending messages to a public worker that hasn't loaded yet, as soon as the worker loads, the queue will be emptied message by message:

yew/src/agent.rs

Lines 601 to 620 in c991c7c

FromWorker::WorkerLoaded => {
// TODO(#944): Send `Connected` message
REMOTE_AGENTS_LOADED.with(|loaded| {
let _ = loaded.borrow_mut().insert(TypeId::of::<AGN>());
});
REMOTE_AGENTS_EARLY_MSGS_QUEUE.with(|queue| {
let mut queue = queue.borrow_mut();
if let Some(msgs) = queue.get_mut(&TypeId::of::<AGN>()) {
for msg in msgs.drain(..) {
cfg_match! {
feature = "std_web" => ({
let worker = &worker;
js! {@{worker}.postMessage(@{msg});};
}),
feature = "web_sys" => worker.post_message_vec(msg),
}
}
}
});

I think we could pull out the code here:

yew/src/agent.rs

Lines 714 to 720 in c991c7c

fn send(&mut self, msg: AGN::Input) {
let msg = ToWorker::ProcessInput(self.id, msg);
if self.worker_is_loaded() {
send_to_remote::<AGN>(&self.worker, msg);
} else {
self.msg_to_queue(msg.pack());
}

and reuse it for non-input messages (Connected, Disconnected, Destroy) so that Connected is sent first from the queue and so that Disconnected and Destroy get sent if the bridge gets dropped before the worker loads.

@jstarry jstarry changed the title [agent.rs] send the Connected message when the WorkerLoaded message is received Send the Connected message when the public worker is loaded Mar 8, 2020
@jstarry jstarry added bug and removed feature-request A feature request labels Mar 8, 2020
TheNeikos added a commit to TheNeikos/yew that referenced this issue Mar 8, 2020
TheNeikos added a commit to TheNeikos/yew that referenced this issue Mar 8, 2020
jstarry pushed a commit that referenced this issue Mar 8, 2020
* Add msg queuing for all messages for public agents

Fixes #944

* Send Disconnected and Destroy correctly

They now use the `send_message` so that they also
get queued in case the worker is not ready yet

* Remove todo about connected message
nicklaswj pushed a commit to nicklaswj/yew that referenced this issue Mar 13, 2020
* Add msg queuing for all messages for public agents

Fixes yewstack#944

* Send Disconnected and Destroy correctly

They now use the `send_message` so that they also
get queued in case the worker is not ready yet

* Remove todo about connected message
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants