Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework event contexts in C# #240

Merged
merged 6 commits into from
Feb 7, 2025
Merged
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
26 changes: 10 additions & 16 deletions examples~/quickstart/client/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,27 +104,21 @@ void Message_OnInsert(EventContext ctx, Message insertedValue)
}
}

void Reducer_OnSetNameEvent(EventContext ctx, string name)
void Reducer_OnSetNameEvent(ReducerEventContext ctx, string name)
{
if (ctx.Event is Event<Reducer>.Reducer reducer)
var e = ctx.Event;
if (e.CallerIdentity == local_identity && e.Status is Status.Failed(var error))
{
var e = reducer.ReducerEvent;
if (e.CallerIdentity == local_identity && e.Status is Status.Failed(var error))
{
Console.Write($"Failed to change name to {name}: {error}");
}
Console.Write($"Failed to change name to {name}: {error}");
}
}

void Reducer_OnSendMessageEvent(EventContext ctx, string text)
void Reducer_OnSendMessageEvent(ReducerEventContext ctx, string text)
{
if (ctx.Event is Event<Reducer>.Reducer reducer)
var e = ctx.Event;
if (e.CallerIdentity == local_identity && e.Status is Status.Failed(var error))
{
var e = reducer.ReducerEvent;
if (e.CallerIdentity == local_identity && e.Status is Status.Failed(var error))
{
Console.Write($"Failed to send message {text}: {error}");
}
Console.Write($"Failed to send message {text}: {error}");
}
}

Expand All @@ -134,7 +128,7 @@ void OnConnect(DbConnection conn, Identity identity, string authToken)
AuthToken.SaveToken(authToken);

var subscriptions = 0;
SubscriptionBuilder<EventContext>.Callback waitForSubscriptions = (EventContext ctx) =>
Action<SubscriptionEventContext> waitForSubscriptions = (SubscriptionEventContext ctx) =>
{
// Note: callbacks are always invoked on the main thread, so you don't need to
// worry about thread synchronization or anything like that.
Expand Down Expand Up @@ -176,7 +170,7 @@ void PrintMessagesInOrder(RemoteTables tables)
}
}

