API Reference and Guide - SixWays/Relay GitHub Wiki
All Relay
classes derive from RelayBase
and have identical functionality apart from the number of arguments listeners take. Relay
uses System.Action
as the delegate type, and therefore takes zero to four arguments. The type TDelegate
in the API reference is Action
, Action<T>
, Action<T,U>
, Action<T,U,V>
, or Action<T,U,V,W>
.
Relay
s must be instantiated like any other class before use.
using Sigtrap.Relays;
public class RelayDispatchTest : MonoBehaviour {
// Type arguments are the arguments passed to listener methods
// Can have 0 - 4 arguments (for 0 just use Relay without type arguments)
public Relay<bool> onEnabled = new Relay<bool>();
void OnEnable(){
// Invokes any listeners on onEnabled relay, passing true as the argument
onEnabled.Dispatch(true);
}
void OnDisable(){
onEnabled.Dispatch(false);
}
}
public class RelayObserverTest : MonoBehaviour {
void Awake(){
// Add method OnEnabledHandler as a listener to onEnabled relay for RelayDispatchTest instance
GetComponent<RelayDispatchTest>().onEnabled.AddListener(OnEnabledHandler);
}
// Can be added as a listener to any Relay<bool>
// Private, protected or public methods can be used as listeners
void OnEnabledHandler(bool e){
// Do something
}
// Make sure to remove listener at end of lifetime to avoid leaks
void OnDestroy(){
GetComponent<RelayDispatchTest>().onEnabled.RemoveListener(OnEnabledHandler);
}
}
uint listenerCount
Returns the number of persistent delegates listening to this instance.
uint oneTimeListenerCount
Returns the number of one-time delegates listening to this instance.
IRelayLink<TDelegate> link
Get an IRelayLink object that wraps this Relay without allowing Dispatch. Provides a safe interface for classes outside the Relay's "owner".
See Advanced Features.
void AddListener(TDelegate listener, bool allowDuplicates=false)
Adds a persistent listener.
If allowDuplicates
is false
, checks whether this delegate is already a persistent listener. This is safer in most cases but incurs a (very small) overhead.
IRelayBinding<TDelegate> BindListener(TDelegate listener, bool allowDuplicates=false)
Adds listener and creates a RelayBinding between the listener and the Relay. The RelayBinding can be used to enable/disable the listener.
See Advanced Features.
void AddOnce(TDelegate listener, bool allowDuplicates=false)
Adds a one-time listener.
These listeners are removed after one Dispatch.
If allowDuplicates
is false
, checks whether this delegate is already a one-time listener. This is safer in most cases but incurs a (very small) overhead.
void Dispatch(<variable arguments depending on generic type>)
Calls all persistent and one-time listeners with the given arguments. One-time listeners are removed after being called.
bool RemoveListener(TDelegate listener)
Removes a persistent listener, if present.
Returns true
if the listener was present (and removed).
bool RemoveOnce(TDelegate listener)
Removes a one-time listener, if preset.
Returns true
if the listener was present (and removed).
void RemoveAll(bool removePersistentListeners=true, bool removeOneTimeListeners=false)
Removes all persistent and/or one-time listeners.
bool Contains(TDelegate listener)
Is this delegate already a persistent listener?
Does NOT query one-time listeners.