Skip to content

Commit 9382710

Browse files
SteveGibsonCLStevejdetter
authored
Reducer event (#28)
* Deserializing reducer args before table updates so they can be accessed from callbacks * Reducer arguments in table events * Adding OnBeforeDelete event to tables * Renaming ReducerCallInfo to ReducerEvent * Removed extra onSubscriptionUpdate --------- Co-authored-by: Steve <[email protected]> Co-authored-by: John Detter <[email protected]>
1 parent cf9c5b0 commit 9382710

File tree

5 files changed

+44
-8
lines changed

5 files changed

+44
-8
lines changed

sdks/csharp/Assets/SpacetimeDB/Scripts/ClientApiExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
{
33
public partial class FunctionCall
44
{
5-
public SpacetimeDB.ReducerCallInfo CallInfo { get; internal set; }
5+
public SpacetimeDB.ReducerEvent CallInfo { get; internal set; }
66
}
77
}

sdks/csharp/Assets/SpacetimeDB/Scripts/ClientCache.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public Type ClientTableType
5555
}
5656

5757
public Action<object, ClientApi.Event> InsertCallback;
58+
public Action<object, ClientApi.Event> BeforeDeleteCallback;
5859
public Action<object, ClientApi.Event> DeleteCallback;
5960
public Action<object, object, ClientApi.Event> UpdateCallback;
6061
// TODO: Consider renaming this one, this kind of implies that its a callback for the Update operation
@@ -79,6 +80,7 @@ public TableCache(Type clientTableType, AlgebraicType rowSchema, Func<AlgebraicV
7980
this.rowSchema = rowSchema;
8081
this.decoderFunc = decoderFunc;
8182
InsertCallback = (Action<object, ClientApi.Event>)clientTableType.GetMethod("OnInsertEvent")?.CreateDelegate(typeof(Action<object, ClientApi.Event>));
83+
BeforeDeleteCallback = (Action<object, ClientApi.Event>)clientTableType.GetMethod("OnBeforeDeleteEvent")?.CreateDelegate(typeof(Action<object, ClientApi.Event>));
8284
DeleteCallback = (Action<object, ClientApi.Event>)clientTableType.GetMethod("OnDeleteEvent")?.CreateDelegate(typeof(Action<object, ClientApi.Event>));
8385
UpdateCallback = (Action<object, object, ClientApi.Event>)clientTableType.GetMethod("OnUpdateEvent")?.CreateDelegate(typeof(Action<object, object, ClientApi.Event>));
8486
RowUpdatedCallback = (Action<NetworkManager.TableOp, object, object, ClientApi.Event>)clientTableType.GetMethod("OnRowUpdateEvent")
@@ -185,6 +187,23 @@ public object DeleteEntry(byte[] rowPk)
185187
return null;
186188
}
187189

190+
/// <summary>
191+
/// Gets a value from the table
192+
/// </summary>
193+
/// <param name="rowPk">The primary key that uniquely identifies this row</param>
194+
/// <returns></returns>
195+
public bool TryGetValue(byte[] rowPk, out object value)
196+
{
197+
if (entries.TryGetValue(rowPk, out var v))
198+
{
199+
value = v.Item2;
200+
return true;
201+
}
202+
203+
value = null;
204+
return false;
205+
}
206+
188207
public bool ComparePrimaryKey(AlgebraicValue v1, AlgebraicValue v2)
189208
{
190209
return (bool)ComparePrimaryKeyFunc.Invoke(rowSchema, v1, v2);

sdks/csharp/Assets/SpacetimeDB/Scripts/NetworkManager.cs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,13 @@ protected void Awake()
157157
// cache all our reducer events by their function name
158158
foreach (var methodInfo in typeof(SpacetimeDB.Reducer).GetMethods())
159159
{
160-
if (methodInfo.GetCustomAttribute<ReducerEvent>() is
160+
if (methodInfo.GetCustomAttribute<ReducerCallbackAttribute>() is
161161
{ } reducerEvent)
162162
{
163163
reducerEventCache.Add(reducerEvent.FunctionName, (Action<ClientApi.Event>)methodInfo.CreateDelegate(typeof(Action<ClientApi.Event>)));
164164
}
165165

166-
if (methodInfo.GetCustomAttribute<DeserializeEvent>() is
166+
if (methodInfo.GetCustomAttribute<DeserializeEventAttribute>() is
167167
{ } deserializeEvent)
168168
{
169169
deserializeEventCache.Add(deserializeEvent.FunctionName, (Action<ClientApi.Event>)methodInfo.CreateDelegate(typeof(Action<ClientApi.Event>)));
@@ -420,7 +420,25 @@ private void OnMessageProcessComplete(Message message, IList<DbEvent> events)
420420
{
421421
case Message.TypeOneofCase.SubscriptionUpdate:
422422
case Message.TypeOneofCase.TransactionUpdate:
423-
// First apply all of the state
423+
// First trigger OnBeforeDelete
424+
for (var i = 0; i < events.Count; i++)
425+
{
426+
// TODO: Reimplement updates when we add support for primary keys
427+
var ev = events[i];
428+
if (ev.op == TableOp.Delete && ev.table.TryGetValue(ev.deletedPk, out var oldVal))
429+
{
430+
try
431+
{
432+
ev.table.BeforeDeleteCallback?.Invoke(oldVal, message.Event);
433+
}
434+
catch (Exception e)
435+
{
436+
Debug.LogException(e);
437+
}
438+
}
439+
}
440+
441+
// Apply all of the state
424442
for (var i = 0; i < events.Count; i++)
425443
{
426444
// TODO: Reimplement updates when we add support for primary keys

sdks/csharp/Assets/SpacetimeDB/Scripts/ReducerExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
namespace SpacetimeDB
44
{
5-
public class ReducerEvent : Attribute
5+
public class ReducerCallbackAttribute : Attribute
66
{
77
public string FunctionName { get; set; }
88
}
99

10-
public class DeserializeEvent : Attribute
10+
public class DeserializeEventAttribute : Attribute
1111
{
1212
public string FunctionName { get; set; }
1313
}

sdks/csharp/Assets/SpacetimeDB/Scripts/Stubs.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ public partial class Reducer
88
{
99
}
1010

11-
public partial class ReducerCallInfo
11+
public partial class ReducerEvent
1212
{
13-
public SpacetimeDB.NetworkManager.DbEvent[] RowChanges { get; internal set; }
1413
}
1514
}

0 commit comments

Comments
 (0)