Return error if subscribing to an entity that was already subscribed to
This commit is contained in:
parent
d7f56d6126
commit
f995d07542
2 changed files with 23 additions and 17 deletions
|
@ -473,18 +473,22 @@ impl Client {
|
||||||
pub fn subscribe_to_entity<T: Entity>(
|
pub fn subscribe_to_entity<T: Entity>(
|
||||||
self: &Arc<Self>,
|
self: &Arc<Self>,
|
||||||
remote_id: u64,
|
remote_id: u64,
|
||||||
) -> PendingEntitySubscription<T> {
|
) -> Result<PendingEntitySubscription<T>> {
|
||||||
let id = (TypeId::of::<T>(), remote_id);
|
let id = (TypeId::of::<T>(), remote_id);
|
||||||
self.state
|
|
||||||
.write()
|
let mut state = self.state.write();
|
||||||
|
if state.entities_by_type_and_remote_id.contains_key(&id) {
|
||||||
|
return Err(anyhow!("already subscribed to entity"));
|
||||||
|
} else {
|
||||||
|
state
|
||||||
.entities_by_type_and_remote_id
|
.entities_by_type_and_remote_id
|
||||||
.insert(id, WeakSubscriber::Pending(Default::default()));
|
.insert(id, WeakSubscriber::Pending(Default::default()));
|
||||||
|
Ok(PendingEntitySubscription {
|
||||||
PendingEntitySubscription {
|
|
||||||
client: self.clone(),
|
client: self.clone(),
|
||||||
remote_id,
|
remote_id,
|
||||||
consumed: false,
|
consumed: false,
|
||||||
_entity_type: PhantomData,
|
_entity_type: PhantomData,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1605,14 +1609,17 @@ mod tests {
|
||||||
|
|
||||||
let _subscription1 = client
|
let _subscription1 = client
|
||||||
.subscribe_to_entity(1)
|
.subscribe_to_entity(1)
|
||||||
|
.unwrap()
|
||||||
.set_model(&model1, &mut cx.to_async());
|
.set_model(&model1, &mut cx.to_async());
|
||||||
let _subscription2 = client
|
let _subscription2 = client
|
||||||
.subscribe_to_entity(2)
|
.subscribe_to_entity(2)
|
||||||
|
.unwrap()
|
||||||
.set_model(&model2, &mut cx.to_async());
|
.set_model(&model2, &mut cx.to_async());
|
||||||
// Ensure dropping a subscription for the same entity type still allows receiving of
|
// Ensure dropping a subscription for the same entity type still allows receiving of
|
||||||
// messages for other entity IDs of the same type.
|
// messages for other entity IDs of the same type.
|
||||||
let subscription3 = client
|
let subscription3 = client
|
||||||
.subscribe_to_entity(3)
|
.subscribe_to_entity(3)
|
||||||
|
.unwrap()
|
||||||
.set_model(&model3, &mut cx.to_async());
|
.set_model(&model3, &mut cx.to_async());
|
||||||
drop(subscription3);
|
drop(subscription3);
|
||||||
|
|
||||||
|
|
|
@ -463,7 +463,7 @@ impl Project {
|
||||||
) -> Result<ModelHandle<Self>> {
|
) -> Result<ModelHandle<Self>> {
|
||||||
client.authenticate_and_connect(true, &cx).await?;
|
client.authenticate_and_connect(true, &cx).await?;
|
||||||
|
|
||||||
let subscription = client.subscribe_to_entity(remote_id);
|
let subscription = client.subscribe_to_entity(remote_id)?;
|
||||||
let response = client
|
let response = client
|
||||||
.request_envelope(proto::JoinProject {
|
.request_envelope(proto::JoinProject {
|
||||||
project_id: remote_id,
|
project_id: remote_id,
|
||||||
|
@ -989,6 +989,11 @@ impl Project {
|
||||||
if self.client_state.is_some() {
|
if self.client_state.is_some() {
|
||||||
return Err(anyhow!("project was already shared"));
|
return Err(anyhow!("project was already shared"));
|
||||||
}
|
}
|
||||||
|
self.client_subscriptions.push(
|
||||||
|
self.client
|
||||||
|
.subscribe_to_entity(project_id)?
|
||||||
|
.set_model(&cx.handle(), &mut cx.to_async()),
|
||||||
|
);
|
||||||
|
|
||||||
for open_buffer in self.opened_buffers.values_mut() {
|
for open_buffer in self.opened_buffers.values_mut() {
|
||||||
match open_buffer {
|
match open_buffer {
|
||||||
|
@ -1025,12 +1030,6 @@ impl Project {
|
||||||
.log_err();
|
.log_err();
|
||||||
}
|
}
|
||||||
|
|
||||||
self.client_subscriptions.push(
|
|
||||||
self.client
|
|
||||||
.subscribe_to_entity(project_id)
|
|
||||||
.set_model(&cx.handle(), &mut cx.to_async()),
|
|
||||||
);
|
|
||||||
|
|
||||||
let (metadata_changed_tx, mut metadata_changed_rx) = mpsc::unbounded();
|
let (metadata_changed_tx, mut metadata_changed_rx) = mpsc::unbounded();
|
||||||
self.client_state = Some(ProjectClientState::Local {
|
self.client_state = Some(ProjectClientState::Local {
|
||||||
remote_id: project_id,
|
remote_id: project_id,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue