feat(SyncDictionary): Added OnChange Action

This commit is contained in:
MrGadget 2024-04-14 17:00:33 -04:00
parent 5d167b9998
commit 52e6ca36f0
2 changed files with 42 additions and 0 deletions

View File

@ -15,6 +15,14 @@ public class SyncIDictionary<TKey, TValue> : SyncObject, IDictionary<TKey, TValu
/// <summary>This is called after the item is removed with TKey. TValue is the OLD item</summary>
public Action<TKey, TValue> OnRemove;
/// <summary>
/// This is called for all changes to the Dictionary.
/// <para>For OP_ADD, TValue is the NEW value of the entry.</para>
/// <para>For OP_SET and OP_REMOVE, TValue is the OLD value of the entry.</para>
/// <para>For OP_CLEAR, both TKey and TValue are default.</para>
/// </summary>
public Action<Operation, TKey, TValue> OnChange;
/// <summary>This is called before the data is cleared</summary>
public Action OnClear;
@ -320,15 +328,19 @@ void AddOperation(Operation op, TKey key, TValue item, TValue oldItem, bool chec
{
case Operation.OP_ADD:
OnAdd?.Invoke(key);
OnChange?.Invoke(op, key, item);
break;
case Operation.OP_SET:
OnSet?.Invoke(key, oldItem);
OnChange?.Invoke(op, key, oldItem);
break;
case Operation.OP_REMOVE:
OnRemove?.Invoke(key, oldItem);
OnChange?.Invoke(op, key, oldItem);
break;
case Operation.OP_CLEAR:
OnClear?.Invoke();
OnChange?.Invoke(op, default, default);
break;
}

View File

@ -290,10 +290,20 @@ public void CallbackTest()
Assert.That(clientSyncDictionary[key], Is.EqualTo("yay"));
};
bool changeActionCalled = false;
clientSyncDictionary.OnChange = (op, key, item) =>
{
changeActionCalled = true;
Assert.That(op, Is.EqualTo(SyncDictionary<int, string>.Operation.OP_ADD));
Assert.That(key, Is.EqualTo(3));
Assert.That(clientSyncDictionary[key], Is.EqualTo("yay"));
};
serverSyncDictionary.Add(3, "yay");
SerializeDeltaTo(serverSyncDictionary, clientSyncDictionary);
Assert.That(called, Is.True);
Assert.That(actionCalled, Is.True);
Assert.That(changeActionCalled, Is.True);
}
[Test]
@ -320,9 +330,19 @@ public void ServerCallbackTest()
Assert.That(serverSyncDictionary[key], Is.EqualTo("yay"));
};
bool changeActionCalled = false;
serverSyncDictionary.OnChange = (op, key, item) =>
{
changeActionCalled = true;
Assert.That(op, Is.EqualTo(SyncDictionary<int, string>.Operation.OP_ADD));
Assert.That(key, Is.EqualTo(3));
Assert.That(serverSyncDictionary[key], Is.EqualTo("yay"));
};
serverSyncDictionary[3] = "yay";
Assert.That(called, Is.True);
Assert.That(actionCalled, Is.True);
Assert.That(changeActionCalled, Is.True);
}
[Test]
@ -348,10 +368,20 @@ public void CallbackRemoveTest()
Assert.That(!clientSyncDictionary.ContainsKey(1));
};
bool changeActionCalled = false;
clientSyncDictionary.OnChange = (op, key, item) =>
{
changeActionCalled = true;
Assert.That(op, Is.EqualTo(SyncDictionary<int, string>.Operation.OP_REMOVE));
Assert.That(key, Is.EqualTo(1));
Assert.That(item, Is.EqualTo("World"));
};
serverSyncDictionary.Remove(1);
SerializeDeltaTo(serverSyncDictionary, clientSyncDictionary);
Assert.That(called, Is.True);
Assert.That(actionCalled, Is.True);
Assert.That(changeActionCalled, Is.True);
}
[Test]