Skip to content

Commit

Permalink
Type evented.js
Browse files Browse the repository at this point in the history
  • Loading branch information
jfirebaugh committed Jul 10, 2017
1 parent b1f16c0 commit 490f0b1
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions src/util/evented.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
// @flow

const util = require('./util');

function _addEventListener(type, listener, listenerList) {
type Listener = (Object) => any;
type Listeners = { [string]: Array<Listener> };

function _addEventListener(type: string, listener: Listener, listenerList: Listeners) {
listenerList[type] = listenerList[type] || [];
listenerList[type].push(listener);
}

function _removeEventListener(type, listener, listenerList) {
function _removeEventListener(type: string, listener: Listener, listenerList: Listeners) {
if (listenerList && listenerList[type]) {
const index = listenerList[type].indexOf(listener);
if (index !== -1) {
Expand All @@ -21,6 +25,10 @@ function _removeEventListener(type, listener, listenerList) {
* @mixin Evented
*/
class Evented {
_listeners: Listeners;
_oneTimeListeners: Listeners;
_eventedParent: ?Evented;
_eventedParentData: ?(Object | () => Object);

/**
* Adds a listener to a specified event type.
Expand All @@ -31,7 +39,7 @@ class Evented {
* extended with `target` and `type` properties.
* @returns {Object} `this`
*/
on(type, listener) {
on(type: string, listener: Listener) {
this._listeners = this._listeners || {};
_addEventListener(type, listener, this._listeners);

Expand All @@ -45,7 +53,7 @@ class Evented {
* @param {Function} listener The listener function to remove.
* @returns {Object} `this`
*/
off(type, listener) {
off(type: string, listener: Listener) {
_removeEventListener(type, listener, this._listeners);
_removeEventListener(type, listener, this._oneTimeListeners);

Expand All @@ -61,7 +69,7 @@ class Evented {
* @param {Function} listener The function to be called when the event is fired the first time.
* @returns {Object} `this`
*/
once(type, listener) {
once(type: string, listener: Listener) {
this._oneTimeListeners = this._oneTimeListeners || {};
_addEventListener(type, listener, this._oneTimeListeners);

Expand All @@ -75,7 +83,7 @@ class Evented {
* @param {Object} [data] Data to be passed to any listeners.
* @returns {Object} `this`
*/
fire(type, data) {
fire(type: string, data: Object) {
if (this.listens(type)) {
data = util.extend({}, data, {type: type, target: this});

Expand Down Expand Up @@ -112,7 +120,7 @@ class Evented {
* @param {string} type The event type
* @returns {boolean} `true` if there is at least one registered listener for specified event type, `false` otherwise
*/
listens(type) {
listens(type: string) {
return (
(this._listeners && this._listeners[type] && this._listeners[type].length > 0) ||
(this._oneTimeListeners && this._oneTimeListeners[type] && this._oneTimeListeners[type].length > 0) ||
Expand All @@ -124,11 +132,9 @@ class Evented {
* Bubble all events fired by this instance of Evented to this parent instance of Evented.
*
* @private
* @param {parent}
* @param {data}
* @returns {Object} `this`
*/
setEventedParent(parent, data) {
setEventedParent(parent: ?Evented, data?: Object | () => Object) {
this._eventedParent = parent;
this._eventedParentData = data;

Expand Down

0 comments on commit 490f0b1

Please sign in to comment.