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

implement websocket ids-no-names (take 2) in c# #191

Open
wants to merge 1 commit into
base: staging
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions examples~/quickstart/client/client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<CheckEolTargetFramework>false</CheckEolTargetFramework>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>enable</Nullable>

Expand Down
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
Loading
Loading