Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LinkPrefetchObserver
: listen for complementary events
Prior to this commit, the `LinkPrefetchObserver` only listened for `mouseleave` events to clear the `PrefetchCache` instance. Not only were `mouseenter` events excluded, but the `mouseleave` event listeners were attached directly to the `<a>` element with a `{ once: true }` option. While unlikely, its was for those event listeners to never be removed if a `mouseleave` were to not fire. Similarly, during `touchstart` events the event listener were added, but never removed since there wasn't a a complementary `mouseleave` event firing to remove it. This commit makes two changes to the event listeners: 1. extract the `addEventListener` calls to a loop, looping over `mouseenter` and `touchstart` event names 2. define complementary events for both `mouseenter` and `touchstart` By moving the cancellation logic out of individual event listeners and into an `this.eventTarget`-wide scope, we limit the risk of leaking listeners. Similarly, we only ever instantiate one per event-pairing. To track the `<a>` element reference, define both a `this.#tryToCancelPrefetchRequest` method and a `this.#linkToPrefetch` property to hold the reference to the `<a>` element in question.
- Loading branch information