File tree Expand file tree Collapse file tree 2 files changed +39
-0
lines changed Expand file tree Collapse file tree 2 files changed +39
-0
lines changed Original file line number Diff line number Diff line change @@ -34,6 +34,28 @@ public void Unsubscribe<T>(Action<T> action)
34
34
}
35
35
}
36
36
37
+ public bool TryUnsubscribe < T > ( Action < T > action )
38
+ {
39
+ bool isSuccessful = false ;
40
+ lock ( _lock )
41
+ {
42
+ for ( int index = 0 ; index < _entries . Count ; index ++ )
43
+ {
44
+ Entry entry = _entries [ index ] ;
45
+
46
+ if ( entry . Delegate as Action < T > != action || ! _entries . Contains ( entry ) )
47
+ {
48
+ continue ;
49
+ }
50
+
51
+ _entries . Remove ( entry ) ;
52
+ isSuccessful = true ;
53
+ }
54
+ }
55
+
56
+ return isSuccessful ;
57
+ }
58
+
37
59
public void Fire < T > ( T signal )
38
60
{
39
61
int hashCode = typeof ( T ) . GetHashCode ( ) ;
Original file line number Diff line number Diff line change @@ -39,6 +39,23 @@ void FooAction(FooSignal _) { }
39
39
Assert . IsFalse ( _signalCenter . Exists < FooSignal > ( FooAction ) ) ;
40
40
}
41
41
42
+ [ Test ]
43
+ public void TryUnsubscribe ( )
44
+ {
45
+ void BarAction ( BarSignal _ ) { }
46
+ void FooAction ( FooSignal _ ) { }
47
+
48
+ _signalCenter . Subscribe < BarSignal > ( BarAction ) ;
49
+ Assert . IsTrue ( _signalCenter . Exists < BarSignal > ( BarAction ) ) ;
50
+ Assert . IsFalse ( _signalCenter . Exists < FooSignal > ( FooAction ) ) ;
51
+ bool isUnsubscribedFromBar = _signalCenter . TryUnsubscribe < BarSignal > ( BarAction ) ;
52
+ bool isUnsubscribedFromFoo = _signalCenter . TryUnsubscribe < FooSignal > ( FooAction ) ;
53
+ Assert . IsFalse ( _signalCenter . Exists < FooSignal > ( FooAction ) ) ;
54
+ Assert . IsFalse ( _signalCenter . Exists < BarSignal > ( BarAction ) ) ;
55
+ Assert . IsTrue ( isUnsubscribedFromBar ) ;
56
+ Assert . IsFalse ( isUnsubscribedFromFoo ) ;
57
+ }
58
+
42
59
[ Test ]
43
60
public void Subscribe_And_Fire ( )
44
61
{
You can’t perform that action at this time.
0 commit comments