Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New waypoint created by dragging is always inserted at first postion in waypoint array #200

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Next Next commit
Add off method to unsubscribe events
Fixes #193
Inspired by tristen commit ref #40
patrix committed Oct 24, 2018
commit 329d51a68f0527ff9652fffdb27d7e04aa8295ae
22 changes: 21 additions & 1 deletion API.md
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@
- [getWaypoints](#getwaypoints)
- [removeRoutes](#removeroutes)
- [on](#on)
- [off](#off)

## MapboxDirections

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

**Parameters**

- `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'`
- `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'`
- **loading** `{ type: } Type is one of 'origin' or 'destination'`
- **profile** `{ profile } Profile is one of 'driving', 'walking', or 'cycling'`
- **origin** `{ feature } Fired when origin is set`
@@ -178,3 +180,21 @@ Subscribe to events that happen within the plugin.
- `fn` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** function that's called when the event is emitted.

Returns **[MapboxDirections](#mapboxdirections)** this;

### off

Unsubscribe to events previously registered.

**Parameters**

- `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'`
- **loading** `{ type: } Type is one of 'origin' or 'destination'`
- **profile** `{ profile } Profile is one of 'driving', 'walking', or 'cycling'`
- **origin** `{ feature } Fired when origin is set`
- **destination** `{ feature } Fired when destination is set`
- **route** `{ route } Fired when a route is updated`
- **error** \`{ error } Error as string
- `fn` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** function used when the event was registered.

Returns **[MapboxDirections](#mapboxdirections)** this;
28 changes: 28 additions & 0 deletions src/actions/index.js
Original file line number Diff line number Diff line change
@@ -315,6 +315,34 @@ export function eventSubscribe(type, fn) {
};
}

export function eventUnsubscribe(type, fn) {
return (dispatch, getState) => {
const { events } = getState();

if (!events[type]) {
return {
type: types.EVENTS,
events
};
}

if (fn) {
// Drop the event if it exists in the events object
const fnToDelete = events[type].indexOf(fn);
if (fnToDelete >= 0) {
events[type].splice(fnToDelete, 1);
}
} else {
delete events[type];
}

return {
type: types.EVENTS,
events: events
};
};
}

export function eventEmit(type, data) {
return (dispatch, getState) => {
const { events } = getState();
11 changes: 11 additions & 0 deletions src/directions.js
Original file line number Diff line number Diff line change
@@ -551,4 +551,15 @@ export default class MapboxDirections {
this.actions.eventSubscribe(type, fn);
return this;
}

/**
* Unsubscribe to events
* @param {String} type name of event. Available events are outlined in `on`
* @param {Function} fn optional. The function that's called when the event is emitted.
* @returns {Directions} this;
*/
off(type, fn) {
this.actions.eventUnsubscribe(type, fn);
return this;
}
}