var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); /** * @module ol/Observable */ import EventTarget from './events/Target.js'; import EventType from './events/EventType.js'; import { listen, listenOnce, unlistenByKey } from './events.js'; /*** * @template {string} Type * @template {Event|import("./events/Event.js").default} EventClass * @template Return * @typedef {(type: Type, listener: (event: EventClass) => ?) => Return} OnSignature */ /*** * @template {string} Type * @template Return * @typedef {(type: Type[], listener: (event: Event|import("./events/Event").default) => ?) => Return extends void ? void : Return[]} CombinedOnSignature */ /** * @typedef {'change'|'error'} EventTypes */ /*** * @template Return * @typedef {OnSignature & CombinedOnSignature} ObservableOnSignature */ /** * @classdesc * Abstract base class; normally only used for creating subclasses and not * instantiated in apps. * An event target providing convenient methods for listener registration * and unregistration. A generic `change` event is always available through * {@link module:ol/Observable~Observable#changed}. * * @fires import("./events/Event.js").default * @api */ var Observable = /** @class */ (function (_super) { __extends(Observable, _super); function Observable() { var _this = _super.call(this) || this; _this.on = /** @type {ObservableOnSignature} */ (_this.onInternal); _this.once = /** @type {ObservableOnSignature} */ (_this.onceInternal); _this.un = /** @type {ObservableOnSignature} */ (_this.unInternal); /** * @private * @type {number} */ _this.revision_ = 0; return _this; } /** * Increases the revision counter and dispatches a 'change' event. * @api */ Observable.prototype.changed = function () { ++this.revision_; this.dispatchEvent(EventType.CHANGE); }; /** * Get the version number for this object. Each time the object is modified, * its version number will be incremented. * @return {number} Revision. * @api */ Observable.prototype.getRevision = function () { return this.revision_; }; /** * @param {string|Array} type Type. * @param {function((Event|import("./events/Event").default)): ?} listener Listener. * @return {import("./events.js").EventsKey|Array} Event key. * @protected */ Observable.prototype.onInternal = function (type, listener) { if (Array.isArray(type)) { var len = type.length; var keys = new Array(len); for (var i = 0; i < len; ++i) { keys[i] = listen(this, type[i], listener); } return keys; } else { return listen(this, /** @type {string} */ (type), listener); } }; /** * @param {string|Array} type Type. * @param {function((Event|import("./events/Event").default)): ?} listener Listener. * @return {import("./events.js").EventsKey|Array} Event key. * @protected */ Observable.prototype.onceInternal = function (type, listener) { var key; if (Array.isArray(type)) { var len = type.length; key = new Array(len); for (var i = 0; i < len; ++i) { key[i] = listenOnce(this, type[i], listener); } } else { key = listenOnce(this, /** @type {string} */ (type), listener); } /** @type {Object} */ (listener).ol_key = key; return key; }; /** * Unlisten for a certain type of event. * @param {string|Array} type Type. * @param {function((Event|import("./events/Event").default)): ?} listener Listener. * @protected */ Observable.prototype.unInternal = function (type, listener) { var key = /** @type {Object} */ (listener).ol_key; if (key) { unByKey(key); } else if (Array.isArray(type)) { for (var i = 0, ii = type.length; i < ii; ++i) { this.removeEventListener(type[i], listener); } } else { this.removeEventListener(type, listener); } }; return Observable; }(EventTarget)); /** * Listen for a certain type of event. * @function * @param {string|Array} type The event type or array of event types. * @param {function((Event|import("./events/Event").default)): ?} listener The listener function. * @return {import("./events.js").EventsKey|Array} Unique key for the listener. If * called with an array of event types as the first argument, the return * will be an array of keys. * @api */ Observable.prototype.on; /** * Listen once for a certain type of event. * @function * @param {string|Array} type The event type or array of event types. * @param {function((Event|import("./events/Event").default)): ?} listener The listener function. * @return {import("./events.js").EventsKey|Array} Unique key for the listener. If * called with an array of event types as the first argument, the return * will be an array of keys. * @api */ Observable.prototype.once; /** * Unlisten for a certain type of event. * @function * @param {string|Array} type The event type or array of event types. * @param {function((Event|import("./events/Event").default)): ?} listener The listener function. * @api */ Observable.prototype.un; /** * Removes an event listener using the key returned by `on()` or `once()`. * @param {import("./events.js").EventsKey|Array} key The key returned by `on()` * or `once()` (or an array of keys). * @api */ export function unByKey(key) { if (Array.isArray(key)) { for (var i = 0, ii = key.length; i < ii; ++i) { unlistenByKey(key[i]); } } else { unlistenByKey(/** @type {import("./events.js").EventsKey} */ (key)); } } export default Observable; //# sourceMappingURL=Observable.js.map