Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/codegen/src/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -979,7 +979,7 @@ fn print_db_update_defn(module: &ModuleDef, out: &mut Indenter) {
for table in iter_tables(module) {
writeln!(
out,
"{}: __sdk::TableUpdate<{}>,",
"pub {}: __sdk::TableUpdate<{}>,",
table_method_name(&table.name),
type_ref_name(module, table.product_type_ref),
);
Expand Down
25 changes: 24 additions & 1 deletion sdks/rust/src/db_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ use std::{
use tokio::{
runtime::{self, Runtime},
sync::Mutex as TokioMutex,
sync::mpsc::UnboundedSender as TokioSender,
};

pub(crate) type SharedCell<T> = Arc<StdMutex<T>>;
Expand Down Expand Up @@ -78,6 +79,9 @@ pub struct DbContextImpl<M: SpacetimeModule> {
/// from which [Self::apply_pending_mutations] and friends read mutations.
pending_mutations_recv: Arc<TokioMutex<mpsc::UnboundedReceiver<PendingMutation<M>>>>,

/// Send channel for all database updates
update_send: Option<TokioSender<M::DbUpdate>>,

/// This connection's `Identity`.
///
/// May be `None` if we connected anonymously
Expand All @@ -103,6 +107,7 @@ impl<M: SpacetimeModule> Clone for DbContextImpl<M> {
recv: Arc::clone(&self.recv),
pending_mutations_send: self.pending_mutations_send.clone(),
pending_mutations_recv: Arc::clone(&self.pending_mutations_recv),
update_send: self.update_send.clone(),
identity: Arc::clone(&self.identity),
connection_id: Arc::clone(&self.connection_id),
}
Expand Down Expand Up @@ -240,7 +245,12 @@ impl<M: SpacetimeModule> DbContextImpl<M> {
// so that it will be unlocked when callbacks run.
let applied_diff = {
let mut cache = self.cache.lock().unwrap();
update.apply_to_client_cache(&mut *cache)
if let Some(update_send) = &self.update_send {
update_send.send(update).unwrap();
Default::default()
} else {
update.apply_to_client_cache(&mut *cache)
}
};
let mut inner = self.inner.lock().unwrap();

Expand Down Expand Up @@ -769,6 +779,8 @@ pub struct DbConnectionBuilder<M: SpacetimeModule> {
on_connect_error: Option<OnConnectErrorCallback<M>>,
on_disconnect: Option<OnDisconnectCallback<M>>,

update_send: Option<TokioSender<M::DbUpdate>>,

params: WsParams,
}

Expand Down Expand Up @@ -816,6 +828,7 @@ impl<M: SpacetimeModule> DbConnectionBuilder<M> {
on_connect: None,
on_connect_error: None,
on_disconnect: None,
update_send: None,
params: <_>::default(),
}
}
Expand Down Expand Up @@ -901,6 +914,7 @@ but you must call one of them, or else the connection will never progress.
recv: Arc::new(TokioMutex::new(parsed_recv_chan)),
pending_mutations_send,
pending_mutations_recv: Arc::new(TokioMutex::new(pending_mutations_recv)),
update_send: self.update_send,
identity: Arc::new(StdMutex::new(None)),
connection_id: Arc::new(StdMutex::new(connection_id_override)),
};
Expand Down Expand Up @@ -980,6 +994,15 @@ but you must call one of them, or else the connection will never progress.
self
}

/// Register a channel that receives all database updates instead of the
/// client cache. If you use this method, none of the [`TableHandle`]
/// callbacks will ever fire, and no data is stored in the [`ClientCache`].
#[doc(hidden)]
pub fn with_update_channel(mut self, sender: TokioSender<M::DbUpdate>) -> Self {
self.update_send = Some(sender);
self
}

/// Register a callback to run when the connection is successfully initiated.
///
/// The callback will receive three arguments:
Expand Down
4 changes: 2 additions & 2 deletions sdks/rust/src/spacetime_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ pub trait SpacetimeModule: Send + Sync + 'static {
type SetReducerFlags: InModule<Module = Self> + Send + 'static;

/// Parsed and typed analogue of [`crate::ws::DatabaseUpdate`].
type DbUpdate: DbUpdate<Module = Self>;
type DbUpdate: DbUpdate<Module = Self> + Send + 'static;

/// The result of applying `Self::DbUpdate` to the client cache.
type AppliedDiff<'r>: AppliedDiff<'r, Module = Self>;
type AppliedDiff<'r>: AppliedDiff<'r, Module = Self> + Default;

/// Module-specific `SubscriptionHandle` type, representing an ongoing incremental subscription to a query.
type SubscriptionHandle: SubscriptionHandle<Module = Self>;
Expand Down