The ActionEvent system is designed to automate the lifecycle of JavaScript event listeners.
The Lifecycle
When you use the += operator on an ActionEvent property:
- A
Callback is created that wraps your .NET delegate (or its reference count is incremented if it already exists). - The system calls the underlying JavaScript
addEventListener with the event name and the Callback.
When you use the -= operator:
- The system looks up the existing
Callback associated with your specific delegate. - It calls the underlying JavaScript
removeEventListener. - It decrements the
Callback's reference count, disposing of it if the count reaches zero.
Implementation Pattern
JSObject subclasses typically implement ActionEvent properties like this:
public ActionEvent<Event> OnChange
{
get => new ActionEvent<Event>("change", AddEventListener, RemoveEventListener);
set { }
}
Note: The empty set { } is required to allow the += and -= operators to compile.