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

Commit

Permalink
Subscription cleanup (#1200)
Browse files Browse the repository at this point in the history
* Fix filename

* Fix folder name

* Split Callbacks.cs into separate files per class

* Remove unused EntitySubscriptions.cs
Folder rename metafiles

* AggregateSubscriptions are now made by the SubscriptionSystem

* Don't clear empty lists
Named tuples

* Formatting to style guide

* Remove GuardedAuthorityCallbackManagerSet as its a duplicated of the GuardedCallbackManagerSet

* Callback arguments are passed as ref readonly (in)

* Remove unused HashSet in WorkerFlagSubscriptionManager

* Have subscriptions use Option<T> instead of re-implementing optional values

* Changelog
  • Loading branch information
zeroZshadow authored Oct 25, 2019
1 parent 8710dd5 commit 82c49b5
Show file tree
Hide file tree
Showing 52 changed files with 259 additions and 292 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@

- Generated Worker ID's for local development are now smaller and easier to read for debugging. [#1197](https://github.com/spatialos/gdk-for-unity/pull/1197)

### Internal

- Cleaned up Subscriptions and Callbacks. [#1200](https://github.com/spatialos/gdk-for-unity/pull/1200)
- Replaced usage of `GuardedAuthorityCallbackManagerSet` with more generic `GuardedCallbackManagerSet`.
- Removed unused `EntitySubscriptions` class.
- Formatting pass on all Subscriptions and Callbacks files.

## `0.2.10` - 2019-10-14

### Breaking Changes
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public AuthorityConstraintCallbackManager(uint componentId, World world)
public void InvokeCallbacks()
{
var changes = componentUpdateSystem.GetAuthorityChangesReceived(componentId);
for (int i = 0; i < changes.Count; ++i)
for (var i = 0; i < changes.Count; ++i)
{
if (changes[i].Authority == Authority.Authoritative)
{
Expand All @@ -38,7 +38,7 @@ public void InvokeCallbacks()
public void InvokeLossImminentCallbacks()
{
var changes = componentUpdateSystem.GetAuthorityChangesReceived(componentId);
for (int i = 0; i < changes.Count; ++i)
for (var i = 0; i < changes.Count; ++i)
{
if (changes[i].Authority == Authority.AuthorityLossImminent)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public CommandRequestCallbackManager(World world)
public void InvokeCallbacks()
{
var requests = commandSystem.GetRequests<T>();
for (int i = 0; i < requests.Count; ++i)
for (var i = 0; i < requests.Count; ++i)
{
ref readonly var request = ref requests[i];
callbacks.InvokeAll(request.GetEntityId().Id, request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public CommandResponseCallbackManager(World world)
public void InvokeCallbacks()
{
var responses = commandSystem.GetResponses<T>();
for (int i = 0; i < responses.Count; ++i)
for (var i = 0; i < responses.Count; ++i)
{
ref readonly var response = ref responses[i];
callbacks.InvokeAll(response.GetRequestId(), response);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using Improbable.Gdk.Core;
using Improbable.Worker.CInterop;
using Unity.Entities;

namespace Improbable.Gdk.Subscriptions
Expand All @@ -23,9 +22,9 @@ public ComponentAddedCallbackManager(uint componentId, World world)
public void InvokeCallbacks()
{
var entities = componentUpdateSystem.GetComponentsAdded(componentId);
for (int i = 0; i < entities.Count; ++i)
foreach (var entityId in entities)
{
callbacks.InvokeAll(entities[i]);
callbacks.InvokeAll(entityId);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,24 @@ public ComponentAuthorityCallbackManager(uint componentId, World world)
public void InvokeCallbacks()
{
var changes = componentUpdateSystem.GetAuthorityChangesReceived(componentId);
for (int i = 0; i < changes.Count; ++i)
for (var i = 0; i < changes.Count; ++i)
{
if (changes[i].Authority == Authority.Authoritative)
switch (changes[i].Authority)
{
callbacks.InvokeAll(changes[i].EntityId.Id, changes[i].Authority);
}
else if (changes[i].Authority == Authority.NotAuthoritative)
{
callbacks.InvokeAllReverse(changes[i].EntityId.Id, changes[i].Authority);
case Authority.Authoritative:
callbacks.InvokeAll(changes[i].EntityId.Id, changes[i].Authority);
break;
case Authority.NotAuthoritative:
callbacks.InvokeAllReverse(changes[i].EntityId.Id, changes[i].Authority);
break;
}
}
}

public void InvokeLossImminentCallbacks()
{
var changes = componentUpdateSystem.GetAuthorityChangesReceived(componentId);
for (int i = 0; i < changes.Count; ++i)
for (var i = 0; i < changes.Count; ++i)
{
if (changes[i].Authority == Authority.AuthorityLossImminent)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Improbable.Gdk.Subscriptions
{
internal class ComponentEventCallbackManager<T> : ICallbackManager where T : IEvent
internal class ComponentEventCallbackManager<T> : ICallbackManager where T : struct, IEvent
{
private readonly IndexedCallbacks<T> callbacks = new IndexedCallbacks<T>();
private readonly ComponentUpdateSystem componentUpdateSystem;
Expand All @@ -19,10 +19,10 @@ public ComponentEventCallbackManager(World world)
public void InvokeCallbacks()
{
var updates = componentUpdateSystem.GetEventsReceived<T>();
for (int i = 0; i < updates.Count; ++i)
for (var i = 0; i < updates.Count; ++i)
{
ref readonly var update = ref updates[i];
callbacks.InvokeAll(update.EntityId.Id, update.Event);
callbacks.InvokeAll(update.EntityId.Id, in update.Event);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public void InvokeCallbacks()
{
// todo like entity stuff this should also be temporarily removed components
var entities = componentUpdateSystem.GetComponentsRemoved(componentId);
for (int i = 0; i < entities.Count; ++i)
foreach (var entityId in entities)
{
callbacks.InvokeAllReverse(entities[i]);
callbacks.InvokeAllReverse(entityId);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Improbable.Gdk.Subscriptions
{
internal class ComponentUpdateCallbackManager<T> : ICallbackManager where T : ISpatialComponentUpdate
internal class ComponentUpdateCallbackManager<T> : ICallbackManager where T : struct, ISpatialComponentUpdate
{
private readonly IndexedCallbacks<T> callbacks = new IndexedCallbacks<T>();
private readonly ComponentUpdateSystem componentUpdateSystem;
Expand All @@ -19,10 +19,10 @@ public ComponentUpdateCallbackManager(World world)
public void InvokeCallbacks()
{
var updates = componentUpdateSystem.GetComponentUpdatesReceived<T>();
for (int i = 0; i < updates.Count; ++i)
for (var i = 0; i < updates.Count; ++i)
{
ref readonly var update = ref updates[i];
callbacks.InvokeAll(update.EntityId.Id, update.Update);
callbacks.InvokeAll(update.EntityId.Id, in update.Update);
}
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public EntityAddedCallbackManager(World world)
public void InvokeCallbacks()
{
var entities = entitySystem.GetEntitiesAdded();
for (int i = 0; i < entities.Count; ++i)
foreach (var entityId in entities)
{
callbacks.InvokeAll(entities[i]);
callbacks.InvokeAll(entityId);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public EntityRemovedCallbackManager(World world)
public void InvokeCallbacks()
{
var entities = entitySystem.GetEntitiesRemoved();
for (int i = 0; i < entities.Count; ++i)
foreach (var entityId in entities)
{
callbacks.InvokeAllReverse(entities[i]);
callbacks.InvokeAllReverse(entityId);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;

namespace Improbable.Gdk.Subscriptions
Expand All @@ -22,23 +23,25 @@ public void AddCallbackManager(TIndex index, TManager manager)
indexesToAdd.Add(index);
}

public List<TManager> GetManagers()
private void UpdateManagers()
{
foreach (var indexToAdd in indexesToAdd)
if (indexesToAdd.Count > 0)
{
callbackManagers.Add(indexToCallbackManager[indexToAdd]);
}

indexesToAdd.Clear();
foreach (var indexToAdd in indexesToAdd)
{
callbackManagers.Add(indexToCallbackManager[indexToAdd]);
}

return callbackManagers;
indexesToAdd.Clear();
}
}

public void InvokeCallbacks()
public void InvokeEach(Action<TManager> callback)
{
foreach (var manager in GetManagers())
UpdateManagers();
foreach (var manager in callbackManagers)
{
manager.InvokeCallbacks();
callback(manager);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public bool UnregisterCallback(ulong callbackKey)
public void InvokeCallbacks()
{
var workerFlagChanges = workerSystem.Diff.GetWorkerFlagChanges();
for (int i = 0; i < workerFlagChanges.Count; ++i)
for (var i = 0; i < workerFlagChanges.Count; ++i)
{
var pair = workerFlagChanges[i];
callbacks.InvokeAll(pair);
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 82c49b5

Please sign in to comment.