Skip to content

Commit

Permalink
Merge pull request #417 from sronveaux/hover-controller-with-abort-co…
Browse files Browse the repository at this point in the history
…ntroller

HoverController using AbortController instead of cancelToken
  • Loading branch information
sronveaux authored Sep 24, 2024
2 parents 7564b80 + 7ff8731 commit 55479d1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
24 changes: 12 additions & 12 deletions src/components/ol/HoverController.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default class HoverController {
conf = null;
timerHandle = null;
activeOverlayId = null;
pendingRequestsCancelSrc = null;
pendingRequestsAbortCtrl = null;

/**
* Initializes the map hover functionality:
Expand Down Expand Up @@ -71,9 +71,9 @@ export default class HoverController {
me.timerHandle = null;
}

if (me.pendingRequestsCancelSrc) {
me.pendingRequestsCancelSrc.cancel();
me.pendingRequestsCancelSrc = null;
if (me.pendingRequestsAbortCtrl) {
me.pendingRequestsAbortCtrl.abort();
me.pendingRequestsAbortCtrl = null;
}

if (me.activeOverlayId) {
Expand All @@ -95,16 +95,16 @@ export default class HoverController {
const map = me.map;
const pixel = event.pixel;
const coordinate = event.coordinate;
const cancelToken = axios.CancelToken;
const abortController = new AbortController();
const featureInfos = [];
let resetTooltip = true;

// Cancel pending requests and create a new cancel token source which corresponds
// to all async requests sent in this iteration.
if (me.pendingRequestsCancelSrc) {
me.pendingRequestsCancelSrc.cancel();
if (me.pendingRequestsAbortCtrl) {
me.pendingRequestsAbortCtrl.abort();
}
me.pendingRequestsCancelSrc = cancelToken.source();
me.pendingRequestsAbortCtrl = abortController;

// Acquire features for all layers.
map.getLayers().forEach((layer) => {
Expand All @@ -114,7 +114,7 @@ export default class HoverController {
const source = layer.getSource();
if (source instanceof TileWmsSource || source instanceof ImageWMSSource) {
resetTooltip = false;
me.getWMSFeaturesAsync(map, layer, coordinate, me.pendingRequestsCancelSrc)
me.getWMSFeaturesAsync(map, layer, coordinate, me.pendingRequestsAbortCtrl)
.then(function (features) {
featureInfos.push(...features.map((feat) => {
return { layer, feature: feat };
Expand Down Expand Up @@ -162,10 +162,10 @@ export default class HoverController {
* @param {ol.Map} map OpenLayers map.
* @param {ol.layer.Tile | ol.layer.Image} layer The layer to acquire the features for.
* @param {ol.Coordinate} coordinate The coordinate in map projection.
* @param {axios.CancelTokenSource} cancelTokenSrc An optional cancel token to abort the request.
* @param {AbortController} abortCtrl An optional abort controller to abort the request.
* @returns {Promise<Array<ol.Feature>>}
*/
getWMSFeaturesAsync (map, layer, coordinate, cancelTokenSrc) {
getWMSFeaturesAsync (map, layer, coordinate, abortCtrl) {
const view = map.getView();
return new Promise((resolve, reject) => {
const url = layer.getSource().getFeatureInfoUrl(
Expand All @@ -183,7 +183,7 @@ export default class HoverController {
const request = {
method: 'GET',
url,
cancelToken: cancelTokenSrc?.token
signal: abortCtrl?.signal
};
axios(request)
.then(response => {
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/specs/components/ol/HoverController.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('ol/HoverController.js', () => {
expect(comp.map).to.equal(map);
expect(comp.timerHandle).to.equal(null);
expect(comp.activeOverlayId).to.equal(null);
expect(comp.pendingRequestsCancelSrc).to.equal(null);
expect(comp.pendingRequestsAbortCtrl).to.equal(null);
expect(comp.conf.delay).to.equal(150)
expect(comp.conf.hideOnMousemove).to.equal(false)
expect(comp.conf.hoverOverlay).to.equal('wgu-hover-tooltip')
Expand Down

0 comments on commit 55479d1

Please sign in to comment.