Skip to content

Commit 7390b3a

Browse files
committed
Add off method to unsubscribe events
Fixes mapbox#193 Inspired by tristen commit ref mapbox#40
1 parent 07ebea2 commit 7390b3a

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

API.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
- [getWaypoints](#getwaypoints)
1717
- [removeRoutes](#removeroutes)
1818
- [on](#on)
19+
- [off](#off)
1920

2021
## MapboxDirections
2122

@@ -168,7 +169,8 @@ Subscribe to events that happen within the plugin.
168169

169170
**Parameters**
170171

171-
- `type` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** name of event. Available events and the data passed into their respective event objects are:- **clear** `{ type: } Type is one of 'origin' or 'destination'`
172+
- `type` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** name of event. Available events and the data passed into their respective event objects are:
173+
- **clear** `{ type: } Type is one of 'origin' or 'destination'`
172174
- **loading** `{ type: } Type is one of 'origin' or 'destination'`
173175
- **profile** `{ profile } Profile is one of 'driving', 'walking', or 'cycling'`
174176
- **origin** `{ feature } Fired when origin is set`
@@ -178,3 +180,21 @@ Subscribe to events that happen within the plugin.
178180
- `fn` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** function that's called when the event is emitted.
179181

180182
Returns **[MapboxDirections](#mapboxdirections)** this;
183+
184+
### off
185+
186+
Unsubscribe to events previously registered.
187+
188+
**Parameters**
189+
190+
- `type` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** name of event. Available events and the data passed into their respective event objects are:
191+
- **clear** `{ type: } Type is one of 'origin' or 'destination'`
192+
- **loading** `{ type: } Type is one of 'origin' or 'destination'`
193+
- **profile** `{ profile } Profile is one of 'driving', 'walking', or 'cycling'`
194+
- **origin** `{ feature } Fired when origin is set`
195+
- **destination** `{ feature } Fired when destination is set`
196+
- **route** `{ route } Fired when a route is updated`
197+
- **error** \`{ error } Error as string
198+
- `fn` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** function used when the event was registered.
199+
200+
Returns **[MapboxDirections](#mapboxdirections)** this;

src/actions/index.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,34 @@ export function eventSubscribe(type, fn) {
315315
};
316316
}
317317

318+
export function eventUnsubscribe(type, fn) {
319+
return (dispatch, getState) => {
320+
const { events } = getState();
321+
322+
if (!events[type]) {
323+
return {
324+
type: types.EVENTS,
325+
events
326+
};
327+
}
328+
329+
if (fn) {
330+
// Drop the event if it exists in the events object
331+
const fnToDelete = events[type].indexOf(fn);
332+
if (fnToDelete >= 0) {
333+
events[type].splice(fnToDelete, 1);
334+
}
335+
} else {
336+
delete events[type];
337+
}
338+
339+
return {
340+
type: types.EVENTS,
341+
events: events
342+
};
343+
};
344+
}
345+
318346
export function eventEmit(type, data) {
319347
return (dispatch, getState) => {
320348
const { events } = getState();

src/directions.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,4 +551,15 @@ export default class MapboxDirections {
551551
this.actions.eventSubscribe(type, fn);
552552
return this;
553553
}
554+
555+
/**
556+
* Unsubscribe to events
557+
* @param {String} type name of event. Available events are outlined in `on`
558+
* @param {Function} fn optional. The function that's called when the event is emitted.
559+
* @returns {Directions} this;
560+
*/
561+
off(type, fn) {
562+
this.actions.eventUnsubscribe(type, fn);
563+
return this;
564+
}
554565
}

0 commit comments

Comments
 (0)