Skip to content

Commit 118403a

Browse files
authored
Merge pull request #2 from Thundernerd/develop
Update Readme & Extra Test Case
2 parents c80e1c0 + 92c927b commit 118403a

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,23 @@ private void InvokeEvent()
127127
}
128128
```
129129

130+
### Exposing
131+
132+
Now that you've set up your Safe Event you most likely want to be able to use it from a different place.
133+
134+
The recommended way of doing this is by exposing an event property as shown below, instead of directly exposing the Safe Event
135+
136+
```c#
137+
private SafeEvent<int> onFooEvent;
138+
139+
public event Action<int> OnFooEvent
140+
{
141+
add => onFooEvent.Subscribe(value);
142+
remove => onFooEvent.Unsubscribe(value);
143+
}
144+
```
145+
146+
The benefit of this is that to the outside it looks and behaves like a normal event even though under the hood it has the benefits of the Safe Event.
130147

131148
## Support
132149
**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.

Tests/Runtime/DummyBehaviour.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using UnityEngine;
1+
using System;
2+
using TNRD.Events;
3+
using UnityEngine;
24

35
public class DummyBehaviour : MonoBehaviour
46
{
@@ -9,6 +11,14 @@ public class DummyBehaviour : MonoBehaviour
911
public int Arg1 { get; private set; }
1012
public int Arg2 { get; private set; }
1113

14+
private SafeEvent onExposedEvent;
15+
16+
public event Action OnExposedEvent
17+
{
18+
add => onExposedEvent.Subscribe(value);
19+
remove => onExposedEvent.Unsubscribe(value);
20+
}
21+
1222
public void Dummy()
1323
{
1424
Invoked = true;
@@ -24,4 +34,9 @@ public void Dummy(int arg1, int arg2)
2434
Arg1 = arg1;
2535
Arg2 = arg2;
2636
}
37+
38+
public void DispatchOnExposedEvent()
39+
{
40+
onExposedEvent.Invoke();
41+
}
2742
}

Tests/Runtime/SafeEventTests.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,25 @@ public IEnumerator Invoke_On_Destroyed_GameObject()
7171
Assert.IsNotNull(exception);
7272
Object.Destroy(holder);
7373
}
74+
75+
[UnityTest]
76+
public IEnumerator Dispatch_Exposed_Action()
77+
{
78+
var holder = new GameObject();
79+
var dummyBehaviour = holder.AddComponent<DummyBehaviour>();
80+
yield return null;
81+
82+
bool invoked = false;
83+
84+
dummyBehaviour.OnExposedEvent += () =>
85+
{
86+
invoked = true;
87+
};
88+
89+
Assert.IsFalse(invoked);
90+
dummyBehaviour.DispatchOnExposedEvent();
91+
Assert.IsTrue(invoked);
92+
93+
Object.Destroy(holder);
94+
}
7495
}

0 commit comments

Comments
 (0)