diff options
| author | Max Audron <audron@cocaine.farm> | 2021-10-03 12:39:06 +0200 |
|---|---|---|
| committer | Max Audron <audron@cocaine.farm> | 2021-10-03 12:39:06 +0200 |
| commit | 2e7f1ee492be200fe2fe1b6204d471adec1d656b (patch) | |
| tree | 4bcd3ec610fd68cb4670fc274a8c7a8fd9af7261 /src/message/signalproxy/mod.rs | |
| parent | statetracker: remove druid-widget-nursery (diff) | |
split StatefulSyncable into client and server side traits
This allows to more easily put each side behind a feature flag,
maintaing ease of use when only one is selected, while
still allowing a consumer to enable both sides and use them.
Diffstat (limited to 'src/message/signalproxy/mod.rs')
| -rw-r--r-- | src/message/signalproxy/mod.rs | 92 |
1 files changed, 65 insertions, 27 deletions
diff --git a/src/message/signalproxy/mod.rs b/src/message/signalproxy/mod.rs index d5cdd80..b1e235a 100644 --- a/src/message/signalproxy/mod.rs +++ b/src/message/signalproxy/mod.rs @@ -1,3 +1,5 @@ +use std::convert::TryInto; + use crate::{ deserialize::Deserialize, primitive::{Variant, VariantList}, @@ -50,54 +52,90 @@ pub trait Syncable { /// } /// } /// ``` - fn sync(&self, session: impl SyncProxy, function: &str, params: VariantList); + fn send_sync(&self, session: impl SyncProxy, function: &str, params: VariantList); /// Send a RpcCall - fn rpc(&self, session: impl SyncProxy, function: &str, params: VariantList) { + fn send_rpc(&self, session: impl SyncProxy, function: &str, params: VariantList) { session.rpc(function, params); } } -/// A Stateful Syncable Object -#[allow(unused_variables)] -pub trait StatefulSyncable: Syncable + translation::NetworkMap -// where -// <T as Iterator>::Item: ToString, +/// Methods for a Stateful Syncable object on the client side. +pub trait StatefulSyncableServer: Syncable + translation::NetworkMap where Variant: From<<Self as translation::NetworkMap>::Item>, { - /// Client -> Server: Update the whole object with received data - fn update(&mut self, session: impl SyncProxy, param: <Self as translation::NetworkMap>::Item) + fn sync(&mut self, session: impl SyncProxy, mut msg: crate::message::SyncMessage) where Self: Sized, { - #[cfg(feature = "client")] - { - self.sync(session, "update", vec![self.to_network_map().into()]); + match msg.slot_name.as_str() { + "requestUpdate" => self.request_update(msg.params.pop().unwrap().try_into().unwrap()), + _ => self.sync_custom(session, msg), } - #[cfg(feature = "server")] - { - *self = Self::from_network_map(&mut param); + } + + #[allow(unused_mut, unused_variables)] + fn sync_custom(&mut self, session: impl SyncProxy, mut msg: crate::message::SyncMessage) + where + Self: Sized, + { + match msg.slot_name.as_str() { + _ => (), } } + /// Client -> Server: Update the whole object with received data + fn update(&mut self, session: impl SyncProxy) + where + Self: Sized, + { + self.send_sync(session, "update", vec![self.to_network_map().into()]); + } + /// Server -> Client: Update the whole object with received data - fn request_update( - &mut self, - session: impl SyncProxy, - mut param: <Self as translation::NetworkMap>::Item, - ) where + fn request_update(&mut self, mut param: <Self as translation::NetworkMap>::Item) + where Self: Sized, { - #[cfg(feature = "client")] - { - *self = Self::from_network_map(&mut param); - } - #[cfg(feature = "server")] - { - self.sync(session, "requestUpdate", vec![self.to_network_map().into()]); + *self = Self::from_network_map(&mut param); + } +} + +/// Methods for a Stateful Syncable object on the server side. +pub trait StatefulSyncableClient: Syncable + translation::NetworkMap { + fn sync(&mut self, session: impl SyncProxy, mut msg: crate::message::SyncMessage) + where + Self: Sized, + { + match msg.slot_name.as_str() { + "update" => self.update(msg.params.pop().unwrap().try_into().unwrap()), + _ => self.sync_custom(session, msg), } } + + fn sync_custom(&mut self, _session: impl SyncProxy, _msg: crate::message::SyncMessage) + where + Self: Sized, + { + () + } + + /// Client -> Server: Update the whole object with received data + fn update(&mut self, mut param: <Self as translation::NetworkMap>::Item) + where + Self: Sized, + { + *self = Self::from_network_map(&mut param); + } + + /// Server -> Client: Update the whole object with received data + fn request_update(&mut self, session: impl SyncProxy) + where + Self: Sized, + { + self.send_sync(session, "requestUpdate", vec![self.to_network_map().into()]); + } } #[derive(Clone, Debug, std::cmp::PartialEq)] |
