mirror of
https://github.com/MirrorNetworking/Mirror.git
synced 2024-11-18 02:50:32 +00:00
feat(SyncDictionary): Added OnChange Action
This commit is contained in:
parent
5d167b9998
commit
52e6ca36f0
@ -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>
|
/// <summary>This is called after the item is removed with TKey. TValue is the OLD item</summary>
|
||||||
public Action<TKey, TValue> OnRemove;
|
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>
|
/// <summary>This is called before the data is cleared</summary>
|
||||||
public Action OnClear;
|
public Action OnClear;
|
||||||
|
|
||||||
@ -320,15 +328,19 @@ void AddOperation(Operation op, TKey key, TValue item, TValue oldItem, bool chec
|
|||||||
{
|
{
|
||||||
case Operation.OP_ADD:
|
case Operation.OP_ADD:
|
||||||
OnAdd?.Invoke(key);
|
OnAdd?.Invoke(key);
|
||||||
|
OnChange?.Invoke(op, key, item);
|
||||||
break;
|
break;
|
||||||
case Operation.OP_SET:
|
case Operation.OP_SET:
|
||||||
OnSet?.Invoke(key, oldItem);
|
OnSet?.Invoke(key, oldItem);
|
||||||
|
OnChange?.Invoke(op, key, oldItem);
|
||||||
break;
|
break;
|
||||||
case Operation.OP_REMOVE:
|
case Operation.OP_REMOVE:
|
||||||
OnRemove?.Invoke(key, oldItem);
|
OnRemove?.Invoke(key, oldItem);
|
||||||
|
OnChange?.Invoke(op, key, oldItem);
|
||||||
break;
|
break;
|
||||||
case Operation.OP_CLEAR:
|
case Operation.OP_CLEAR:
|
||||||
OnClear?.Invoke();
|
OnClear?.Invoke();
|
||||||
|
OnChange?.Invoke(op, default, default);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -290,10 +290,20 @@ public void CallbackTest()
|
|||||||
Assert.That(clientSyncDictionary[key], Is.EqualTo("yay"));
|
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");
|
serverSyncDictionary.Add(3, "yay");
|
||||||
SerializeDeltaTo(serverSyncDictionary, clientSyncDictionary);
|
SerializeDeltaTo(serverSyncDictionary, clientSyncDictionary);
|
||||||
Assert.That(called, Is.True);
|
Assert.That(called, Is.True);
|
||||||
Assert.That(actionCalled, Is.True);
|
Assert.That(actionCalled, Is.True);
|
||||||
|
Assert.That(changeActionCalled, Is.True);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
@ -320,9 +330,19 @@ public void ServerCallbackTest()
|
|||||||
Assert.That(serverSyncDictionary[key], Is.EqualTo("yay"));
|
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";
|
serverSyncDictionary[3] = "yay";
|
||||||
Assert.That(called, Is.True);
|
Assert.That(called, Is.True);
|
||||||
Assert.That(actionCalled, Is.True);
|
Assert.That(actionCalled, Is.True);
|
||||||
|
Assert.That(changeActionCalled, Is.True);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
@ -348,10 +368,20 @@ public void CallbackRemoveTest()
|
|||||||
Assert.That(!clientSyncDictionary.ContainsKey(1));
|
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);
|
serverSyncDictionary.Remove(1);
|
||||||
SerializeDeltaTo(serverSyncDictionary, clientSyncDictionary);
|
SerializeDeltaTo(serverSyncDictionary, clientSyncDictionary);
|
||||||
Assert.That(called, Is.True);
|
Assert.That(called, Is.True);
|
||||||
Assert.That(actionCalled, Is.True);
|
Assert.That(actionCalled, Is.True);
|
||||||
|
Assert.That(changeActionCalled, Is.True);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
|
Loading…
Reference in New Issue
Block a user