Advanced Features - SixWays/Relay GitHub Wiki
IRelayLink
is a wrapper for a Relay
, giving access to all functionality except Dispatch
.
Each IRelayLink
method/property simply passes through to the referenced Relay
, but that reference is kept private. This is useful for enforcing event
-like encapsulation; i.e. allowing calling code to add/remove listeners, but allowing only the Relay's owner to call Dispatch
.
Relay
types have a property link
which lazily (and once only per instance) instantiates and returns an appropriate IRelayLink
wrapper. This can be thought of a bit like List<T>.AsReadOnly()
. It's not recommended to create an IRelayLink any other way.
All Relay
classes and RelayLink
classes implement the IRelayLink
interface so that calling code can equally accept either.
private Relay<int> _onMyEvent = new Relay<int>();
public IRelayLink<int> onMyEvent {get {return _onMyEvent.link;}}
Relay.BindListener()
adds a listener (exactly the same as AddListener) but also returns an IRelayBinding
object which allows simple enabling/disabling of a listener on a Relay
.
Calling Enable(true)
adds the listener to the bound Relay
and Enable(false)
removes it. This is convenient for situations where regular toggling of a listener is desired without having to keep a reference to the Relay
.
IRelayBinding
doesn't reference the generic type of the Relay
so all bindings are polymorphic. Therefore you can keep all bindings in, say, a single List
and easily disable them all at once (for example in OnDestroy()
).
bool enabled
Is the listener currently subscribed to the Relay
?
bool allowDuplicates
If false, calling Enable(true)
will fail if the listener has already been added to the bound Relay
. Note that Enable
will always fail (returning false) if trying to enable an already-enabled binding or vice versa.
uint listenerCount
How many persistent listeners does the bound Relay
currently have?
bool Enable(bool enable)
Enable or disable the listener on the bound Relay
. Return value indicates success, not current state of enabled
. Will fail (returning false) if trying to enable an already-enabled binding or vice versa.