-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
Initial release
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
exclude_paths: | ||
- '*.md' | ||
- '*.json' |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Changelog | ||
|
||
## [1.0.0] - 2020-07-31 | ||
|
||
### Added | ||
- Initial version |
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2020 Christiaan Bloemendaal | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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 |
---|---|---|
@@ -0,0 +1,138 @@ | ||
# Safe Event | ||
|
||
<p align="center"> | ||
<img alt="GitHub package.json version" src ="https://img.shields.io/github/package-json/v/Thundernerd/Unity3D-SafeEvent" /> | ||
<a href="/~https://github.com/Thundernerd/Unity3D-SafeEvent/issues"> | ||
<img alt="GitHub issues" src ="https://img.shields.io/github/issues/Thundernerd/Unity3D-SafeEvent" /> | ||
</a> | ||
<a href="/~https://github.com/Thundernerd/Unity3D-SafeEvent/pulls"> | ||
<img alt="GitHub pull requests" src ="https://img.shields.io/github/issues-pr/Thundernerd/Unity3D-SafeEvent" /> | ||
</a> | ||
<a href="/~https://github.com/Thundernerd/Unity3D-SafeEvent/blob/master/LICENSE.md"> | ||
<img alt="GitHub license" src ="https://img.shields.io/github/license/Thundernerd/Unity3D-SafeEvent" /> | ||
</a> | ||
<img alt="GitHub last commit" src ="https://img.shields.io/github/last-commit/Thundernerd/Unity3D-SafeEvent" /> | ||
</p> | ||
|
||
An event class that has extra checks to help prevent mistakes. | ||
|
||
It will notify you when you've subscribed more than once with the same callback. In case your callback lies within a MonoBehaviour it will also detect if that behaviour and/or the GameObject has been destroyed and will let you know if you forgot to unsubscribe from the event. | ||
|
||
## Installation | ||
1. The package is available on the [openupm registry](https://openupm.com). You can install it via [openupm-cli](/~https://github.com/openupm/openupm-cli). | ||
``` | ||
openupm add net.tnrd.safeevent | ||
``` | ||
2. You can also install via git url by adding these entries in your **manifest.json** | ||
```json | ||
"net.tnrd.safeevent": "/~https://github.com/Thundernerd/Unity3D-SafeEvent.git", | ||
``` | ||
|
||
## Usage | ||
|
||
There are three versions of Safe Event that you can use. One without any parameters, one with a single parameter, and one with two parameters. | ||
|
||
### Definition | ||
|
||
The definition is the same as you would define an Action | ||
|
||
```c# | ||
private SafeEvent onFooEvent; | ||
``` | ||
|
||
```c# | ||
private SafeEvent<int> onFooEvent; | ||
``` | ||
|
||
```c# | ||
private SafeEvent<int, string> onFooEvent; | ||
``` | ||
|
||
### Subscriptions | ||
|
||
Subscribing and unsubscribing can be done in two ways. | ||
|
||
One way is through the Subscribe and Unsubscribe methods | ||
|
||
```c# | ||
private void SubscribeToEvent() | ||
{ | ||
onFooEvent.Subscribe(ExecuteOnEvent); | ||
} | ||
|
||
private void ExecuteOnEvent() | ||
{ | ||
[...] | ||
} | ||
|
||
private void UnsubscribeFromEvent() | ||
{ | ||
onFooEvent.Unsubscribe(ExecuteOnEvent); | ||
} | ||
``` | ||
|
||
And the other is through the use of operators | ||
|
||
```c# | ||
private void SubscribeToEvent() | ||
{ | ||
onFooEvent += ExecuteOnEvent; | ||
} | ||
|
||
private void ExecuteOnEvent() | ||
{ | ||
[...] | ||
} | ||
|
||
private void UnsubscribeFromEvent() | ||
{ | ||
onFooEvent -= ExecuteOnEvent; | ||
} | ||
``` | ||
|
||
### Invocation | ||
|
||
Invoking the event is the same as you would invoke a regular event | ||
|
||
```c# | ||
private SafeEvent onFooEvent; | ||
|
||
[...] | ||
|
||
private void InvokeEvent() | ||
{ | ||
onFooEvent.Invoke(); | ||
} | ||
``` | ||
|
||
```c# | ||
private SafeEvent<int> onFooEvent; | ||
|
||
[...] | ||
|
||
private void InvokeEvent() | ||
{ | ||
onFooEvent.Invoke(123); | ||
} | ||
``` | ||
|
||
```c# | ||
private SafeEvent<int, string> onFooEvent; | ||
|
||
[...] | ||
|
||
private void InvokeEvent() | ||
{ | ||
onFooEvent.Invoke(123, "abcde"); | ||
} | ||
``` | ||
|
||
|
||
## Support | ||
**Safe Event** is a small and open-source utility that I hope helps other people. It is by no means necessary but if you feel generous you can support me by donating. | ||
|
||
[](https://ko-fi.com/J3J11GEYY) | ||
|
||
## Contributions | ||
Pull requests are welcomed. Please feel free to fix any issues you find, or add new features. | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using System; | ||
|
||
namespace TNRD.Events | ||
{ | ||
public class DuplicateSubscriptionException : Exception | ||
{ | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,82 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using UnityEngine; | ||
|
||
namespace TNRD.Events | ||
{ | ||
public struct SafeEvent | ||
{ | ||
private HashSet<Action> subscriptions; | ||
|
||
public int Count => subscriptions?.Count ?? 0; | ||
|
||
public void RemoveAllSubscriptions() | ||
{ | ||
subscriptions.Clear(); | ||
} | ||
|
||
public void Invoke() | ||
{ | ||
foreach (var subscription in subscriptions) | ||
{ | ||
#if UNITY_EDITOR || DEBUG | ||
ThrowIfInvalidSubscription(subscription); | ||
#endif | ||
subscription?.Invoke(); | ||
} | ||
} | ||
|
||
private static void ThrowIfInvalidSubscription(Action subscription) | ||
{ | ||
if (subscription?.Target == null) | ||
{ | ||
throw new SubscriptionIsNullException(); | ||
} | ||
|
||
if (subscription.Target is Component component && !component) | ||
{ | ||
throw new SubscriptionIsNullException(); | ||
} | ||
|
||
if (subscription.Target is GameObject gameObject && !gameObject) | ||
{ | ||
throw new SubscriptionIsNullException(); | ||
} | ||
} | ||
|
||
public void Subscribe(Action action) | ||
{ | ||
if (subscriptions == null) | ||
{ | ||
subscriptions = new HashSet<Action>(); | ||
} | ||
|
||
if (subscriptions.Contains(action)) | ||
{ | ||
#if UNITY_EDITOR || DEBUG | ||
throw new DuplicateSubscriptionException(); | ||
#endif | ||
return; | ||
} | ||
|
||
subscriptions.Add(action); | ||
} | ||
|
||
public void Unsubscribe(Action action) | ||
{ | ||
subscriptions?.Remove(action); | ||
} | ||
|
||
public static SafeEvent operator +(SafeEvent safeEvent, Action action) | ||
{ | ||
safeEvent.Subscribe(action); | ||
return safeEvent; | ||
} | ||
|
||
public static SafeEvent operator -(SafeEvent safeEvent, Action action) | ||
{ | ||
safeEvent.Unsubscribe(action); | ||
return safeEvent; | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,82 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using UnityEngine; | ||
|
||
namespace TNRD.Events | ||
{ | ||
public struct SafeEvent<T> | ||
{ | ||
private HashSet<Action<T>> subscriptions; | ||
|
||
public int Count => subscriptions?.Count ?? 0; | ||
|
||
public void RemoveAllSubscriptions() | ||
{ | ||
subscriptions.Clear(); | ||
} | ||
|
||
public void Invoke(T obj) | ||
{ | ||
foreach (var subscription in subscriptions) | ||
{ | ||
#if UNITY_EDITOR || DEBUG | ||
ThrowIfInvalidSubscription(subscription); | ||
#endif | ||
subscription?.Invoke(obj); | ||
} | ||
} | ||
|
||
private static void ThrowIfInvalidSubscription(Action<T> subscription) | ||
{ | ||
if (subscription?.Target == null) | ||
{ | ||
throw new SubscriptionIsNullException(); | ||
} | ||
|
||
if (subscription.Target is Component component && !component) | ||
{ | ||
throw new SubscriptionIsNullException(); | ||
} | ||
|
||
if (subscription.Target is GameObject gameObject && !gameObject) | ||
{ | ||
throw new SubscriptionIsNullException(); | ||
} | ||
} | ||
|
||
public void Subscribe(Action<T> action) | ||
{ | ||
if (subscriptions == null) | ||
{ | ||
subscriptions = new HashSet<Action<T>>(); | ||
} | ||
|
||
if (subscriptions.Contains(action)) | ||
{ | ||
#if UNITY_EDITOR || DEBUG | ||
throw new DuplicateSubscriptionException(); | ||
#endif | ||
return; | ||
} | ||
|
||
subscriptions.Add(action); | ||
} | ||
|
||
public void Unsubscribe(Action<T> action) | ||
{ | ||
subscriptions?.Remove(action); | ||
} | ||
|
||
public static SafeEvent<T> operator +(SafeEvent<T> safeEvent, Action<T> action) | ||
{ | ||
safeEvent.Subscribe(action); | ||
return safeEvent; | ||
} | ||
|
||
public static SafeEvent<T> operator -(SafeEvent<T> safeEvent, Action<T> action) | ||
{ | ||
safeEvent.Unsubscribe(action); | ||
return safeEvent; | ||
} | ||
} | ||
} |