// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE // WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. // This was generated using spacetimedb cli version 2.0.0 (commit 5183461758f0c9cb05138a5054c947a7aeb8497e). #nullable enable using System; using SpacetimeDB.ClientApi; using System.Collections.Generic; using System.Runtime.Serialization; namespace SpacetimeDB.Types { public sealed partial class RemoteReducers : RemoteBase { internal RemoteReducers(DbConnection conn) : base(conn) { } internal event Action? InternalOnUnhandledReducerError; } public sealed partial class RemoteProcedures : RemoteBase { internal RemoteProcedures(DbConnection conn) : base(conn) { } } public sealed partial class RemoteTables : RemoteTablesBase { public RemoteTables(DbConnection conn) { AddTable(Person = new(conn)); } } public interface IRemoteDbContext : IDbContext { public event Action? OnUnhandledReducerError; } public sealed class EventContext : IEventContext, IRemoteDbContext { private readonly DbConnection conn; /// /// The event that caused this callback to run. /// public readonly Event Event; /// /// Access to tables in the client cache, which stores a read-only replica of the remote database state. /// /// The returned DbView will have a method to access each table defined by the module. /// public RemoteTables Db => conn.Db; /// /// Access to reducers defined by the module. /// /// The returned RemoteReducers will have a method to invoke each reducer defined by the module, /// plus methods for adding and removing callbacks on each of those reducers. /// public RemoteReducers Reducers => conn.Reducers; /// /// Access to procedures defined by the module. /// /// The returned RemoteProcedures will have a method to invoke each procedure defined by the module, /// with a callback for when the procedure completes and returns a value. /// public RemoteProcedures Procedures => conn.Procedures; /// /// Returns true if the connection is active, i.e. has not yet disconnected. /// public bool IsActive => conn.IsActive; /// /// Close the connection. /// /// Throws an error if the connection is already closed. /// public void Disconnect() { conn.Disconnect(); } /// /// Start building a subscription. /// /// A builder-pattern constructor for subscribing to queries, /// causing matching rows to be replicated into the client cache. public SubscriptionBuilder SubscriptionBuilder() => conn.SubscriptionBuilder(); /// /// Get the Identity of this connection. /// /// This method returns null if the connection was constructed anonymously /// and we have not yet received our newly-generated Identity from the host. /// public Identity? Identity => conn.Identity; /// /// Get this connection's ConnectionId. /// public ConnectionId ConnectionId => conn.ConnectionId; /// /// Register a callback to be called when a reducer with no handler returns an error. /// public event Action? OnUnhandledReducerError { add => Reducers.InternalOnUnhandledReducerError += value; remove => Reducers.InternalOnUnhandledReducerError -= value; } internal EventContext(DbConnection conn, Event Event) { this.conn = conn; this.Event = Event; } } public sealed class ReducerEventContext : IReducerEventContext, IRemoteDbContext { private readonly DbConnection conn; /// /// The reducer event that caused this callback to run. /// public readonly ReducerEvent Event; /// /// Access to tables in the client cache, which stores a read-only replica of the remote database state. /// /// The returned DbView will have a method to access each table defined by the module. /// public RemoteTables Db => conn.Db; /// /// Access to reducers defined by the module. /// /// The returned RemoteReducers will have a method to invoke each reducer defined by the module, /// plus methods for adding and removing callbacks on each of those reducers. /// public RemoteReducers Reducers => conn.Reducers; /// /// Access to procedures defined by the module. /// /// The returned RemoteProcedures will have a method to invoke each procedure defined by the module, /// with a callback for when the procedure completes and returns a value. /// public RemoteProcedures Procedures => conn.Procedures; /// /// Returns true if the connection is active, i.e. has not yet disconnected. /// public bool IsActive => conn.IsActive; /// /// Close the connection. /// /// Throws an error if the connection is already closed. /// public void Disconnect() { conn.Disconnect(); } /// /// Start building a subscription. /// /// A builder-pattern constructor for subscribing to queries, /// causing matching rows to be replicated into the client cache. public SubscriptionBuilder SubscriptionBuilder() => conn.SubscriptionBuilder(); /// /// Get the Identity of this connection. /// /// This method returns null if the connection was constructed anonymously /// and we have not yet received our newly-generated Identity from the host. /// public Identity? Identity => conn.Identity; /// /// Get this connection's ConnectionId. /// public ConnectionId ConnectionId => conn.ConnectionId; /// /// Register a callback to be called when a reducer with no handler returns an error. /// public event Action? OnUnhandledReducerError { add => Reducers.InternalOnUnhandledReducerError += value; remove => Reducers.InternalOnUnhandledReducerError -= value; } internal ReducerEventContext(DbConnection conn, ReducerEvent reducerEvent) { this.conn = conn; Event = reducerEvent; } } public sealed class ErrorContext : IErrorContext, IRemoteDbContext { private readonly DbConnection conn; /// /// The Exception that caused this error callback to be run. /// public readonly Exception Event; Exception IErrorContext.Event { get { return Event; } } /// /// Access to tables in the client cache, which stores a read-only replica of the remote database state. /// /// The returned DbView will have a method to access each table defined by the module. /// public RemoteTables Db => conn.Db; /// /// Access to reducers defined by the module. /// /// The returned RemoteReducers will have a method to invoke each reducer defined by the module, /// plus methods for adding and removing callbacks on each of those reducers. /// public RemoteReducers Reducers => conn.Reducers; /// /// Access to procedures defined by the module. /// /// The returned RemoteProcedures will have a method to invoke each procedure defined by the module, /// with a callback for when the procedure completes and returns a value. /// public RemoteProcedures Procedures => conn.Procedures; /// /// Returns true if the connection is active, i.e. has not yet disconnected. /// public bool IsActive => conn.IsActive; /// /// Close the connection. /// /// Throws an error if the connection is already closed. /// public void Disconnect() { conn.Disconnect(); } /// /// Start building a subscription. /// /// A builder-pattern constructor for subscribing to queries, /// causing matching rows to be replicated into the client cache. public SubscriptionBuilder SubscriptionBuilder() => conn.SubscriptionBuilder(); /// /// Get the Identity of this connection. /// /// This method returns null if the connection was constructed anonymously /// and we have not yet received our newly-generated Identity from the host. /// public Identity? Identity => conn.Identity; /// /// Get this connection's ConnectionId. /// public ConnectionId ConnectionId => conn.ConnectionId; /// /// Register a callback to be called when a reducer with no handler returns an error. /// public event Action? OnUnhandledReducerError { add => Reducers.InternalOnUnhandledReducerError += value; remove => Reducers.InternalOnUnhandledReducerError -= value; } internal ErrorContext(DbConnection conn, Exception error) { this.conn = conn; Event = error; } } public sealed class SubscriptionEventContext : ISubscriptionEventContext, IRemoteDbContext { private readonly DbConnection conn; /// /// Access to tables in the client cache, which stores a read-only replica of the remote database state. /// /// The returned DbView will have a method to access each table defined by the module. /// public RemoteTables Db => conn.Db; /// /// Access to reducers defined by the module. /// /// The returned RemoteReducers will have a method to invoke each reducer defined by the module, /// plus methods for adding and removing callbacks on each of those reducers. /// public RemoteReducers Reducers => conn.Reducers; /// /// Access to procedures defined by the module. /// /// The returned RemoteProcedures will have a method to invoke each procedure defined by the module, /// with a callback for when the procedure completes and returns a value. /// public RemoteProcedures Procedures => conn.Procedures; /// /// Returns true if the connection is active, i.e. has not yet disconnected. /// public bool IsActive => conn.IsActive; /// /// Close the connection. /// /// Throws an error if the connection is already closed. /// public void Disconnect() { conn.Disconnect(); } /// /// Start building a subscription. /// /// A builder-pattern constructor for subscribing to queries, /// causing matching rows to be replicated into the client cache. public SubscriptionBuilder SubscriptionBuilder() => conn.SubscriptionBuilder(); /// /// Get the Identity of this connection. /// /// This method returns null if the connection was constructed anonymously /// and we have not yet received our newly-generated Identity from the host. /// public Identity? Identity => conn.Identity; /// /// Get this connection's ConnectionId. /// public ConnectionId ConnectionId => conn.ConnectionId; /// /// Register a callback to be called when a reducer with no handler returns an error. /// public event Action? OnUnhandledReducerError { add => Reducers.InternalOnUnhandledReducerError += value; remove => Reducers.InternalOnUnhandledReducerError -= value; } internal SubscriptionEventContext(DbConnection conn) { this.conn = conn; } } public sealed class ProcedureEventContext : IProcedureEventContext, IRemoteDbContext { private readonly DbConnection conn; /// /// The procedure event that caused this callback to run. /// public readonly ProcedureEvent Event; /// /// Access to tables in the client cache, which stores a read-only replica of the remote database state. /// /// The returned DbView will have a method to access each table defined by the module. /// public RemoteTables Db => conn.Db; /// /// Access to reducers defined by the module. /// /// The returned RemoteReducers will have a method to invoke each reducer defined by the module, /// plus methods for adding and removing callbacks on each of those reducers. /// public RemoteReducers Reducers => conn.Reducers; /// /// Access to procedures defined by the module. /// /// The returned RemoteProcedures will have a method to invoke each procedure defined by the module, /// with a callback for when the procedure completes and returns a value. /// public RemoteProcedures Procedures => conn.Procedures; /// /// Returns true if the connection is active, i.e. has not yet disconnected. /// public bool IsActive => conn.IsActive; /// /// Close the connection. /// /// Throws an error if the connection is already closed. /// public void Disconnect() { conn.Disconnect(); } /// /// Start building a subscription. /// /// A builder-pattern constructor for subscribing to queries, /// causing matching rows to be replicated into the client cache. public SubscriptionBuilder SubscriptionBuilder() => conn.SubscriptionBuilder(); /// /// Get the Identity of this connection. /// /// This method returns null if the connection was constructed anonymously /// and we have not yet received our newly-generated Identity from the host. /// public Identity? Identity => conn.Identity; /// /// Get this connection's ConnectionId. /// public ConnectionId ConnectionId => conn.ConnectionId; /// /// Register a callback to be called when a reducer with no handler returns an error. /// public event Action? OnUnhandledReducerError { add => Reducers.InternalOnUnhandledReducerError += value; remove => Reducers.InternalOnUnhandledReducerError -= value; } internal ProcedureEventContext(DbConnection conn, ProcedureEvent Event) { this.conn = conn; this.Event = Event; } } /// /// Builder-pattern constructor for subscription queries. /// public sealed class SubscriptionBuilder { private readonly IDbConnection conn; private event Action? Applied; private event Action? Error; /// /// Private API, use conn.SubscriptionBuilder() instead. /// public SubscriptionBuilder(IDbConnection conn) { this.conn = conn; } /// /// Register a callback to run when the subscription is applied. /// public SubscriptionBuilder OnApplied( Action callback ) { Applied += callback; return this; } /// /// Register a callback to run when the subscription fails. /// /// Note that this callback may run either when attempting to apply the subscription, /// in which case Self::on_applied will never run, /// or later during the subscription's lifetime if the module's interface changes, /// in which case Self::on_applied may have already run. /// public SubscriptionBuilder OnError( Action callback ) { Error += callback; return this; } /// /// Add a typed query to this subscription. /// /// This is the entry point for building subscriptions without writing SQL by hand. /// Once a typed query is added, only typed queries may follow (SQL and typed queries cannot be mixed). /// public TypedSubscriptionBuilder AddQuery( Func> build ) { var typed = new TypedSubscriptionBuilder(conn, Applied, Error); return typed.AddQuery(build); } /// /// Subscribe to the following SQL queries. /// /// This method returns immediately, with the data not yet added to the DbConnection. /// The provided callbacks will be invoked once the data is returned from the remote server. /// Data from all the provided queries will be returned at the same time. /// /// See the SpacetimeDB SQL docs for more information on SQL syntax: /// https://spacetimedb.com/docs/sql /// public SubscriptionHandle Subscribe( string[] querySqls ) => new(conn, Applied, Error, querySqls); /// /// Subscribe to all rows from all tables. /// /// This method is intended as a convenience /// for applications where client-side memory use and network bandwidth are not concerns. /// Applications where these resources are a constraint /// should register more precise queries via Self.Subscribe /// in order to replicate only the subset of data which the client needs to function. /// /// This method should not be combined with Self.Subscribe on the same DbConnection. /// A connection may either Self.Subscribe to particular queries, /// or Self.SubscribeToAllTables, but not both. /// Attempting to call Self.Subscribe /// on a DbConnection that has previously used Self.SubscribeToAllTables, /// or vice versa, may misbehave in any number of ways, /// including dropping subscriptions, corrupting the client cache, or panicking. /// public SubscriptionHandle SubscribeToAllTables() => new(conn, Applied, Error, QueryBuilder.AllTablesSqlQueries()); } public sealed class SubscriptionHandle : SubscriptionHandleBase { /// /// Internal API. Construct SubscriptionHandles using conn.SubscriptionBuilder. /// public SubscriptionHandle( IDbConnection conn, Action? onApplied, Action? onError, string[] querySqls ) : base(conn, onApplied, onError, querySqls) { } } public sealed class QueryBuilder { public From From { get; } = new(); internal static string[] AllTablesSqlQueries() => new string[] { new QueryBuilder().From.Person().ToSql(), } ; } public sealed class From { public global::SpacetimeDB.Table Person() => new("Person", new PersonCols("Person"), new PersonIxCols("Person")); } public sealed class TypedSubscriptionBuilder { private readonly IDbConnection conn; private Action? Applied; private Action? Error; private readonly List querySqls = new(); internal TypedSubscriptionBuilder(IDbConnection conn, Action? applied, Action? error) { this.conn = conn; Applied = applied; Error = error; } public TypedSubscriptionBuilder OnApplied(Action callback) { Applied += callback; return this; } public TypedSubscriptionBuilder OnError(Action callback) { Error += callback; return this; } public TypedSubscriptionBuilder AddQuery(Func> build) { var qb = new QueryBuilder(); querySqls.Add(build(qb).ToSql()); return this; } public SubscriptionHandle Subscribe() => new(conn, Applied, Error, querySqls.ToArray()); } public abstract partial class Reducer { private Reducer() { } } public abstract partial class Procedure { private Procedure() { } } public sealed class DbConnection : DbConnectionBase { public override RemoteTables Db { get; } public readonly RemoteReducers Reducers; public readonly RemoteProcedures Procedures; public DbConnection() { Db = new(this); Reducers = new(this); Procedures = new(this); } protected override IEventContext ToEventContext(Event Event) => new EventContext(this, Event); protected override IReducerEventContext ToReducerEventContext(ReducerEvent reducerEvent) => new ReducerEventContext(this, reducerEvent); protected override ISubscriptionEventContext MakeSubscriptionEventContext() => new SubscriptionEventContext(this); protected override IErrorContext ToErrorContext(Exception exception) => new ErrorContext(this, exception); protected override IProcedureEventContext ToProcedureEventContext(ProcedureEvent procedureEvent) => new ProcedureEventContext(this, procedureEvent); protected override bool Dispatch(IReducerEventContext context, Reducer reducer) { var eventContext = (ReducerEventContext)context; return reducer switch { Reducer.Add args => Reducers.InvokeAdd(eventContext, args), Reducer.SayHello args => Reducers.InvokeSayHello(eventContext, args), _ => throw new ArgumentOutOfRangeException("Reducer", $"Unknown reducer {reducer}") }; } public SubscriptionBuilder SubscriptionBuilder() => new(this); public event Action OnUnhandledReducerError { add => Reducers.InternalOnUnhandledReducerError += value; remove => Reducers.InternalOnUnhandledReducerError -= value; } } }