Use generics to limit event types in on(), once() and un()

This commit is contained in:
Andreas Hocevar
2021-06-20 20:22:53 +02:00
parent cf7213c5cb
commit 018ad97f25
30 changed files with 49 additions and 7 deletions

View File

@@ -59,7 +59,7 @@ export class CollectionEvent extends Event {
* Collection as a whole. * Collection as a whole.
* *
* @fires CollectionEvent * @fires CollectionEvent
* * @extends BaseObject<'add'|'remove'|'change:length'>
* @template T * @template T
* @api * @api
*/ */

View File

@@ -58,6 +58,7 @@ import {listen, unlistenByKey} from './events.js';
* *
* @api * @api
* @template {import("./geom/Geometry.js").default} Geometry * @template {import("./geom/Geometry.js").default} Geometry
* @extends BaseObject<'change:geometry'>
*/ */
class Feature extends BaseObject { class Feature extends BaseObject {
/** /**

View File

@@ -82,6 +82,7 @@ class GeolocationError extends BaseEvent {
* }); * });
* *
* @fires module:ol/events/Event~BaseEvent#event:error * @fires module:ol/events/Event~BaseEvent#event:error
* @extends BaseObject<'change:accuracy'|'change:accuracyGeometry'|'change:altitude'|'change:altitudeAccuracy'|'change:heading'|'change:position'|'change:projection'|'change:speed'|'change:tracking'|'change:trackingOptions'>
* @api * @api
*/ */
class Geolocation extends BaseObject { class Geolocation extends BaseObject {

View File

@@ -78,6 +78,8 @@ export class ObjectEvent extends Event {
* object.unset('foo'). * object.unset('foo').
* *
* @fires ObjectEvent * @fires ObjectEvent
* @template {string} EventTypes
* @extends Observable<'propertychange'|EventTypes>
* @api * @api
*/ */
class BaseObject extends Observable { class BaseObject extends Observable {

View File

@@ -5,6 +5,11 @@ import EventTarget from './events/Target.js';
import EventType from './events/EventType.js'; import EventType from './events/EventType.js';
import {listen, listenOnce, unlistenByKey} from './events.js'; import {listen, listenOnce, unlistenByKey} from './events.js';
/**
* @template ChildTypes
* @typedef {ChildTypes|'change'|'error'|Array<ChildTypes|'change'|'error'>} EventTypes
*/
/** /**
* @classdesc * @classdesc
* Abstract base class; normally only used for creating subclasses and not * Abstract base class; normally only used for creating subclasses and not
@@ -14,6 +19,7 @@ import {listen, listenOnce, unlistenByKey} from './events.js';
* {@link module:ol/Observable~Observable#changed}. * {@link module:ol/Observable~Observable#changed}.
* *
* @fires import("./events/Event.js").default * @fires import("./events/Event.js").default
* @template {string} ChildTypes
* @api * @api
*/ */
class Observable extends EventTarget { class Observable extends EventTarget {
@@ -48,7 +54,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 {EventTypes<ChildTypes>} type The event type or array of event types.
* @param {function(?): ?} listener The listener function. * @param {function(?): ?} 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
@@ -70,7 +76,7 @@ class Observable extends EventTarget {
/** /**
* Listen once for a certain type of event. * Listen once for a certain type of event.
* @param {string|Array<string>} type The event type or array of event types. * @param {EventTypes<ChildTypes>} type The event type or array of event types.
* @param {function(?): ?} listener The listener function. * @param {function(?): ?} 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
@@ -94,7 +100,7 @@ class Observable extends EventTarget {
/** /**
* Unlisten for a certain type of event. * Unlisten for a certain type of event.
* @param {string|Array<string>} type The event type or array of event types. * @param {EventTypes<ChildTypes>} type The event type or array of event types.
* @param {function(?): ?} listener The listener function. * @param {function(?): ?} listener The listener function.
* @api * @api
*/ */

View File

@@ -101,6 +101,7 @@ const Property = {
* popup.setPosition(coordinate); * popup.setPosition(coordinate);
* map.addOverlay(popup); * map.addOverlay(popup);
* *
* @extends BaseObject<'change:element'|'change:map'|'change:offset'|'change:position'|'change:positioning'>
* @api * @api
*/ */
class Overlay extends BaseObject { class Overlay extends BaseObject {

View File

@@ -132,6 +132,7 @@ import {removeNode} from './dom.js';
* @fires import("./render/Event.js").default#precompose * @fires import("./render/Event.js").default#precompose
* @fires import("./render/Event.js").default#postcompose * @fires import("./render/Event.js").default#postcompose
* @fires import("./render/Event.js").default#rendercomplete * @fires import("./render/Event.js").default#rendercomplete
* @extends BaseObject<'change:layergroup'|'change:size'|'change:target'|'change:view'|'precompose'|'singleclick'|'click'|'dblclick'|'pointerdrag'|'pointermove'|'postrender'|'movestart'|'moveend'|'postcompose'|'rendercomplete'>
* @api * @api
*/ */
class PluggableMap extends BaseObject { class PluggableMap extends BaseObject {

View File

@@ -288,6 +288,7 @@ const DEFAULT_MIN_ZOOM = 0;
* the snap angle is 0), an animation will be triggered at the interaction end to * the snap angle is 0), an animation will be triggered at the interaction end to
* put back the view to a stable state; * put back the view to a stable state;
* *
* @extends BaseObject<'change:center'|'change:resolution'|'change:rotation'>
* @api * @api
*/ */
class View extends BaseObject { class View extends BaseObject {

View File

@@ -41,6 +41,8 @@ import {removeNode} from '../dom.js';
* You can also extend this base for your own control class. See * You can also extend this base for your own control class. See
* examples/custom-controls for an example of how to do this. * examples/custom-controls for an example of how to do this.
* *
* @template {string} EventTypes
* @extends BaseObject<EventTypes>
* @api * @api
*/ */
class Control extends BaseObject { class Control extends BaseObject {

View File

@@ -66,6 +66,7 @@ const FullScreenEventType = {
* *
* @fires FullScreenEventType#enterfullscreen * @fires FullScreenEventType#enterfullscreen
* @fires FullScreenEventType#leavefullscreen * @fires FullScreenEventType#leavefullscreen
* @extends Control<'enterfullscreen'|'leavefullscreen'>
* @api * @api
*/ */
class FullScreen extends Control { class FullScreen extends Control {

View File

@@ -50,6 +50,7 @@ const COORDINATE_FORMAT = 'coordinateFormat';
* On touch devices, which usually do not have a mouse cursor, the coordinates * On touch devices, which usually do not have a mouse cursor, the coordinates
* of the currently touched position are shown. * of the currently touched position are shown.
* *
* @extends Control<'change:coordinaetFormat'|'change:projection'>
* @api * @api
*/ */
class MousePosition extends Control { class MousePosition extends Control {

View File

@@ -69,6 +69,7 @@ const DEFAULT_DPI = 25.4 / 0.28;
* When specifying `bar` as `true`, a scalebar will be rendered instead * When specifying `bar` as `true`, a scalebar will be rendered instead
* of a scaleline. * of a scaleline.
* *
* @extends Control<'change:units'>
* @api * @api
*/ */
class ScaleLine extends Control { class ScaleLine extends Control {

View File

@@ -82,6 +82,7 @@ export class DragAndDropEvent extends Event {
* combinnation of formats read both text string and ArrayBuffer sources. Older browsers such * combinnation of formats read both text string and ArrayBuffer sources. Older browsers such
* as IE which do not support this will need a TextDecoder polyfill to be loaded before use. * as IE which do not support this will need a TextDecoder polyfill to be loaded before use.
* *
* @extends Interaction<'addfeatures'>
* @api * @api
* *
* @fires DragAndDropEvent * @fires DragAndDropEvent

View File

@@ -103,6 +103,7 @@ export class DragBoxEvent extends Event {
* {@link module:ol/interaction/DragRotateAndZoom}). * {@link module:ol/interaction/DragRotateAndZoom}).
* *
* @fires DragBoxEvent * @fires DragBoxEvent
* @extends PointerInteraction<'boxcancel'|'boxdrag'|'boxend'>
* @api * @api
*/ */
class DragBox extends PointerInteraction { class DragBox extends PointerInteraction {

View File

@@ -178,6 +178,7 @@ export class DrawEvent extends Event {
* Interaction for drawing feature geometries. * Interaction for drawing feature geometries.
* *
* @fires DrawEvent * @fires DrawEvent
* @extends PointerInteraction<'drawabort'|'drawend'|'drawstart'>
* @api * @api
*/ */
class Draw extends PointerInteraction { class Draw extends PointerInteraction {

View File

@@ -81,6 +81,7 @@ export class ExtentEvent extends Event {
* This interaction is only supported for mouse devices. * This interaction is only supported for mouse devices.
* *
* @fires ExtentEvent * @fires ExtentEvent
* @extends PointerInteraction<'extentchanged'>
* @api * @api
*/ */
class Extent extends PointerInteraction { class Extent extends PointerInteraction {

View File

@@ -27,6 +27,9 @@ import {easeOut, linear} from '../easing.js';
* by a keyboard event not a button element event. * by a keyboard event not a button element event.
* Although interactions do not have a DOM element, some of them do render * Although interactions do not have a DOM element, some of them do render
* vectors and so are visible on the screen. * vectors and so are visible on the screen.
*
* @template {string} EventTypes
* @extends BaseObject<EventTypes|'change:active'>
* @api * @api
*/ */
class Interaction extends BaseObject { class Interaction extends BaseObject {

View File

@@ -183,6 +183,7 @@ export class ModifyEvent extends Event {
* key is pressed. To configure the interaction with a different condition * key is pressed. To configure the interaction with a different condition
* for deletion, use the `deleteCondition` option. * for deletion, use the `deleteCondition` option.
* @fires ModifyEvent * @fires ModifyEvent
* @extends PointerInteraction<'modifyend'|'modifystart'>
* @api * @api
*/ */
class Modify extends PointerInteraction { class Modify extends PointerInteraction {

View File

@@ -41,6 +41,9 @@ import {getValues} from '../obj.js';
* started. During a drag sequence the `handleDragEvent` user function is * started. During a drag sequence the `handleDragEvent` user function is
* called on `move` events. The drag sequence ends when the `handleUpEvent` * called on `move` events. The drag sequence ends when the `handleUpEvent`
* user function is called and returns `false`. * user function is called and returns `false`.
*
* @template {string} EventTypes
* @extends Interaction<EventTypes>
* @api * @api
*/ */
class PointerInteraction extends Interaction { class PointerInteraction extends Interaction {

View File

@@ -147,6 +147,7 @@ const originalFeatureStyles = {};
* Selected features are added to an internal unmanaged layer. * Selected features are added to an internal unmanaged layer.
* *
* @fires SelectEvent * @fires SelectEvent
* @extends Interaction<'select'>
* @api * @api
*/ */
class Select extends Interaction { class Select extends Interaction {

View File

@@ -116,6 +116,7 @@ export class TranslateEvent extends Event {
* Interaction for translating (moving) features. * Interaction for translating (moving) features.
* *
* @fires TranslateEvent * @fires TranslateEvent
* @extends PointerInteraction<'translateend'|'translatestart'|'translating'>
* @api * @api
*/ */
class Translate extends PointerInteraction { class Translate extends PointerInteraction {

View File

@@ -38,6 +38,8 @@ import {clamp} from '../math.js';
* the options is set as a {@link module:ol/Object} property on the layer object, so * the options is set as a {@link module:ol/Object} property on the layer object, so
* is observable, and has get/set accessors. * is observable, and has get/set accessors.
* *
* @template {string} EventTypes
* @extends BaseObject<EventTypes|'change:extent'|'change:maxResolution'|'change:maxZoom'|'change:minResolution'|'change:minZoom'|'change:opacity'|'change:visible'|'change:zIndex'>
* @api * @api
*/ */
class BaseLayer extends BaseObject { class BaseLayer extends BaseObject {

View File

@@ -45,7 +45,7 @@ import {assign} from '../obj.js';
* options means that `title` is observable, and has get/set accessors. * options means that `title` is observable, and has get/set accessors.
* *
* @template {import("../source/Tile.js").default} TileSourceType * @template {import("../source/Tile.js").default} TileSourceType
* @extends {Layer<TileSourceType>} * @extends Layer<TileSourceType,'preload'|'useInterimTilesOnError'>
* @api * @api
*/ */
class BaseTileLayer extends Layer { class BaseTileLayer extends Layer {

View File

@@ -73,7 +73,8 @@ const Property = {
* options means that `title` is observable, and has get/set accessors. * options means that `title` is observable, and has get/set accessors.
* *
* @template {import("../source/Vector.js").default|import("../source/VectorTile.js").default} VectorSourceType * @template {import("../source/Vector.js").default|import("../source/VectorTile.js").default} VectorSourceType
* @extends {Layer<VectorSourceType>} * @template {string} EventTypes
* @extends Layer<VectorSourceType,EventTypes>
* @api * @api
*/ */
class BaseVectorLayer extends Layer { class BaseVectorLayer extends Layer {

View File

@@ -85,6 +85,8 @@ import {listen, unlistenByKey} from '../events.js';
* @fires import("../render/Event.js").RenderEvent#postrender * @fires import("../render/Event.js").RenderEvent#postrender
* *
* @template {import("../source/Source.js").default} SourceType * @template {import("../source/Source.js").default} SourceType
* @template {string} EventTypes
* @extends BaseLayer<EventTypes|'postrender'|'prerender'>
* @api * @api
*/ */
class Layer extends BaseLayer { class Layer extends BaseLayer {

View File

@@ -74,7 +74,7 @@ import {assign} from '../obj.js';
* options means that `title` is observable, and has get/set accessors. * options means that `title` is observable, and has get/set accessors.
* *
* @param {Options} [opt_options] Options. * @param {Options} [opt_options] Options.
* @extends {BaseVectorLayer<import("../source/VectorTile.js").default>} * @extends BaseVectorLayer<import("../source/VectorTile.js").default,'change:preload'|'change:useInterimTilesOnError'>
* @api * @api
*/ */
class VectorTileLayer extends BaseVectorLayer { class VectorTileLayer extends BaseVectorLayer {

View File

@@ -76,6 +76,7 @@ export class ImageSourceEvent extends Event {
* Base class for sources providing a single image. * Base class for sources providing a single image.
* @abstract * @abstract
* @fires module:ol/source/Image.ImageSourceEvent * @fires module:ol/source/Image.ImageSourceEvent
* @extends Source<'imageloadend'|'imageloaderror'|'imageloadstart'>
* @api * @api
*/ */
class ImageSource extends Source { class ImageSource extends Source {

View File

@@ -40,6 +40,8 @@ import {get as getProjection} from '../proj.js';
* Base class for {@link module:ol/layer/Layer~Layer} sources. * Base class for {@link module:ol/layer/Layer~Layer} sources.
* *
* A generic `change` event is triggered when the state of the source changes. * A generic `change` event is triggered when the state of the source changes.
* @template {string} EventTypes
* @extends BaseObject<EventTypes>
* @abstract * @abstract
* @api * @api
*/ */

View File

@@ -36,6 +36,8 @@ import {scale as scaleSize, toSize} from '../size.js';
* Abstract base class; normally only used for creating subclasses and not * Abstract base class; normally only used for creating subclasses and not
* instantiated in apps. * instantiated in apps.
* Base class for sources providing images divided into a tile grid. * Base class for sources providing images divided into a tile grid.
* @template {string} EventTypes
* @extends Source<EventTypes>
* @abstract * @abstract
* @api * @api
*/ */

View File

@@ -33,6 +33,7 @@ import {getUid} from '../util.js';
* Base class for sources providing tiles divided into a tile grid over http. * Base class for sources providing tiles divided into a tile grid over http.
* *
* @fires import("./Tile.js").TileSourceEvent * @fires import("./Tile.js").TileSourceEvent
* @extends TileSource<'tileloadend'|'tileloaderror'|'tileloadstart'>
*/ */
class UrlTile extends TileSource { class UrlTile extends TileSource {
/** /**