Skip to content

Commit

Permalink
Restore some Evented backward compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
jfirebaugh committed May 3, 2018
1 parent 535ba4a commit db83b5e
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

### ⚠️ Breaking changes

* `Evented` is no longer publicly exported, and `Evented#fire` and `Evented#listens` are now private. If you are writing a class that needs event emitting functionality, consider using [`EventEmitter`](https://nodejs.org/api/events.html#events_class_eventemitter) or similar libraries instead.
* `Evented#fire` and `Evented#listens` are now marked as private. Though `Evented` is still exported, and `fire` and `listens` are still functional, we encourage you to seek alternatives; a future version may remove their API accessibility or change its behavior. If you are writing a class that needs event emitting functionality, consider using [`EventEmitter`](https://nodejs.org/api/events.html#events_class_eventemitter) or similar libraries instead.
* The `"to-string"` expression operator now converts `null` to an empty string rather than to `"null"`. [#6534](https://github.com/mapbox/mapbox-gl-js/pull/6534)

### ✨ Features and improvements
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import Style from './style/style';
import LngLat from './geo/lng_lat';
import LngLatBounds from './geo/lng_lat_bounds';
import Point from '@mapbox/point-geometry';
import {Evented} from './util/evented';
import config from './util/config';
import {setRTLTextPlugin} from './source/rtl_text_plugin';

Expand All @@ -37,6 +38,7 @@ const exported = {
LngLat,
LngLatBounds,
Point,
Evented,
config,

/**
Expand Down
7 changes: 7 additions & 0 deletions src/util/evented.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ export class Evented {
}

fire(event: Event) {
// Compatibility with (type: string, properties: Object) signature from previous versions.
// See https://github.com/mapbox/mapbox-gl-js/issues/6522,
// https://github.com/mapbox/mapbox-gl-draw/issues/766
if (typeof event === 'string') {
event = new Event(event, arguments[1] || {});
}

const type = event.type;

if (this.listens(type)) {
Expand Down
10 changes: 10 additions & 0 deletions test/unit/util/evented.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,16 @@ test('Evented', (t) => {
t.end();
});

t.test('has backward compatibility for fire(string, object) API', (t) => {
const evented = new Evented();
const listener = t.spy();
evented.on('a', listener);
evented.fire('a', {foo: 'bar'});
t.ok(listener.calledOnce);
t.ok(listener.firstCall.args[0].foo, 'bar');
t.end();
});

t.test('evented parents', (t) => {

t.test('adds parents with "setEventedParent"', (t) => {
Expand Down

0 comments on commit db83b5e

Please sign in to comment.