Skip to content

Commit

Permalink
Jeremie/cs module (#1719)
Browse files Browse the repository at this point in the history
  • Loading branch information
lcodes authored and Centril committed Sep 29, 2024
1 parent 72f4257 commit d352e45
Show file tree
Hide file tree
Showing 26 changed files with 1,406 additions and 895 deletions.
8 changes: 8 additions & 0 deletions crates/bindings-csharp/BSATN.Runtime/Db.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace SpacetimeDB;

public abstract record DbContext<DbView>(DbView Db)
where DbView : class, new()
{
public DbContext()
: this(new DbView()) { }
}
35 changes: 19 additions & 16 deletions crates/bindings-csharp/Codegen.Tests/fixtures/server/Lib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ public partial struct CustomStruct
}

[SpacetimeDB.Type]
public partial struct CustomClass
public partial class CustomClass
{
public const int IGNORE_ME = 0;
public static readonly string IGNORE_ME_TOO = "";
public int IntField;
public string StringField;
public int IntField = 0;
public string StringField = "";
}

[StructLayout(LayoutKind.Auto)]
public partial struct CustomClass
public partial class CustomClass
{
public int IgnoreExtraFields;
}
Expand All @@ -44,7 +44,8 @@ public partial class PrivateTable { }
[SpacetimeDB.Table]
public partial struct PublicTable
{
[SpacetimeDB.Column(ColumnAttrs.PrimaryKeyAuto)]
[SpacetimeDB.AutoInc]
[SpacetimeDB.PrimaryKey]
public int Id;

public byte ByteField;
Expand Down Expand Up @@ -81,18 +82,18 @@ public partial struct PublicTable
public static partial class Reducers
{
[SpacetimeDB.Reducer]
public static void InsertData(PublicTable data)
public static void InsertData(ReducerContext ctx, PublicTable data)
{
data.Insert();
ctx.Db.PublicTable.Insert(data);
Log.Info("New list");
foreach (var item in PublicTable.Iter())
foreach (var item in ctx.Db.PublicTable.Iter())
{
Log.Info($"Item: {item.StringField}");
}
}

[SpacetimeDB.Reducer]
public static void ScheduleImmediate(PublicTable data)
public static void ScheduleImmediate(ReducerContext ctx, PublicTable data)
{
VolatileNonatomicScheduleImmediateInsertData(data);
}
Expand All @@ -107,7 +108,7 @@ public static partial class AndClasses
[SpacetimeDB.Reducer("test_custom_name_and_reducer_ctx")]
public static void InsertData2(ReducerContext ctx, PublicTable data)
{
data.Insert();
ctx.Db.PublicTable.Insert(data);
}
}
}
Expand All @@ -122,7 +123,7 @@ public partial struct SendMessageTimer
}

[SpacetimeDB.Reducer]
public static void SendScheduledMessage(SendMessageTimer arg)
public static void SendScheduledMessage(ReducerContext ctx, SendMessageTimer arg)
{
// verify that fields were auto-added
ulong id = arg.ScheduledId;
Expand All @@ -133,10 +134,12 @@ public static void SendScheduledMessage(SendMessageTimer arg)
[SpacetimeDB.Reducer(ReducerKind.Init)]
public static void Init(ReducerContext ctx)
{
new SendMessageTimer
{
Text = "bot sending a message",
ScheduledAt = ctx.Time.AddSeconds(10),
}.Insert();
ctx.Db.SendMessageTimer.Insert(
new SendMessageTimer
{
Text = "bot sending a message",
ScheduledAt = ctx.Time.AddSeconds(10),
}
);
}
}
Loading

0 comments on commit d352e45

Please sign in to comment.