function listen(target, type, listener, thisArg, once) {
  if (once) {
    const originalListener = listener;
    /**
     * @param {Event|import('./events/Event.js').default} event The event
     * @return {void|boolean} When the function returns `false`, event propagation will stop.
     * @this {typeof target}
     */
    listener = function (event) {
      target.removeEventListener(type, listener);
      return originalListener.call(thisArg ?? this, event);
    };
  } else if (thisArg && thisArg !== target) {
    listener = listener.bind(thisArg);
  }
  const eventsKey = {
    target: target,
    type: type,
    listener: listener,
  };
  target.addEventListener(type, listener);
  return eventsKey;
}
Registers an event listener on an event target. Inspired by
https://google.github.io/closure-library/api/source/closure/goog/events/events.js.src.html

This function efficiently binds a `listener` to a `this` object, and returns
a key for use with {@link module:ol/events.unlistenByKey}.

@param {import("./events/Target.js").EventTargetLike} target Event target.
@param {string} type Event type.
@param {ListenerFunction} listener Listener.
@param {Object} [thisArg] Object referenced by the `this` keyword in the
    listener. Default is the `target`.
@param {boolean} [once] If true, add the listener as one-off listener.
@return {EventsKey} Unique key for the listener.