UNPKG

@miyauci/get-event-listeners

Version:
68 lines (67 loc) 2.43 kB
// Copyright © 2023 Tomoki Miyauchi. All rights reserved. MIT license. // This module is browser compatible. import * as dntShim from "./_dnt.shims.js"; import { compositeKey, isBoolean, isFunction, isObject } from "./deps.js"; /** To flatten {@linkcode options}. * @see https://dom.spec.whatwg.org/#concept-flatten-options */ export function flatOptions(options) { if (isBoolean(options)) return options; return Boolean(options?.capture); } /** To flatten {@linkcode options} more. * @see https://dom.spec.whatwg.org/#event-flatten-more */ export function flatOptionsMore(options) { const capture = flatOptions(options); const { once = false, passive = null, signal = null } = isObject(options) ? options : {}; return { capture, once, passive, signal }; } /** Return default passive value. * @see https://dom.spec.whatwg.org/#default-passive-value */ export function defaultPassiveValue(type, target) { if (!touchOrUiEvents.has(type)) return false; if (isWindow(target)) return true; if (!isNodeLike(target)) return false; // target is Document if (!target.ownerDocument) { return target === Reflect.get(dntShim.dntGlobalThis, "document"); } return ("documentElement" in target.ownerDocument && target.ownerDocument.documentElement === target || // target is HTML document "body" in target.ownerDocument && target.ownerDocument.body === target); // target is body } const touchOrUiEvents = /*@__PURE__*/ new Set([ "touchstart", "touchmove", "wheel", "mousewheel", ]); /** Whether the {@linkcode input} is {@linkcode NodeLike} or not. */ export function isNodeLike(input) { return "ownerDocument" in input && typeof input.ownerDocument === "object"; } /** Whether the {@linkcode input} is {@linkcode Window} or not. * This works universally. */ export function isWindow(input) { return globalThis.Window && input instanceof globalThis.Window; } /** {@linkcode listener} to Function. * if {@linkcode listener} is function, return it; otherwise return bound `handleEvent`. */ export function toHandler(listener) { return isFunction(listener) ? listener : listener.handleEvent.bind(listener); } /** Return {@linkcode listener} representation key. */ export function toKey(listener) { return compositeKey(listener.callback, listener.type, listener.capture); }