Skip to content

Commit 40b9ffb

Browse files
authored
clean up GL JS state onRemove (#163)
* clean up GL JS state onRemove * add gl-js state cleanup test and refactor
1 parent 03a58c1 commit 40b9ffb

File tree

2 files changed

+54
-14
lines changed

2 files changed

+54
-14
lines changed

src/directions.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@ export default class MapboxDirections {
109109
this.storeUnsubscribe();
110110
delete this.storeUnsubscribe;
111111
}
112+
directionsStyle.forEach((layer) => {
113+
if (map.getLayer(layer.id)) map.removeLayer(layer.id);
114+
});
115+
116+
if (map.getSource('directions')) map.removeSource('directions');
117+
112118
this._map = null;
113119
return this;
114120
}

test/test.directions.js

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,58 @@
22

33
const test = require('tape');
44
const once = require('lodash.once');
5+
const MapboxDirections = require('..');
56

6-
test('directions', (tt) => {
7-
var container, map, directions;
7+
function setup() {
8+
var container = document.createElement('div');
9+
var map = new mapboxgl.Map({ container: container, style: {
10+
"version": 8,
11+
"sources": {
12+
},
13+
"layers": [
14+
{
15+
"id": "background", "type": "background",
16+
"paint": {
17+
"background-color": "#fff"
18+
}
19+
}] }});
820

9-
function setup(opts) {
10-
container = document.createElement('div');
11-
map = new mapboxgl.Map({ container: container });
12-
const MapboxDirections = require('..');
13-
directions = new MapboxDirections(opts);
14-
map.addControl(directions);
15-
}
21+
return map;
22+
}
1623

24+
test('directions', (tt) => {
1725
tt.test('initialized', t => {
18-
setup();
26+
var map = setup();
27+
var directions = new MapboxDirections();
28+
map.addControl(directions);
29+
1930
t.ok(directions, 'directions is initialized');
2031
t.end();
2132
});
2233

2334
tt.test('set/get inputs', t => {
24-
setup({
35+
var map = setup();
36+
37+
var directions = new MapboxDirections({
2538
geocoder: {
2639
proximity: [-79.45, 43.65]
2740
}
2841
});
42+
map.addControl(directions);
43+
2944

3045
directions.setOrigin('Queen Street NY');
3146
directions.setDestination([-77, 41]);
3247

33-
directions.on('origin', function(e) {
48+
directions.on('origin', (e) => {
3449
t.ok(directions.getOrigin(), 'origin feature is present from get');
3550
t.ok(e.feature, 'origin feature is in the event object');
3651
});
3752

38-
directions.on('destination', once((e) => {
53+
directions.on('destination', (e) => {
3954
t.ok(directions.getDestination(), 'destination feature is present from get');
4055
t.ok(e.feature, 'destination feature is in the event object');
41-
}));
56+
});
4257

4358
directions.on('route', once((e) => {
4459
t.ok(e.route, 'routing data was passed');
@@ -50,3 +65,22 @@ test('directions', (tt) => {
5065
tt.end();
5166
});
5267

68+
test('Directions#onRemove', t => {
69+
var map = setup();
70+
var directions = new MapboxDirections({
71+
geocoder: {
72+
proximity: [-79.45, 43.65]
73+
}
74+
});
75+
map.addControl(directions);
76+
77+
78+
directions.setOrigin('Queen Street NY');
79+
directions.setDestination([-77, 41]);
80+
directions.on('route', once(()=>{
81+
t.true(!!map.getSource('directions'), 'directions source is added');
82+
map.removeControl(directions);
83+
t.false(!!map.getSource('directions'), 'directions source is removed');
84+
t.end();
85+
}));
86+
});

0 commit comments

Comments
 (0)