Fix types

This commit is contained in:
Andreas Hocevar
2020-03-10 14:20:12 +01:00
parent e099257461
commit 4fa454f2b3
3 changed files with 19 additions and 14 deletions
+1 -1
View File
@@ -51,7 +51,7 @@ class Observable extends EventTarget {
/** /**
* Listen for a certain type of event. * Listen for a certain type of event.
* @param {string|Array<string>} type The event type or array of event types. * @param {string|Array<string>} type The event type or array of event types.
* @param {function(?): ?} listener The listener function. * @param {import("./events.js").ListenerFunction} listener The listener function.
* @return {import("./events.js").EventsKey|Array<import("./events.js").EventsKey>} Unique key for the listener. If * @return {import("./events.js").EventsKey|Array<import("./events.js").EventsKey>} Unique key for the listener. If
* called with an array of event types as the first argument, the return * called with an array of event types as the first argument, the return
* will be an array of keys. * will be an array of keys.
+9 -2
View File
@@ -13,15 +13,22 @@ import {clear} from './obj.js';
* @api * @api
*/ */
/** /**
* Listener function. This function is called with an event object as argument. * Listener function. This function is called with an event object as argument.
* When the function returns `false`, event propagation will stop. * When the function returns `false`, event propagation will stop.
* *
* @typedef {EventListener|function((Event|import("./events/Event.js").default)): (void|boolean)} ListenerFunction * @typedef {function((Event|import("./events/Event.js").default)): (void|boolean)} ListenerFunction
* @api * @api
*/ */
/**
* @typedef {Object} ListenerObject
* @property {ListenerFunction} handleEvent
*/
/**
* @typedef {ListenerFunction|ListenerObject} Listener
*/
/** /**
* Registers an event listener on an event target. Inspired by * Registers an event listener on an event target. Inspired by
+9 -11
View File
@@ -56,7 +56,7 @@ class Target extends Disposable {
/** /**
* @private * @private
* @type {!Object<string, Array<import("../events.js").ListenerFunction>>} * @type {!Object<string, Array<import("../events.js").Listener>>}
*/ */
this.listeners_ = {}; this.listeners_ = {};
@@ -64,7 +64,7 @@ class Target extends Disposable {
/** /**
* @param {string} type Type. * @param {string} type Type.
* @param {import("../events.js").ListenerFunction} listener Listener. * @param {import("../events.js").Listener} listener Listener.
*/ */
addEventListener(type, listener) { addEventListener(type, listener) {
if (!type || !listener) { if (!type || !listener) {
@@ -85,15 +85,13 @@ class Target extends Disposable {
* of this type. The event parameter can either be a string or an * of this type. The event parameter can either be a string or an
* Object with a `type` property. * Object with a `type` property.
* *
* @param {{type: string, * @param {import("./Event.js").default|string} event Event object.
* target: (EventTargetLike|undefined),
* propagationStopped: (boolean|undefined)}|
* import("./Event.js").default|string} event Event object.
* @return {boolean|undefined} `false` if anyone called preventDefault on the * @return {boolean|undefined} `false` if anyone called preventDefault on the
* event object or if any of the listeners returned false. * event object or if any of the listeners returned false.
* @api * @api
*/ */
dispatchEvent(event) { dispatchEvent(event) {
/** @type {import("./Event.js").default|Event} */
const evt = typeof event === 'string' ? new Event(event) : event; const evt = typeof event === 'string' ? new Event(event) : event;
const type = evt.type; const type = evt.type;
if (!evt.target) { if (!evt.target) {
@@ -108,10 +106,10 @@ class Target extends Disposable {
} }
++this.dispatching_[type]; ++this.dispatching_[type];
for (let i = 0, ii = listeners.length; i < ii; ++i) { for (let i = 0, ii = listeners.length; i < ii; ++i) {
if (listeners[i].handleEvent) { if ('handleEvent' in listeners[i]) {
propagate = listeners[i].handleEvent(evt); propagate = /** @type {import("../events.js").ListenerObject} */ (listeners[i]).handleEvent(evt);
} else { } else {
propagate = listeners[i].call(this, evt); propagate = /** @type {import("../events.js").ListenerFunction} */ (listeners[i]).call(this, evt);
} }
if (propagate === false || evt.propagationStopped) { if (propagate === false || evt.propagationStopped) {
propagate = false; propagate = false;
@@ -149,7 +147,7 @@ class Target extends Disposable {
* order that they will be called in. * order that they will be called in.
* *
* @param {string} type Type. * @param {string} type Type.
* @return {Array<import("../events.js").ListenerFunction>} Listeners. * @return {Array<import("../events.js").Listener>} Listeners.
*/ */
getListeners(type) { getListeners(type) {
return this.listeners_[type]; return this.listeners_[type];
@@ -168,7 +166,7 @@ class Target extends Disposable {
/** /**
* @param {string} type Type. * @param {string} type Type.
* @param {import("../events.js").ListenerFunction} listener Listener. * @param {import("../events.js").Listener} listener Listener.
*/ */
removeEventListener(type, listener) { removeEventListener(type, listener) {
const listeners = this.listeners_[type]; const listeners = this.listeners_[type];