From 7064c200d59804ed877ac4637c60c27ae3f04c0c Mon Sep 17 00:00:00 2001 From: Ingvar Stepanyan Date: Mon, 13 May 2024 11:45:56 -0400 Subject: [PATCH] Fix conflict resolution shenanigans (#87) --- .../client/module_bindings/Message.cs | 7 - .../quickstart/client/module_bindings/User.cs | 7 - src/SpacetimeDBClient.cs | 123 +++++++++--------- 3 files changed, 62 insertions(+), 75 deletions(-) diff --git a/examples/quickstart/client/module_bindings/Message.cs b/examples/quickstart/client/module_bindings/Message.cs index 25800e8f..438777e8 100644 --- a/examples/quickstart/client/module_bindings/Message.cs +++ b/examples/quickstart/client/module_bindings/Message.cs @@ -109,11 +109,9 @@ public static bool ComparePrimaryKey(SpacetimeDB.SATS.AlgebraicType t, Spacetime public delegate void InsertEventHandler(Message insertedValue, SpacetimeDB.Types.ReducerEvent dbEvent); public delegate void DeleteEventHandler(Message deletedValue, SpacetimeDB.Types.ReducerEvent dbEvent); - public delegate void RowUpdateEventHandler(SpacetimeDBClient.TableOp op, Message oldValue, Message newValue, SpacetimeDB.Types.ReducerEvent dbEvent); public static event InsertEventHandler OnInsert; public static event DeleteEventHandler OnBeforeDelete; public static event DeleteEventHandler OnDelete; - public static event RowUpdateEventHandler OnRowUpdate; public static void OnInsertEvent(object newValue, ClientApi.Event dbEvent) { @@ -129,10 +127,5 @@ public static void OnDeleteEvent(object oldValue, ClientApi.Event dbEvent) { OnDelete?.Invoke((Message)oldValue,(ReducerEvent)dbEvent?.FunctionCall.CallInfo); } - - public static void OnRowUpdateEvent(SpacetimeDBClient.TableOp op, object oldValue, object newValue, ClientApi.Event dbEvent) - { - OnRowUpdate?.Invoke(op, (Message)oldValue,(Message)newValue,(ReducerEvent)dbEvent?.FunctionCall.CallInfo); - } } } diff --git a/examples/quickstart/client/module_bindings/User.cs b/examples/quickstart/client/module_bindings/User.cs index 85c7ca12..04230fc3 100644 --- a/examples/quickstart/client/module_bindings/User.cs +++ b/examples/quickstart/client/module_bindings/User.cs @@ -126,12 +126,10 @@ public static SpacetimeDB.SATS.AlgebraicType GetPrimaryKeyType(SpacetimeDB.SATS. public delegate void InsertEventHandler(User insertedValue, SpacetimeDB.Types.ReducerEvent dbEvent); public delegate void UpdateEventHandler(User oldValue, User newValue, SpacetimeDB.Types.ReducerEvent dbEvent); public delegate void DeleteEventHandler(User deletedValue, SpacetimeDB.Types.ReducerEvent dbEvent); - public delegate void RowUpdateEventHandler(SpacetimeDBClient.TableOp op, User oldValue, User newValue, SpacetimeDB.Types.ReducerEvent dbEvent); public static event InsertEventHandler OnInsert; public static event UpdateEventHandler OnUpdate; public static event DeleteEventHandler OnBeforeDelete; public static event DeleteEventHandler OnDelete; - public static event RowUpdateEventHandler OnRowUpdate; public static void OnInsertEvent(object newValue, ClientApi.Event dbEvent) { @@ -152,10 +150,5 @@ public static void OnDeleteEvent(object oldValue, ClientApi.Event dbEvent) { OnDelete?.Invoke((User)oldValue,(ReducerEvent)dbEvent?.FunctionCall.CallInfo); } - - public static void OnRowUpdateEvent(SpacetimeDBClient.TableOp op, object oldValue, object newValue, ClientApi.Event dbEvent) - { - OnRowUpdate?.Invoke(op, (User)oldValue,(User)newValue,(ReducerEvent)dbEvent?.FunctionCall.CallInfo); - } } } diff --git a/src/SpacetimeDBClient.cs b/src/SpacetimeDBClient.cs index 9c1263da..bddc4e6c 100644 --- a/src/SpacetimeDBClient.cs +++ b/src/SpacetimeDBClient.cs @@ -704,85 +704,86 @@ void InternalInsertCallback(DbOp op) var oldValue = dbOps[i].oldValue; var newValue = dbOps[i].newValue; - switch (tableOp) + switch (tableOp) + { + case TableOp.Insert: + if (oldValue == null && newValue != null) { - case TableOp.Insert: - if (oldValue == null && newValue != null) + try + { + if (dbOps[i].table.InsertCallback != null) + { + dbOps[i].table.InsertCallback.Invoke(newValue, + message.TransactionUpdate?.Event); + } + } + catch (Exception e) + { + Logger.LogException(e); + } + } + else + { + Logger.LogError("Failed to send callback: invalid insert!"); + } + + break; + case TableOp.Delete: + { + if (oldValue != null && newValue == null) + { + if (dbOps[i].table.DeleteCallback != null) { try { - if (dbOps[i].table.InsertCallback != null) - { - dbOps[i].table.InsertCallback.Invoke(newValue, - message.TransactionUpdate?.Event); - } + dbOps[i].table.DeleteCallback.Invoke(oldValue, + message.TransactionUpdate?.Event); } catch (Exception e) { Logger.LogException(e); } } - else - { - Logger.LogError("Failed to send callback: invalid insert!"); - } + } + else + { + Logger.LogError("Failed to send callback: invalid delete"); + } - break; - case TableOp.Delete: + break; + } + case TableOp.Update: + { + if (oldValue != null && newValue != null) + { + try { - if (oldValue != null && newValue == null) - { - if (dbOps[i].table.DeleteCallback != null) - { - try - { - dbOps[i].table.DeleteCallback.Invoke(oldValue, - message.TransactionUpdate?.Event); - } - catch (Exception e) - { - Logger.LogException(e); - } - } - } - else + if (dbOps[i].table.UpdateCallback != null) { - Logger.LogError("Failed to send callback: invalid delete"); + dbOps[i].table.UpdateCallback.Invoke(oldValue, newValue, + message.TransactionUpdate?.Event); } - - break; } - case TableOp.Update: + catch (Exception e) { - if (oldValue != null && newValue != null) - { - try - { - if (dbOps[i].table.UpdateCallback != null) - { - dbOps[i].table.UpdateCallback.Invoke(oldValue, newValue, - message.TransactionUpdate?.Event); - } - } - catch (Exception e) - { - Logger.LogException(e); - } - } - else - { - Logger.LogError("Failed to send callback: invalid update"); - } - - break; + Logger.LogException(e); } - case TableOp.NoChange: - // noop - break; - default: - throw new ArgumentOutOfRangeException(); + } + else + { + Logger.LogError("Failed to send callback: invalid update"); + } + + break; } - } + case TableOp.NoChange: + // noop + break; + default: + throw new ArgumentOutOfRangeException(); + } + } + } private void OnMessageProcessComplete(Message message, List dbOps) {