Skip to content

Commit

Permalink
implement websocket ids-no-names (take 2) in c#
Browse files Browse the repository at this point in the history
  • Loading branch information
Centril committed Nov 19, 2024
1 parent e5606f1 commit bf83334
Show file tree
Hide file tree
Showing 13 changed files with 296 additions and 77 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE
// WILL NOT BE SAVED. MODIFY TABLES IN RUST INSTEAD.
// <auto-generated />

#nullable enable

using System;
using SpacetimeDB;

namespace SpacetimeDB.Types
{
[SpacetimeDB.Type]
public partial class IdentityConnected : IReducerArgs
{
string IReducerArgs.ReducerName => "__identity_connected__";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE
// WILL NOT BE SAVED. MODIFY TABLES IN RUST INSTEAD.
// <auto-generated />

#nullable enable

using System;
using SpacetimeDB;

namespace SpacetimeDB.Types
{
[SpacetimeDB.Type]
public partial class IdentityDisconnected : IReducerArgs
{
string IReducerArgs.ReducerName => "__identity_disconnected__";
}
}
17 changes: 17 additions & 0 deletions examples~/quickstart/client/module_bindings/InitReducer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE
// WILL NOT BE SAVED. MODIFY TABLES IN RUST INSTEAD.
// <auto-generated />

#nullable enable

using System;
using SpacetimeDB;

namespace SpacetimeDB.Types
{
[SpacetimeDB.Type]
public partial class Init : IReducerArgs
{
string IReducerArgs.ReducerName => "__init__";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,30 @@ internal MessageHandle()

public class UserHandle : RemoteTableHandle<EventContext, User>
{
private static Dictionary<SpacetimeDB.Identity, User> Identity_Index = new(16);

public override void InternalInvokeValueInserted(IDatabaseRow row)
{
var value = (User)row;
Identity_Index[value.Identity] = value;
Identity.Cache[value.Identity] = value;
}

public override void InternalInvokeValueDeleted(IDatabaseRow row)
{
Identity_Index.Remove(((User)row).Identity);
Identity.Cache.Remove(((User)row).Identity);
}

public readonly ref struct IdentityUniqueIndex
public class IdentityUniqueIndex
{
internal readonly Dictionary<SpacetimeDB.Identity, User> Cache = new(16);
public User? Find(SpacetimeDB.Identity value)
{
Identity_Index.TryGetValue(value, out var r);
Cache.TryGetValue(value, out var r);
return r;
}

}

public IdentityUniqueIndex Identity => new();
public IdentityUniqueIndex Identity = new();

internal UserHandle()
{
Expand All @@ -65,6 +65,54 @@ public sealed class RemoteReducers : RemoteBase<DbConnection>
{
internal RemoteReducers(DbConnection conn, SetReducerFlags SetReducerFlags) : base(conn) { this.SetCallReducerFlags = SetReducerFlags; }
internal readonly SetReducerFlags SetCallReducerFlags;
public delegate void IdentityConnectedHandler(EventContext ctx);
public event IdentityConnectedHandler? OnIdentityConnected;

public void IdentityConnected()
{
conn.InternalCallReducer(new IdentityConnected { }, this.SetCallReducerFlags.IdentityConnectedFlags);
}

public bool InvokeIdentityConnected(EventContext ctx, IdentityConnected args)
{
if (OnIdentityConnected == null) return false;
OnIdentityConnected(
ctx
);
return true;
}
public delegate void IdentityDisconnectedHandler(EventContext ctx);
public event IdentityDisconnectedHandler? OnIdentityDisconnected;

public void IdentityDisconnected()
{
conn.InternalCallReducer(new IdentityDisconnected { }, this.SetCallReducerFlags.IdentityDisconnectedFlags);
}

public bool InvokeIdentityDisconnected(EventContext ctx, IdentityDisconnected args)
{
if (OnIdentityDisconnected == null) return false;
OnIdentityDisconnected(
ctx
);
return true;
}
public delegate void InitHandler(EventContext ctx);
public event InitHandler? OnInit;

public void Init()
{
conn.InternalCallReducer(new Init { }, this.SetCallReducerFlags.InitFlags);
}

public bool InvokeInit(EventContext ctx, Init args)
{
if (OnInit == null) return false;
OnInit(
ctx
);
return true;
}
public delegate void SendMessageHandler(EventContext ctx, string text);
public event SendMessageHandler? OnSendMessage;

Expand Down Expand Up @@ -104,6 +152,12 @@ public bool InvokeSetName(EventContext ctx, SetName args)
public sealed class SetReducerFlags
{
internal SetReducerFlags() { }
internal CallReducerFlags IdentityConnectedFlags;
public void IdentityConnected(CallReducerFlags flags) { this.IdentityConnectedFlags = flags; }
internal CallReducerFlags IdentityDisconnectedFlags;
public void IdentityDisconnected(CallReducerFlags flags) { this.IdentityDisconnectedFlags = flags; }
internal CallReducerFlags InitFlags;
public void Init(CallReducerFlags flags) { this.InitFlags = flags; }
internal CallReducerFlags SendMessageFlags;
public void SendMessage(CallReducerFlags flags) { this.SendMessageFlags = flags; }
internal CallReducerFlags SetNameFlags;
Expand All @@ -126,11 +180,12 @@ internal EventContext(DbConnection conn, Event<Reducer> reducerEvent) : base(con

[Type]
public partial record Reducer : TaggedEnum<(
IdentityConnected IdentityConnected,
IdentityDisconnected IdentityDisconnected,
Init Init,
SendMessage SendMessage,
SetName SetName,
Unit StdbNone,
Unit StdbIdentityConnected,
Unit StdbIdentityDisconnected
Unit StdbNone
)>;
public class DbConnection : DbConnectionBase<DbConnection, Reducer>
{
Expand All @@ -147,17 +202,16 @@ public DbConnection()
clientDB.AddTable<User>("user", Db.User);
}

protected override Reducer ToReducer(TransactionUpdate update)
protected override Reducer ToReducer(string reducerName, TransactionUpdate update)
{
var encodedArgs = update.ReducerCall.Args;
return update.ReducerCall.ReducerName switch
{
return reducerName switch {
"__identity_connected__" => new Reducer.IdentityConnected(BSATNHelpers.Decode<IdentityConnected>(encodedArgs)),
"__identity_disconnected__" => new Reducer.IdentityDisconnected(BSATNHelpers.Decode<IdentityDisconnected>(encodedArgs)),
"__init__" => new Reducer.Init(BSATNHelpers.Decode<Init>(encodedArgs)),
"send_message" => new Reducer.SendMessage(BSATNHelpers.Decode<SendMessage>(encodedArgs)),
"set_name" => new Reducer.SetName(BSATNHelpers.Decode<SetName>(encodedArgs)),
"<none>" => new Reducer.StdbNone(default),
"__identity_connected__" => new Reducer.StdbIdentityConnected(default),
"__identity_disconnected__" => new Reducer.StdbIdentityDisconnected(default),
"" => new Reducer.StdbNone(default),
var reducer => throw new ArgumentOutOfRangeException("Reducer", $"Unknown reducer {reducer}")
};
}
Expand All @@ -168,13 +222,13 @@ protected override IEventContext ToEventContext(Event<Reducer> reducerEvent) =>
protected override bool Dispatch(IEventContext context, Reducer reducer)
{
var eventContext = (EventContext)context;
return reducer switch
{
return reducer switch {
Reducer.IdentityConnected(var args) => Reducers.InvokeIdentityConnected(eventContext, args),
Reducer.IdentityDisconnected(var args) => Reducers.InvokeIdentityDisconnected(eventContext, args),
Reducer.Init(var args) => Reducers.InvokeInit(eventContext, args),
Reducer.SendMessage(var args) => Reducers.InvokeSendMessage(eventContext, args),
Reducer.SetName(var args) => Reducers.InvokeSetName(eventContext, args),
Reducer.StdbNone or
Reducer.StdbIdentityConnected or
Reducer.StdbIdentityDisconnected => true,
Reducer.StdbNone => true,
_ => throw new ArgumentOutOfRangeException("Reducer", $"Unknown reducer {reducer}")
};
}
Expand Down
35 changes: 35 additions & 0 deletions src/SpacetimeDB/ClientApi/AfterConnecting.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#nullable enable

using System;
using SpacetimeDB;
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace SpacetimeDB.ClientApi
{
[SpacetimeDB.Type]
[DataContract]
public partial class AfterConnecting
{
[DataMember(Name = "identity_token")]
public SpacetimeDB.ClientApi.IdentityToken IdentityToken;
[DataMember(Name = "ids_to_names")]
public SpacetimeDB.ClientApi.IdsToNames IdsToNames;

public AfterConnecting(
SpacetimeDB.ClientApi.IdentityToken IdentityToken,
SpacetimeDB.ClientApi.IdsToNames IdsToNames
)
{
this.IdentityToken = IdentityToken;
this.IdsToNames = IdsToNames;
}

public AfterConnecting()
{
this.IdentityToken = new();
this.IdsToNames = new();
}

}
}
9 changes: 4 additions & 5 deletions src/SpacetimeDB/ClientApi/CallReducer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ namespace SpacetimeDB.ClientApi
[DataContract]
public partial class CallReducer
{
[DataMember(Name = "reducer")]
public string Reducer;
[DataMember(Name = "reducer_id")]
public uint ReducerId;
[DataMember(Name = "args")]
public byte[] Args;
[DataMember(Name = "request_id")]
Expand All @@ -25,21 +25,20 @@ public partial class CallReducer
public byte Flags;

public CallReducer(
string Reducer,
uint ReducerId,
byte[] Args,
uint RequestId,
byte Flags
)
{
this.Reducer = Reducer;
this.ReducerId = ReducerId;
this.Args = Args;
this.RequestId = RequestId;
this.Flags = Flags;
}

public CallReducer()
{
this.Reducer = "";
this.Args = Array.Empty<byte>();
}

Expand Down
45 changes: 45 additions & 0 deletions src/SpacetimeDB/ClientApi/IdsToNames.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#nullable enable

using System;
using SpacetimeDB;
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace SpacetimeDB.ClientApi
{
[SpacetimeDB.Type]
[DataContract]
public partial class IdsToNames
{
[DataMember(Name = "reducer_ids")]
public System.Collections.Generic.List<uint> ReducerIds;
[DataMember(Name = "reducer_names")]
public System.Collections.Generic.List<string> ReducerNames;
[DataMember(Name = "table_ids")]
public System.Collections.Generic.List<uint> TableIds;
[DataMember(Name = "table_names")]
public System.Collections.Generic.List<string> TableNames;

public IdsToNames(
System.Collections.Generic.List<uint> ReducerIds,
System.Collections.Generic.List<string> ReducerNames,
System.Collections.Generic.List<uint> TableIds,
System.Collections.Generic.List<string> TableNames
)
{
this.ReducerIds = ReducerIds;
this.ReducerNames = ReducerNames;
this.TableIds = TableIds;
this.TableNames = TableNames;
}

public IdsToNames()
{
ReducerIds = new();
ReducerNames = new();
TableIds = new();
TableNames = new();
}

}
}
9 changes: 4 additions & 5 deletions src/SpacetimeDB/ClientApi/OneOffTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,22 @@ namespace SpacetimeDB.ClientApi
[DataContract]
public partial class OneOffTable
{
[DataMember(Name = "table_name")]
public string TableName;
[DataMember(Name = "table_id")]
public uint TableId;
[DataMember(Name = "rows")]
public SpacetimeDB.ClientApi.BsatnRowList Rows;

public OneOffTable(
string TableName,
uint TableId,
SpacetimeDB.ClientApi.BsatnRowList Rows
)
{
this.TableName = TableName;
this.TableId = TableId;
this.Rows = Rows;
}

public OneOffTable()
{
this.TableName = "";
this.Rows = new();
}

Expand Down
5 changes: 0 additions & 5 deletions src/SpacetimeDB/ClientApi/ReducerCallInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ namespace SpacetimeDB.ClientApi
[DataContract]
public partial class ReducerCallInfo
{
[DataMember(Name = "reducer_name")]
public string ReducerName;
[DataMember(Name = "reducer_id")]
public uint ReducerId;
[DataMember(Name = "args")]
Expand All @@ -25,21 +23,18 @@ public partial class ReducerCallInfo
public uint RequestId;

public ReducerCallInfo(
string ReducerName,
uint ReducerId,
byte[] Args,
uint RequestId
)
{
this.ReducerName = ReducerName;
this.ReducerId = ReducerId;
this.Args = Args;
this.RequestId = RequestId;
}

public ReducerCallInfo()
{
this.ReducerName = "";
this.Args = Array.Empty<byte>();
}

Expand Down
Loading

0 comments on commit bf83334

Please sign in to comment.