Skip to content

Commit

Permalink
[release/v1.0.0-rc1]: Merge remote-tracking branch 'origin/centril/we…
Browse files Browse the repository at this point in the history
…bsocket-light' into release/v1.0.0-rc1
  • Loading branch information
bfops committed Oct 24, 2024
2 parents ba16f99 + 9c85842 commit 1fb6396
Show file tree
Hide file tree
Showing 195 changed files with 4,401 additions and 186 deletions.
28 changes: 24 additions & 4 deletions crates/cli/src/subcommands/generate/csharp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ fn default_init(ctx: &GenCtx, ty: &AlgebraicType) -> Option<&'static str> {
AlgebraicType::Sum(sum_type) if sum_type.is_option() || sum_type.is_simple_enum() => None,
// TODO: generate some proper default here (what would it be for tagged enums?).
AlgebraicType::Sum(_) => Some("null!"),
// For product types, arrays, and maps, we can use the default constructor.
// For product types and arrays, we can use the default constructor.
AlgebraicType::Product(_) | AlgebraicType::Array(_) => Some("new()"),
// Strings must have explicit default value of "".
AlgebraicType::String => Some(r#""""#),
Expand Down Expand Up @@ -654,7 +654,11 @@ pub fn autogen_csharp_globals(ctx: &GenCtx, items: &[GenItem], namespace: &str)

writeln!(output, "public sealed class RemoteReducers : RemoteBase<DbConnection>");
indented_block(&mut output, |output| {
writeln!(output, "internal RemoteReducers(DbConnection conn) : base(conn) {{}}");
writeln!(
output,
"internal RemoteReducers(DbConnection conn, SetReducerFlags SetReducerFlags) : base(conn) {{ this.SetCallReducerFlags = SetReducerFlags; }}"
);
writeln!(output, "internal readonly SetReducerFlags SetCallReducerFlags;");

for reducer in &reducers {
let func_name = &*reducer.name;
Expand Down Expand Up @@ -696,7 +700,7 @@ pub fn autogen_csharp_globals(ctx: &GenCtx, items: &[GenItem], namespace: &str)
indented_block(output, |output| {
writeln!(
output,
"conn.InternalCallReducer(new {func_name_pascal_case} {{ {field_inits} }});"
"conn.InternalCallReducer(new {func_name_pascal_case} {{ {field_inits} }}, this.SetCallReducerFlags.{func_name_pascal_case}Flags);"
);
});
writeln!(output);
Expand Down Expand Up @@ -729,12 +733,25 @@ pub fn autogen_csharp_globals(ctx: &GenCtx, items: &[GenItem], namespace: &str)
});
writeln!(output);

writeln!(output, "public sealed class SetReducerFlags");
indented_block(&mut output, |output| {
writeln!(output, "internal SetReducerFlags() {{ }}");
for reducer in &reducers {
let func_name = &*reducer.name;
let func_name_pascal_case = func_name.to_case(Case::Pascal);
writeln!(output, "internal CallReducerFlags {func_name_pascal_case}Flags;");
writeln!(output, "public void {func_name_pascal_case}(CallReducerFlags flags) {{ this.{func_name_pascal_case}Flags = flags; }}");
}
});
writeln!(output);

writeln!(
output,
"public partial record EventContext : DbContext<RemoteTables>, IEventContext"
);
indented_block(&mut output, |output| {
writeln!(output, "public readonly RemoteReducers Reducers;");
writeln!(output, "public readonly SetReducerFlags SetReducerFlags;");
writeln!(output, "public readonly Event<Reducer> Event;");
writeln!(output);
writeln!(
Expand All @@ -743,6 +760,7 @@ pub fn autogen_csharp_globals(ctx: &GenCtx, items: &[GenItem], namespace: &str)
);
indented_block(output, |output| {
writeln!(output, "Reducers = conn.Reducers;");
writeln!(output, "SetReducerFlags = conn.SetReducerFlags;");
writeln!(output, "Event = reducerEvent;");
});
});
Expand All @@ -768,11 +786,13 @@ pub fn autogen_csharp_globals(ctx: &GenCtx, items: &[GenItem], namespace: &str)
indented_block(&mut output, |output| {
writeln!(output, "public readonly RemoteTables Db = new();");
writeln!(output, "public readonly RemoteReducers Reducers;");
writeln!(output, "public readonly SetReducerFlags SetReducerFlags;");
writeln!(output);

writeln!(output, "public DbConnection()");
indented_block(output, |output| {
writeln!(output, "Reducers = new(this);");
writeln!(output, "SetReducerFlags = new();");
writeln!(output, "Reducers = new(this, this.SetReducerFlags);");
writeln!(output);

for item in items {
Expand Down
59 changes: 59 additions & 0 deletions crates/cli/src/subcommands/generate/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ Requested namespace: {namespace}",

let reducer_name = reducer.name.deref();
let func_name = reducer_function_name(reducer);
let set_reducer_flags_trait = format!("set_flags_for_{func_name}");
let args_type = reducer_args_type_name(&reducer.name);

define_struct_for_product(module, out, &args_type, &reducer.params_for_generate.elements);
Expand Down Expand Up @@ -431,6 +432,26 @@ impl {func_name} for super::RemoteReducers {{
self.imp.remove_on_reducer::<{args_type}>({reducer_name:?}, callback.0)
}}
}}
#[allow(non_camel_case_types)]
#[doc(hidden)]
/// Extension trait for setting the call-flags for the reducer `{reducer_name}`.
///
/// Implemented for [`super::SetReducerFlags`].
///
/// This type is currently unstable and may be removed without a major version bump.
pub trait {set_reducer_flags_trait} {{
/// Set the call-reducer flags for the reducer `{reducer_name}` to `flags`.
///
/// This type is currently unstable and may be removed without a major version bump.
fn {func_name}(&self, flags: __ws::CallReducerFlags);
}}
impl {set_reducer_flags_trait} for super::SetReducerFlags {{
fn {func_name}(&self, flags: __ws::CallReducerFlags) {{
self.imp.set_call_reducer_flags({reducer_name:?}, flags);
}}
}}
"
);

Expand Down Expand Up @@ -985,6 +1006,7 @@ impl __sdk::spacetime_module::SpacetimeModule for RemoteModule {{
type Reducer = Reducer;
type DbView = RemoteTables;
type Reducers = RemoteReducers;
type SetReducerFlags = SetReducerFlags;
type DbUpdate = DbUpdate;
type SubscriptionHandle = SubscriptionHandle;
}}
Expand All @@ -999,6 +1021,20 @@ impl __sdk::spacetime_module::InModule for RemoteReducers {{
type Module = RemoteModule;
}}
#[doc(hidden)]
/// The `set_reducer_flags` field of [`DbConnection`],
/// with methods provided by extension traits for each reducer defined by the module.
/// Each method sets the flags for the reducer with the same name.
///
/// This type is currently unstable and may be removed without a major version bump.
pub struct SetReducerFlags {{
imp: __sdk::db_connection::DbContextImpl<RemoteModule>,
}}
impl __sdk::spacetime_module::InModule for SetReducerFlags {{
type Module = RemoteModule;
}}
/// The `db` field of [`EventContext`] and [`DbConnection`],
/// with methods provided by extension traits for each table defined by the module.
pub struct RemoteTables {{
Expand Down Expand Up @@ -1030,6 +1066,12 @@ pub struct DbConnection {{
pub db: RemoteTables,
/// Access to reducers defined by the module via extension traits implemented for [`RemoteReducers`].
pub reducers: RemoteReducers,
#[doc(hidden)]
/// Access to setting the call-flags of each reducer defined for each reducer defined by the module
/// via extension traits implemented for [`SetReducerFlags`].
///
/// This type is currently unstable and may be removed without a major version bump.
pub set_reducer_flags: SetReducerFlags,
imp: __sdk::db_connection::DbContextImpl<RemoteModule>,
}}
Expand All @@ -1041,13 +1083,17 @@ impl __sdk::spacetime_module::InModule for DbConnection {{
impl __sdk::db_context::DbContext for DbConnection {{
type DbView = RemoteTables;
type Reducers = RemoteReducers;
type SetReducerFlags = SetReducerFlags;
fn db(&self) -> &Self::DbView {{
&self.db
}}
fn reducers(&self) -> &Self::Reducers {{
&self.reducers
}}
fn set_reducer_flags(&self) -> &Self::SetReducerFlags {{
&self.set_reducer_flags
}}
fn is_active(&self) -> bool {{
self.imp.is_active()
Expand Down Expand Up @@ -1147,6 +1193,7 @@ impl __sdk::spacetime_module::DbConnection for DbConnection {{
Self {{
db: RemoteTables {{ imp: imp.clone() }},
reducers: RemoteReducers {{ imp: imp.clone() }},
set_reducer_flags: SetReducerFlags {{ imp: imp.clone() }},
imp,
}}
}}
Expand All @@ -1159,6 +1206,11 @@ pub struct EventContext {{
pub db: RemoteTables,
/// Access to reducers defined by the module via extension traits implemented for [`RemoteReducers`].
pub reducers: RemoteReducers,
/// Access to setting the call-flags of each reducer defined for each reducer defined by the module
/// via extension traits implemented for [`SetReducerFlags`].
///
/// This type is currently unstable and may be removed without a major version bump.
pub set_reducer_flags: SetReducerFlags,
/// The event which caused these callbacks to run.
pub event: __sdk::event::Event<Reducer>,
imp: __sdk::db_connection::DbContextImpl<RemoteModule>,
Expand All @@ -1171,13 +1223,17 @@ impl __sdk::spacetime_module::InModule for EventContext {{
impl __sdk::db_context::DbContext for EventContext {{
type DbView = RemoteTables;
type Reducers = RemoteReducers;
type SetReducerFlags = SetReducerFlags;
fn db(&self) -> &Self::DbView {{
&self.db
}}
fn reducers(&self) -> &Self::Reducers {{
&self.reducers
}}
fn set_reducer_flags(&self) -> &Self::SetReducerFlags {{
&self.set_reducer_flags
}}
fn is_active(&self) -> bool {{
self.imp.is_active()
Expand Down Expand Up @@ -1209,6 +1265,7 @@ impl __sdk::spacetime_module::EventContext for EventContext {{
Self {{
db: RemoteTables {{ imp: imp.clone() }},
reducers: RemoteReducers {{ imp: imp.clone() }},
set_reducer_flags: SetReducerFlags {{ imp: imp.clone() }},
event,
imp,
}}
Expand Down Expand Up @@ -1239,11 +1296,13 @@ impl __sdk::spacetime_module::SubscriptionHandle for SubscriptionHandle {{
pub trait RemoteDbContext: __sdk::DbContext<
DbView = RemoteTables,
Reducers = RemoteReducers,
SetReducerFlags = SetReducerFlags,
SubscriptionBuilder = __sdk::subscription::SubscriptionBuilder<RemoteModule>,
> {{}}
impl<Ctx: __sdk::DbContext<
DbView = RemoteTables,
Reducers = RemoteReducers,
SetReducerFlags = SetReducerFlags,
SubscriptionBuilder = __sdk::subscription::SubscriptionBuilder<RemoteModule>,
>> RemoteDbContext for Ctx {{}}
",
Expand Down
44 changes: 37 additions & 7 deletions crates/cli/src/subcommands/generate/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,8 +388,11 @@ eventContextConstructor: (imp: DBConnectionImpl, event: Event<Reducer>) => {{
dbViewConstructor: (imp: DBConnectionImpl) => {{
return new RemoteTables(imp);
}},
reducersConstructor: (imp: DBConnectionImpl) => {{
return new RemoteReducers(imp);
reducersConstructor: (imp: DBConnectionImpl, setReducerFlags: SetReducerFlags) => {{
return new RemoteReducers(imp, setReducerFlags);
}},
setReducerFlagsConstructor: () => {{
return new SetReducerFlags();
}}"
);
out.dedent(1);
Expand All @@ -405,6 +408,10 @@ reducersConstructor: (imp: DBConnectionImpl) => {{

out.newline();

print_set_reducer_flags(module, out);

out.newline();

print_remote_tables(module, out);

out.newline();
Expand All @@ -415,7 +422,7 @@ reducersConstructor: (imp: DBConnectionImpl) => {{

writeln!(
out,
"export type EventContext = EventContextInterface<RemoteTables, RemoteReducers, Reducer>;"
"export type EventContext = EventContextInterface<RemoteTables, RemoteReducers, SetReducerFlags, Reducer>;"
);

vec![("index.ts".to_string(), (output.into_inner()))]
Expand All @@ -425,7 +432,10 @@ reducersConstructor: (imp: DBConnectionImpl) => {{
fn print_remote_reducers(module: &ModuleDef, out: &mut Indenter) {
writeln!(out, "export class RemoteReducers {{");
out.indent(1);
writeln!(out, "constructor(private connection: DBConnectionImpl) {{}}");
writeln!(
out,
"constructor(private connection: DBConnectionImpl, private setCallReducerFlags: SetReducerFlags) {{}}"
);
out.newline();

for reducer in iter_reducers(module) {
Expand Down Expand Up @@ -455,7 +465,7 @@ fn print_remote_reducers(module: &ModuleDef, out: &mut Indenter) {
out.with_indent(|out| {
writeln!(
out,
"this.connection.callReducer(\"{reducer_name}\", new Uint8Array(0));"
"this.connection.callReducer(\"{reducer_name}\", new Uint8Array(0), this.setCallReducerFlags.{reducer_function_name}Flags);"
);
});
} else {
Expand All @@ -468,7 +478,7 @@ fn print_remote_reducers(module: &ModuleDef, out: &mut Indenter) {
"{reducer_variant}.getTypeScriptAlgebraicType().serialize(__writer, __args);"
);
writeln!(out, "let __argsBuffer = __writer.getBuffer();");
writeln!(out, "this.connection.callReducer(\"{reducer_name}\", __argsBuffer);");
writeln!(out, "this.connection.callReducer(\"{reducer_name}\", __argsBuffer, this.setCallReducerFlags.{reducer_function_name}Flags);");
});
}
writeln!(out, "}}");
Expand Down Expand Up @@ -503,6 +513,25 @@ fn print_remote_reducers(module: &ModuleDef, out: &mut Indenter) {
writeln!(out, "}}");
}

fn print_set_reducer_flags(module: &ModuleDef, out: &mut Indenter) {
writeln!(out, "export class SetReducerFlags {{");
out.indent(1);

for reducer in iter_reducers(module) {
let reducer_function_name = reducer_function_name(reducer);
writeln!(out, "{reducer_function_name}Flags: CallReducerFlags = 'FullUpdate';");
writeln!(out, "{reducer_function_name}(flags: CallReducerFlags) {{");
out.with_indent(|out| {
writeln!(out, "this.{reducer_function_name}Flags = flags;");
});
writeln!(out, "}}");
out.newline();
}

out.dedent(1);
writeln!(out, "}}");
}

fn print_remote_tables(module: &ModuleDef, out: &mut Indenter) {
writeln!(out, "export class RemoteTables {{");
out.indent(1);
Expand Down Expand Up @@ -533,7 +562,7 @@ fn print_remote_tables(module: &ModuleDef, out: &mut Indenter) {
fn print_db_connection(_module: &ModuleDef, out: &mut Indenter) {
writeln!(
out,
"export class DBConnection extends DBConnectionImpl<RemoteTables, RemoteReducers> {{"
"export class DBConnection extends DBConnectionImpl<RemoteTables, RemoteReducers, SetReducerFlags> {{"
);
out.indent(1);
writeln!(out, "static builder = (): DBConnectionBuilder<DBConnection> => {{");
Expand Down Expand Up @@ -575,6 +604,7 @@ fn print_spacetimedb_imports(out: &mut Indenter) {
"DBConnectionBuilder",
"TableCache",
"BinaryWriter",
"CallReducerFlags",
"EventContextInterface",
"BinaryReader",
"DBConnectionImpl",
Expand Down
17 changes: 10 additions & 7 deletions crates/cli/src/subcommands/subscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ where
S: TryStream<Ok = WsMessage> + Unpin,
S::Error: std::error::Error + Send + Sync + 'static,
{
const RECV_TX_UPDATE: &str = "protocol error: received transaction update before initial subscription update";

while let Some(msg) = ws.try_next().await? {
let Some(msg) = parse_msg_json(&msg) else { continue };
match msg {
Expand All @@ -207,12 +209,12 @@ where
}
break;
}
ws::ServerMessage::TransactionUpdate(ws::TransactionUpdate { status, .. }) => {
let message = match status {
ws::UpdateStatus::Failed(msg) => msg,
_ => "protocol error: received transaction update before initial subscription update".into(),
};
anyhow::bail!(message)
ws::ServerMessage::TransactionUpdate(ws::TransactionUpdate { status, .. }) => anyhow::bail!(match status {
ws::UpdateStatus::Failed(msg) => msg,
_ => RECV_TX_UPDATE.into(),
}),
ws::ServerMessage::TransactionUpdateLight(ws::TransactionUpdateLight { .. }) => {
anyhow::bail!(RECV_TX_UPDATE)
}
_ => continue,
}
Expand Down Expand Up @@ -248,7 +250,8 @@ where
ws::ServerMessage::InitialSubscription(_) => {
anyhow::bail!("protocol error: received a second initial subscription update")
}
ws::ServerMessage::TransactionUpdate(ws::TransactionUpdate {
ws::ServerMessage::TransactionUpdateLight(ws::TransactionUpdateLight { update, .. })
| ws::ServerMessage::TransactionUpdate(ws::TransactionUpdate {
status: ws::UpdateStatus::Committed(update),
..
}) => {
Expand Down
Loading

0 comments on commit 1fb6396

Please sign in to comment.