void OnSubscriptionApplied(EventContext ctx)
void OnSubscriptionApplied(SubscriptionEventContext ctx)
{
Console.WriteLine("Connected");
PrintMessagesInOrder(ctx.Db);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ namespace SpacetimeDB.Types
{
public sealed partial class RemoteReducers : RemoteBase
{
public delegate void IdentityConnectedHandler(EventContext ctx);
public delegate void IdentityConnectedHandler(ReducerEventContext ctx);
public event IdentityConnectedHandler? OnIdentityConnected;

public bool InvokeIdentityConnected(EventContext ctx, Reducer.IdentityConnected args)
public bool InvokeIdentityConnected(ReducerEventContext ctx, Reducer.IdentityConnected args)
{
if (OnIdentityConnected == null) return false;
OnIdentityConnected(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ namespace SpacetimeDB.Types
{
public sealed partial class RemoteReducers : RemoteBase
{
public delegate void IdentityDisconnectedHandler(EventContext ctx);
public delegate void IdentityDisconnectedHandler(ReducerEventContext ctx);
public event IdentityDisconnectedHandler? OnIdentityDisconnected;

public bool InvokeIdentityDisconnected(EventContext ctx, Reducer.IdentityDisconnected args)
public bool InvokeIdentityDisconnected(ReducerEventContext ctx, Reducer.IdentityDisconnected args)
{
if (OnIdentityDisconnected == null) return false;
OnIdentityDisconnected(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ namespace SpacetimeDB.Types
{
public sealed partial class RemoteReducers : RemoteBase
{
public delegate void SendMessageHandler(EventContext ctx, string text);
public delegate void SendMessageHandler(ReducerEventContext ctx, string text);
public event SendMessageHandler? OnSendMessage;

public void SendMessage(string text)
{
conn.InternalCallReducer(new Reducer.SendMessage(text), this.SetCallReducerFlags.SendMessageFlags);
}

public bool InvokeSendMessage(EventContext ctx, Reducer.SendMessage args)
public bool InvokeSendMessage(ReducerEventContext ctx, Reducer.SendMessage args)
{
if (OnSendMessage == null) return false;
OnSendMessage(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ namespace SpacetimeDB.Types
{
public sealed partial class RemoteReducers : RemoteBase
{
public delegate void SetNameHandler(EventContext ctx, string name);
public delegate void SetNameHandler(ReducerEventContext ctx, string name);
public event SetNameHandler? OnSetName;

public void SetName(string name)
{
conn.InternalCallReducer(new Reducer.SetName(name), this.SetCallReducerFlags.SetNameFlags);
}

public bool InvokeSetName(EventContext ctx, Reducer.SetName args)
public bool InvokeSetName(ReducerEventContext ctx, Reducer.SetName args)
{
if (OnSetName == null) return false;
OnSetName(
Expand Down
76 changes: 70 additions & 6 deletions examples~/quickstart/client/module_bindings/SpacetimeDBClient.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public RemoteTables(DbConnection conn)
}

public sealed partial class SetReducerFlags { }

public sealed class EventContext : IEventContext
{
private readonly DbConnection conn;
Expand All @@ -35,13 +36,67 @@ public sealed class EventContext : IEventContext
public RemoteReducers Reducers => conn.Reducers;
public SetReducerFlags SetReducerFlags => conn.SetReducerFlags;

internal EventContext(DbConnection conn, Event<Reducer> reducerEvent)
internal EventContext(DbConnection conn, Event<Reducer> Event)
{
this.conn = conn;
this.Event = Event;
}
}

public sealed class ReducerEventContext : IReducerEventContext
{
private readonly DbConnection conn;
public readonly ReducerEvent<Reducer> Event;

public RemoteTables Db => conn.Db;
public RemoteReducers Reducers => conn.Reducers;
public SetReducerFlags SetReducerFlags => conn.SetReducerFlags;

internal ReducerEventContext(DbConnection conn, ReducerEvent<Reducer> reducerEvent)
{
this.conn = conn;
Event = reducerEvent;
}
}

public sealed class ErrorContext : IErrorContext
{
private readonly DbConnection conn;
public readonly Exception Event;
Exception IErrorContext.Event
{
get
{
return Event;
}
}

public RemoteTables Db => conn.Db;
public RemoteReducers Reducers => conn.Reducers;
public SetReducerFlags SetReducerFlags => conn.SetReducerFlags;
public Exception Error => Event;

internal ErrorContext(DbConnection conn, Exception error)
{
this.conn = conn;
Event = error;
}
}

public sealed class SubscriptionEventContext : ISubscriptionEventContext
{
private readonly DbConnection conn;

public RemoteTables Db => conn.Db;
public RemoteReducers Reducers => conn.Reducers;
public SetReducerFlags SetReducerFlags => conn.SetReducerFlags;

internal SubscriptionEventContext(DbConnection conn)
{
this.conn = conn;
}
}

public abstract partial class Reducer
{
private Reducer() { }
Expand Down Expand Up @@ -75,12 +130,21 @@ protected override Reducer ToReducer(TransactionUpdate update)
};
}

protected override IEventContext ToEventContext(Event<Reducer> reducerEvent) =>
new EventContext(this, reducerEvent);
protected override IEventContext ToEventContext(Event<Reducer> Event) =>
new EventContext(this, Event);

protected override IReducerEventContext ToReducerEventContext(ReducerEvent<Reducer> reducerEvent) =>
new ReducerEventContext(this, reducerEvent);

protected override ISubscriptionEventContext MakeSubscriptionEventContext() =>
new SubscriptionEventContext(this);

protected override IErrorContext ToErrorContext(Exception exception) =>
new ErrorContext(this, exception);

protected override bool Dispatch(IEventContext context, Reducer reducer)
protected override bool Dispatch(IReducerEventContext context, Reducer reducer)
{
var eventContext = (EventContext)context;
var eventContext = (ReducerEventContext)context;
return reducer switch
{
Reducer.IdentityConnected args => Reducers.InvokeIdentityConnected(eventContext, args),
Expand All @@ -92,6 +156,6 @@ protected override bool Dispatch(IEventContext context, Reducer reducer)
};
}

public SubscriptionBuilder<EventContext> SubscriptionBuilder() => new(this);
public SubscriptionBuilder<SubscriptionEventContext, ErrorContext> SubscriptionBuilder() => new(this);
}
}
Loading