Skip to content

Commit

Permalink
revert more diff for easier review
Browse files Browse the repository at this point in the history
  • Loading branch information
Centril committed Nov 5, 2024
1 parent 7ef6ce2 commit 273d4f1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 22 deletions.
5 changes: 2 additions & 3 deletions crates/sdk/src/client_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ impl<M: SpacetimeModule> Default for ClientCache<M> {
type TableCacheMap<Row> = HashMap<&'static str, Arc<TableCache<Row>>>;

impl<M: SpacetimeModule> ClientCache<M> {
/// Get a handle on the [`TableCache`] which stores rows of type `Row`
/// for the table with the name `table_name`.
/// Get a handle on the [`TableCache`] which stores rows of type `Row` for the table `table_name`.
pub(crate) fn get_table<Row: InModule<Module = M> + Send + Sync + 'static>(
&self,
table_name: &'static str,
Expand All @@ -105,7 +104,7 @@ impl<M: SpacetimeModule> ClientCache<M> {
}

/// Apply all the mutations in `diff`
/// to the [`TableCache`] which stores rows of type `Row` for the table with id `table_id`.
/// to the [`TableCache`] which stores rows of type `Row` for the table `table_name`.
pub fn apply_diff_to_table<Row: InModule<Module = M> + Clone + Send + Sync + 'static>(
&mut self,
table_name: &'static str,
Expand Down
44 changes: 26 additions & 18 deletions crates/sdk/src/db_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,8 +389,15 @@ impl<M: SpacetimeModule> DbContextImpl<M> {
.reducer_callbacks
.remove_on_reducer(reducer, callback_id);
}
PendingMutation::SetCallReducerFlags { reducer, flags } => {
self.inner.lock().unwrap().call_reducer_flags.set_flags(reducer, flags);
PendingMutation::SetCallReducerFlags {
reducer: reducer_name,
flags,
} => {
self.inner
.lock()
.unwrap()
.call_reducer_flags
.set_flags(reducer_name, flags);
}
};
Ok(())
Expand Down Expand Up @@ -568,21 +575,19 @@ impl<M: SpacetimeModule> DbContextImpl<M> {
}

/// Called by autogenerated reducer invocation methods.
///
/// The `reducer_idx` is the position of the reducer,
/// in the known list of reducers, sorted lexicographically by name.
/// It is not necessarily the ID of the reducer itself,
/// which is resolved using this index.
pub fn call_reducer<Args: Serialize + InModule<Module = M>>(
&self,
reducer: &'static str,
reducer_name: &'static str,
args: Args,
) -> Result<()> {
// TODO(centril, perf): consider using a thread local pool to avoid allocating each time.
let args_bsatn = bsatn::to_vec(&args)
.with_context(|| format!("Failed to BSATN serialize arguments for reducer {reducer}"))?;
.with_context(|| format!("Failed to BSATN serialize arguments for reducer {reducer_name}"))?;

self.queue_mutation(PendingMutation::CallReducer { reducer, args_bsatn });
self.queue_mutation(PendingMutation::CallReducer {
reducer: reducer_name,
args_bsatn,
});
Ok(())
}

Expand All @@ -594,12 +599,12 @@ impl<M: SpacetimeModule> DbContextImpl<M> {
/// Called by autogenerated reducer callback methods.
pub fn on_reducer<Args: Deserialize<'static> + InModule<Module = M> + 'static>(
&self,
reducer: &'static str,
reducer_name: &'static str,
mut callback: impl FnMut(&M::EventContext, &Args) + Send + 'static,
) -> CallbackId {
let callback_id = CallbackId::get_next();
self.queue_mutation(PendingMutation::AddReducerCallback {
reducer,
reducer: reducer_name,
callback_id,
callback: Box::new(move |ctx, args| {
let args = args.downcast_ref::<Args>().unwrap();
Expand All @@ -610,8 +615,11 @@ impl<M: SpacetimeModule> DbContextImpl<M> {
}

/// Called by autogenerated reducer callback methods.
pub fn remove_on_reducer<Args: InModule<Module = M>>(&self, reducer: &'static str, callback_id: CallbackId) {
self.queue_mutation(PendingMutation::RemoveReducerCallback { reducer, callback_id });
pub fn remove_on_reducer<Args: InModule<Module = M>>(&self, reducer_name: &'static str, callback: CallbackId) {
self.queue_mutation(PendingMutation::RemoveReducerCallback {
reducer: reducer_name,
callback_id: callback,
});
}

/// Called by the autogenerated `DbConnection` method of the same name.
Expand Down Expand Up @@ -661,22 +669,22 @@ pub(crate) struct DbContextImplInner<M: SpacetimeModule> {
processed_after_connecting: bool,
}

/// Maps reducer indices to the flags to use for `.call_reducer(..)`.
/// Maps reducer names to the flags to use for `.call_reducer(..)`.
#[derive(Default, Clone)]
struct CallReducerFlagsMap {
map: HashMap<&'static str, CallReducerFlags>,
}

impl CallReducerFlagsMap {
/// Returns the [`CallReducerFlags`] for `reducer_name`.
fn get_flags(&self, reducer_name: &'static str) -> CallReducerFlags {
self.map.get(&reducer_name).copied().unwrap_or_default()
fn get_flags(&self, reducer_name: &str) -> CallReducerFlags {
self.map.get(reducer_name).copied().unwrap_or_default()
}

/// Sets the [`CallReducerFlags`] for `reducer_name` to `flags`.
pub fn set_flags(&mut self, reducer_name: &'static str, flags: CallReducerFlags) {
if flags == <_>::default() {
self.map.remove(&reducer_name)
self.map.remove(reducer_name)
} else {
self.map.insert(reducer_name, flags)
};
Expand Down
2 changes: 1 addition & 1 deletion crates/sdk/src/spacetime_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ pub trait Reducer: InModule + std::fmt::Debug + Send + 'static + Sized
where
Self::Module: SpacetimeModule<Reducer = Self>,
{
/// Get the name of the reducer variant stored in this instance.
/// Get the string name of the reducer variant stored in this instance.
///
/// Used by [`crate::callbacks::ReducerCallbacks::invoke_on_reducer`] to determine which callback to run.
fn reducer_name(&self) -> &'static str;
Expand Down

0 comments on commit 273d4f1

Please sign in to comment.