Skip to content

Commit 55479d1

Browse files
authored
Merge pull request #417 from sronveaux/hover-controller-with-abort-controller
HoverController using AbortController instead of cancelToken
2 parents 7564b80 + 7ff8731 commit 55479d1

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

src/components/ol/HoverController.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export default class HoverController {
1818
conf = null;
1919
timerHandle = null;
2020
activeOverlayId = null;
21-
pendingRequestsCancelSrc = null;
21+
pendingRequestsAbortCtrl = null;
2222

2323
/**
2424
* Initializes the map hover functionality:
@@ -71,9 +71,9 @@ export default class HoverController {
7171
me.timerHandle = null;
7272
}
7373

74-
if (me.pendingRequestsCancelSrc) {
75-
me.pendingRequestsCancelSrc.cancel();
76-
me.pendingRequestsCancelSrc = null;
74+
if (me.pendingRequestsAbortCtrl) {
75+
me.pendingRequestsAbortCtrl.abort();
76+
me.pendingRequestsAbortCtrl = null;
7777
}
7878

7979
if (me.activeOverlayId) {
@@ -95,16 +95,16 @@ export default class HoverController {
9595
const map = me.map;
9696
const pixel = event.pixel;
9797
const coordinate = event.coordinate;
98-
const cancelToken = axios.CancelToken;
98+
const abortController = new AbortController();
9999
const featureInfos = [];
100100
let resetTooltip = true;
101101

102102
// Cancel pending requests and create a new cancel token source which corresponds
103103
// to all async requests sent in this iteration.
104-
if (me.pendingRequestsCancelSrc) {
105-
me.pendingRequestsCancelSrc.cancel();
104+
if (me.pendingRequestsAbortCtrl) {
105+
me.pendingRequestsAbortCtrl.abort();
106106
}
107-
me.pendingRequestsCancelSrc = cancelToken.source();
107+
me.pendingRequestsAbortCtrl = abortController;
108108

109109
// Acquire features for all layers.
110110
map.getLayers().forEach((layer) => {
@@ -114,7 +114,7 @@ export default class HoverController {
114114
const source = layer.getSource();
115115
if (source instanceof TileWmsSource || source instanceof ImageWMSSource) {
116116
resetTooltip = false;
117-
me.getWMSFeaturesAsync(map, layer, coordinate, me.pendingRequestsCancelSrc)
117+
me.getWMSFeaturesAsync(map, layer, coordinate, me.pendingRequestsAbortCtrl)
118118
.then(function (features) {
119119
featureInfos.push(...features.map((feat) => {
120120
return { layer, feature: feat };
@@ -162,10 +162,10 @@ export default class HoverController {
162162
* @param {ol.Map} map OpenLayers map.
163163
* @param {ol.layer.Tile | ol.layer.Image} layer The layer to acquire the features for.
164164
* @param {ol.Coordinate} coordinate The coordinate in map projection.
165-
* @param {axios.CancelTokenSource} cancelTokenSrc An optional cancel token to abort the request.
165+
* @param {AbortController} abortCtrl An optional abort controller to abort the request.
166166
* @returns {Promise<Array<ol.Feature>>}
167167
*/
168-
getWMSFeaturesAsync (map, layer, coordinate, cancelTokenSrc) {
168+
getWMSFeaturesAsync (map, layer, coordinate, abortCtrl) {
169169
const view = map.getView();
170170
return new Promise((resolve, reject) => {
171171
const url = layer.getSource().getFeatureInfoUrl(
@@ -183,7 +183,7 @@ export default class HoverController {
183183
const request = {
184184
method: 'GET',
185185
url,
186-
cancelToken: cancelTokenSrc?.token
186+
signal: abortCtrl?.signal
187187
};
188188
axios(request)
189189
.then(response => {

tests/unit/specs/components/ol/HoverController.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ describe('ol/HoverController.js', () => {
4242
expect(comp.map).to.equal(map);
4343
expect(comp.timerHandle).to.equal(null);
4444
expect(comp.activeOverlayId).to.equal(null);
45-
expect(comp.pendingRequestsCancelSrc).to.equal(null);
45+
expect(comp.pendingRequestsAbortCtrl).to.equal(null);
4646
expect(comp.conf.delay).to.equal(150)
4747
expect(comp.conf.hideOnMousemove).to.equal(false)
4848
expect(comp.conf.hoverOverlay).to.equal('wgu-hover-tooltip')

0 commit comments

Comments
 (0)