Skip to content
This repository was archived by the owner on Jan 18, 2022. It is now read-only.

Commit fa5dfa2

Browse files
authored
Simplify callbacks (#1348)
* Experimental selective burning of all things callback * Changelog * Flip order callbacks to be: Requests -> Responses EntityCallbacks no longer grow unbounded.
1 parent c77d059 commit fa5dfa2

22 files changed

+152
-207
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
- Produce code coverage reports in tests [#1359](https://github.com/spatialos/gdk-for-unity/pull/1359)
2424
- Replaced code generated ReferenceProviders with generic version. [#1358](https://github.com/spatialos/gdk-for-unity/pull/1358)
25+
- Refactor callbacks API. [#1348](https://github.com/spatialos/gdk-for-unity/pull/1348)
2526

2627
## `0.3.5` - 2020-04-23
2728

test-project/Assets/Generated/Source/improbable/dependentschema/DependentDataComponentCommandSenderReceiver.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public class DependentDataComponentCommandReceiver : ICommandReceiver
116116
return;
117117
}
118118

119-
callbackSystem.UnregisterCommandRequestCallback(key);
119+
callbackSystem.UnregisterCommandRequestCallback<global::Improbable.DependentSchema.DependentDataComponent.BarCommand.ReceivedRequest>(key);
120120
barCommandCallbackToCallbackKey.Remove(value);
121121
}
122122
}
@@ -152,7 +152,7 @@ public void RemoveAllCallbacks()
152152
{
153153
foreach (var callbackToKey in barCommandCallbackToCallbackKey)
154154
{
155-
callbackSystem.UnregisterCommandRequestCallback(callbackToKey.Value);
155+
callbackSystem.UnregisterCommandRequestCallback<global::Improbable.DependentSchema.DependentDataComponent.BarCommand.ReceivedRequest>(callbackToKey.Value);
156156
}
157157

158158
barCommandCallbackToCallbackKey.Clear();

workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/AuthorityConstraintCallbackManager.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Improbable.Gdk.Subscriptions
77
{
88
internal class AuthorityConstraintCallbackManager : IAuthorityCallbackManager
99
{
10-
private readonly Callbacks<AuthorityChangeReceived> callbacks = new Callbacks<AuthorityChangeReceived>();
10+
private readonly CallbackCollection<AuthorityChangeReceived> callbackCollection = new CallbackCollection<AuthorityChangeReceived>();
1111
private readonly uint componentId;
1212
private readonly ComponentUpdateSystem componentUpdateSystem;
1313

@@ -26,11 +26,11 @@ public void InvokeCallbacks()
2626
{
2727
if (changes[i].Authority == Authority.Authoritative)
2828
{
29-
callbacks.InvokeAll(changes[i]);
29+
callbackCollection.InvokeAll(changes[i]);
3030
}
3131
else if (changes[i].Authority == Authority.NotAuthoritative)
3232
{
33-
callbacks.InvokeAllReverse(changes[i]);
33+
callbackCollection.InvokeAllReverse(changes[i]);
3434
}
3535
}
3636
}
@@ -42,20 +42,20 @@ public void InvokeLossImminentCallbacks()
4242
{
4343
if (changes[i].Authority == Authority.AuthorityLossImminent)
4444
{
45-
callbacks.InvokeAllReverse(changes[i]);
45+
callbackCollection.InvokeAllReverse(changes[i]);
4646
}
4747
}
4848
}
4949

5050
public ulong RegisterCallback(Action<AuthorityChangeReceived> callback)
5151
{
52-
callbacks.Add(nextCallbackId, callback);
52+
callbackCollection.Add(nextCallbackId, callback);
5353
return nextCallbackId++;
5454
}
5555

5656
public bool UnregisterCallback(ulong callbackKey)
5757
{
58-
return callbacks.Remove(callbackKey);
58+
return callbackCollection.Remove(callbackKey);
5959
}
6060
}
6161
}

workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/CommandRequestCallbackManager.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,12 @@ namespace Improbable.Gdk.Subscriptions
77
{
88
public class CommandRequestCallbackManager<T> : ICallbackManager where T : struct, IReceivedCommandRequest
99
{
10-
private readonly IndexedCallbacks<T> callbacks = new IndexedCallbacks<T>();
10+
private readonly EntityCallbacks<T> callbacks = new EntityCallbacks<T>();
1111
private readonly CommandSystem commandSystem;
1212

13-
private ulong nextCallbackId = 1;
14-
15-
public CommandRequestCallbackManager(World world)
13+
public CommandRequestCallbackManager(CommandSystem commandSystem)
1614
{
17-
commandSystem = world.GetExistingSystem<CommandSystem>();
15+
this.commandSystem = commandSystem;
1816
}
1917

2018
public void InvokeCallbacks()
@@ -23,14 +21,13 @@ public void InvokeCallbacks()
2321
for (var i = 0; i < requests.Count; ++i)
2422
{
2523
ref readonly var request = ref requests[i];
26-
callbacks.InvokeAll(request.EntityId.Id, request);
24+
callbacks.InvokeAll(request.EntityId, request);
2725
}
2826
}
2927

3028
public ulong RegisterCallback(EntityId entityId, Action<T> callback)
3129
{
32-
callbacks.Add(entityId.Id, nextCallbackId, callback);
33-
return nextCallbackId++;
30+
return callbacks.Add(entityId, callback);
3431
}
3532

3633
public bool UnregisterCallback(ulong callbackKey)
Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using Improbable.Gdk.Core;
34
using Improbable.Gdk.Core.Commands;
45
using Unity.Entities;
@@ -7,11 +8,9 @@ namespace Improbable.Gdk.Subscriptions
78
{
89
public class CommandResponseCallbackManager<T> : ICallbackManager where T : struct, IReceivedCommandResponse
910
{
10-
private readonly SingleUseIndexCallbacks<T> callbacks = new SingleUseIndexCallbacks<T>();
11+
private readonly Dictionary<long, Action<T>> callbacks = new Dictionary<long, Action<T>>();
1112
private readonly CommandSystem commandSystem;
1213

13-
private ulong nextCallbackId = 1;
14-
1514
public CommandResponseCallbackManager(World world)
1615
{
1716
commandSystem = world.GetExistingSystem<CommandSystem>();
@@ -23,20 +22,22 @@ public void InvokeCallbacks()
2322
for (var i = 0; i < responses.Count; ++i)
2423
{
2524
ref readonly var response = ref responses[i];
26-
callbacks.InvokeAll(response.RequestId, response);
27-
callbacks.RemoveAllCallbacksForIndex(response.RequestId);
25+
if (callbacks.TryGetValue(response.RequestId, out var callback))
26+
{
27+
callbacks.Remove(response.RequestId);
28+
callback(response);
29+
}
2830
}
2931
}
3032

31-
public ulong RegisterCallback(long requestId, Action<T> callback)
33+
public void RegisterCallback(long requestId, Action<T> callback)
3234
{
33-
callbacks.Add(requestId, nextCallbackId, callback);
34-
return nextCallbackId++;
35+
callbacks.Add(requestId, callback);
3536
}
3637

37-
public bool UnregisterCallback(ulong callbackKey)
38+
public bool UnregisterCallback(ulong requestId)
3839
{
39-
return callbacks.Remove(callbackKey);
40+
return callbacks.Remove((long) requestId);
4041
}
4142
}
4243
}

workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/ComponentAddedCallbackManager.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace Improbable.Gdk.Subscriptions
66
{
77
internal class ComponentAddedCallbackManager : ICallbackManager
88
{
9-
private readonly Callbacks<EntityId> callbacks = new Callbacks<EntityId>();
9+
private readonly CallbackCollection<EntityId> callbackCollection = new CallbackCollection<EntityId>();
1010
private readonly uint componentId;
1111
private readonly EntityManager entityManager;
1212
private readonly ComponentUpdateSystem componentUpdateSystem;
@@ -24,19 +24,19 @@ public void InvokeCallbacks()
2424
var entities = componentUpdateSystem.GetComponentsAdded(componentId);
2525
foreach (var entityId in entities)
2626
{
27-
callbacks.InvokeAll(entityId);
27+
callbackCollection.InvokeAll(entityId);
2828
}
2929
}
3030

3131
public ulong RegisterCallback(Action<EntityId> callback)
3232
{
33-
callbacks.Add(nextCallbackId, callback);
33+
callbackCollection.Add(nextCallbackId, callback);
3434
return nextCallbackId++;
3535
}
3636

3737
public bool UnregisterCallback(ulong callbackKey)
3838
{
39-
return callbacks.Remove(callbackKey);
39+
return callbackCollection.Remove(callbackKey);
4040
}
4141
}
4242
}

workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/ComponentAuthorityCallbackManager.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@ namespace Improbable.Gdk.Subscriptions
77
{
88
internal class ComponentAuthorityCallbackManager : IAuthorityCallbackManager
99
{
10-
private readonly IndexedCallbacks<Authority> callbacks = new IndexedCallbacks<Authority>();
10+
private readonly EntityCallbacks<Authority> callbacks = new EntityCallbacks<Authority>();
1111
private readonly uint componentId;
1212
private readonly ComponentUpdateSystem componentUpdateSystem;
1313

14-
private ulong nextCallbackId = 1;
15-
1614
public ComponentAuthorityCallbackManager(uint componentId, World world)
1715
{
1816
this.componentId = componentId;
@@ -27,10 +25,10 @@ public void InvokeCallbacks()
2725
switch (changes[i].Authority)
2826
{
2927
case Authority.Authoritative:
30-
callbacks.InvokeAll(changes[i].EntityId.Id, changes[i].Authority);
28+
callbacks.InvokeAll(changes[i].EntityId, changes[i].Authority);
3129
break;
3230
case Authority.NotAuthoritative:
33-
callbacks.InvokeAllReverse(changes[i].EntityId.Id, changes[i].Authority);
31+
callbacks.InvokeAllReverse(changes[i].EntityId, changes[i].Authority);
3432
break;
3533
}
3634
}
@@ -43,15 +41,14 @@ public void InvokeLossImminentCallbacks()
4341
{
4442
if (changes[i].Authority == Authority.AuthorityLossImminent)
4543
{
46-
callbacks.InvokeAllReverse(changes[i].EntityId.Id, changes[i].Authority);
44+
callbacks.InvokeAllReverse(changes[i].EntityId, changes[i].Authority);
4745
}
4846
}
4947
}
5048

5149
public ulong RegisterCallback(EntityId entityId, Action<Authority> callback)
5250
{
53-
callbacks.Add(entityId.Id, nextCallbackId, callback);
54-
return nextCallbackId++;
51+
return callbacks.Add(entityId, callback);
5552
}
5653

5754
public bool UnregisterCallback(ulong callbackKey)

workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/ComponentEventCallbackManager.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@ namespace Improbable.Gdk.Subscriptions
66
{
77
internal class ComponentEventCallbackManager<T> : ICallbackManager where T : struct, IEvent
88
{
9-
private readonly IndexedCallbacks<T> callbacks = new IndexedCallbacks<T>();
9+
private readonly EntityCallbacks<T> callbacks = new EntityCallbacks<T>();
1010
private readonly ComponentUpdateSystem componentUpdateSystem;
1111

12-
private ulong nextCallbackId = 1;
13-
1412
public ComponentEventCallbackManager(World world)
1513
{
1614
componentUpdateSystem = world.GetExistingSystem<ComponentUpdateSystem>();
@@ -22,14 +20,13 @@ public void InvokeCallbacks()
2220
for (var i = 0; i < updates.Count; ++i)
2321
{
2422
ref readonly var update = ref updates[i];
25-
callbacks.InvokeAll(update.EntityId.Id, in update.Event);
23+
callbacks.InvokeAll(update.EntityId, in update.Event);
2624
}
2725
}
2826

2927
public ulong RegisterCallback(EntityId entityId, Action<T> callback)
3028
{
31-
callbacks.Add(entityId.Id, nextCallbackId, callback);
32-
return nextCallbackId++;
29+
return callbacks.Add(entityId, callback);
3330
}
3431

3532
public bool UnregisterCallback(ulong callbackKey)

workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/ComponentRemovedCallbackManager.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace Improbable.Gdk.Subscriptions
66
{
77
internal class ComponentRemovedCallbackManager : ICallbackManager
88
{
9-
private readonly Callbacks<EntityId> callbacks = new Callbacks<EntityId>();
9+
private readonly CallbackCollection<EntityId> callbackCollection = new CallbackCollection<EntityId>();
1010
private readonly uint componentId;
1111
private readonly EntityManager entityManager;
1212
private readonly ComponentUpdateSystem componentUpdateSystem;
@@ -25,19 +25,19 @@ public void InvokeCallbacks()
2525
var entities = componentUpdateSystem.GetComponentsRemoved(componentId);
2626
foreach (var entityId in entities)
2727
{
28-
callbacks.InvokeAllReverse(entityId);
28+
callbackCollection.InvokeAllReverse(entityId);
2929
}
3030
}
3131

3232
public ulong RegisterCallback(Action<EntityId> callback)
3333
{
34-
callbacks.Add(nextCallbackId, callback);
34+
callbackCollection.Add(nextCallbackId, callback);
3535
return nextCallbackId++;
3636
}
3737

3838
public bool UnregisterCallback(ulong callbackKey)
3939
{
40-
return callbacks.Remove(callbackKey);
40+
return callbackCollection.Remove(callbackKey);
4141
}
4242
}
4343
}

workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/ComponentUpdateCallbackManager.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@ namespace Improbable.Gdk.Subscriptions
66
{
77
internal class ComponentUpdateCallbackManager<T> : ICallbackManager where T : struct, ISpatialComponentUpdate
88
{
9-
private readonly IndexedCallbacks<T> callbacks = new IndexedCallbacks<T>();
9+
private readonly EntityCallbacks<T> callbacks = new EntityCallbacks<T>();
1010
private readonly ComponentUpdateSystem componentUpdateSystem;
1111

12-
private ulong nextCallbackId = 1;
13-
1412
public ComponentUpdateCallbackManager(World world)
1513
{
1614
componentUpdateSystem = world.GetExistingSystem<ComponentUpdateSystem>();
@@ -22,14 +20,13 @@ public void InvokeCallbacks()
2220
for (var i = 0; i < updates.Count; ++i)
2321
{
2422
ref readonly var update = ref updates[i];
25-
callbacks.InvokeAll(update.EntityId.Id, in update.Update);
23+
callbacks.InvokeAll(update.EntityId, in update.Update);
2624
}
2725
}
2826

2927
public ulong RegisterCallback(EntityId entityId, Action<T> callback)
3028
{
31-
callbacks.Add(entityId.Id, nextCallbackId, callback);
32-
return nextCallbackId++;
29+
return callbacks.Add(entityId, callback);
3330
}
3431

3532
public bool UnregisterCallback(ulong callbackKey)

0 commit comments

Comments
 (0)