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

Restore some Evented backward compatibility #6604

Merged
merged 1 commit into from
May 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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