"Improved" InvokeCallbackSafe method to better guard against concurrent modification #1254
Conversation
…allbacks in order to better guard agains removal and addition of new callbacks while still inside the loop
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Modified all InvokeCallbackSafe methods to Clone the InlineArray of callbacks in order to better guard against removal and addition of new callbacks while still inside the loop.
Currently, when a user registers a new callback, inside another callback, the callback gets executed immediately. I believe this is counter intuitive to what a user might expect.
For example, when an input action is triggered a game object is enabled. Inside that gameobject OnEnabled is called. Say I want to subscribe to the same input action that triggered this. However, as soon as I subscribe to the input action my method will be executed before even Start is called.
One possible solution could have been to iterate over the callback array backwards, however, this wouldn't account for callbacks being removed and added while still in the loop.
My solution is to use the Clone method of the InlineArray to create a copy of the array we can iterate over to avoid being affected by concurrent modification.