Blazor.DOM
Blazor.DOM copied to clipboard
Implementations of `AddEventListenerAsync`, `RemoveEventListenerAsync`, `AddEventListener`, and `RemoveEventListenerAsync` can be simplified
We currently use a helper method to invoke the addEventListener method in places like this:
public async Task AddEventListenerAsync<TEvent>(string type, EventListener<TEvent>? callback, AddEventListenerOptions? options = null)
where TEvent : Event, IJSCreatable<TEvent>
{
IJSObjectReference helper = await helperTask.Value;
await helper.InvokeVoidAsync("addEventListener", JSReference, type, callback?.JSReference, options);
}
However, that indirection is unnecessary as the addEventListener method could be called directly on the JSReference. It could be changed to:
public async Task AddEventListenerAsync<TEvent>(string type, EventListener<TEvent>? callback, AddEventListenerOptions? options = null)
where TEvent : Event, IJSCreatable<TEvent>
{
await JSReference.InvokeVoidAsync("addEventListener", type, callback?.JSReference, options);
}
It would be valuable to add some unit tests for these parts before making the change to ensure that all the related methods still work after the refactor. Since these methods are also used in derived classes like AbortSignal then it would make sense to add a couple of tests that also encapsulate their behavior for the related methods.