Skip to content
JuDelCo edited this page Jun 1, 2021 · 9 revisions

Namespace: Ju.Handlers

LinkHandlers

LinkHandlers are classes that implements the following interface:

bool IsActive;
bool IsDestroyed;

They are used to handle the lifecycle of callbacks.

For example, if an object is subscribed to the EventBus service to be notified of an event, the callback itself won't know if the object was disposed or destroyed and the callback could fail to run (and even more, the EventBus would no longer have the need to keep saving that callback internally and try to run it).

If the IsActive property is false the callback won't run, and if the IsDestroyed property is true, the callback will be removed automatically. Currently only the IEventBusService, ICoroutineService, ITaskService and Observable<T> classes use LinkHandlers.

This framework provides multiple LinkHandlers types. Each gives you different types of control.

ObjectLinkHandler

Creates a WeakReference for an object so if the object is destroyed, it will report IsActive as false and IsDestroyed as true.

DisposableLinkHandler

Inherits the IDisposable interface. Once disposed, it will report IsActive as false and IsDestroyed as true.

JNodeLinkHandler

Similar to the DisposableLinkHandler with the difference that it holds a reference to a JNode and will report as IsDestroyed when either the JNode or the LinkHandler is disposed.

KeepLinkHandler

Always will report IsActive as true and IsDestroyed as false. Try not to use this unless the callback doesn't reference any object that can be destroyed or be null.

BehaviourLinkHandler

Similar to the ObjectLinkHandler, but this will monitorize a Unity Behaviour. It will report IsActive as true if the behaviour is enabled and active in the scene hierarchy and IsDestroyed as true if the Behaviour was destroyed.

You can make IsActive report always true (so the callback will run even if the Behaviour is disabled) if you pass true in the parameter alwaysActive on the constructor.

Usage (Examples)

Please take note that there are extension methods which will save you from using LinkHandlers in your code. Those extension methods are available for Services, FSM states and Behaviours (MonoBehaviours).

For example, in a method of one of your custom services (IService), you can do:

this.EventSubscribe<TimeUpdateEvent>(e => Update(e.DeltaTime));
this.EventSubscribe<CustomEvent>(CustomServiceMethod);

... instead of:

Core.Event.Subscribe<TimeUpdateEvent>(new ObjectLinkHandler<IService>(this), e => Update(e.DeltaTime));
Core.Event.Subscribe<CustomEvent>(new ObjectLinkHandler<IService>(this), CustomServiceMethod);
Clone this wiki locally