|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2023 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +/** |
| 8 | + * A symbol used to access dispatch hooks on an event. |
| 9 | + */ |
| 10 | +const dispatchHooks = Symbol('dispatchHooks'); |
| 11 | + |
| 12 | +/** |
| 13 | + * An `Event` with additional symbols for dispatch hooks. |
| 14 | + */ |
| 15 | +interface EventWithDispatchHooks extends Event { |
| 16 | + [dispatchHooks]: EventTarget; |
| 17 | +} |
| 18 | + |
| 19 | +/** |
| 20 | + * Add a hook for an event that is called after the event is dispatched and |
| 21 | + * propagates to other event listeners. |
| 22 | + * |
| 23 | + * This is useful for behaviors that need to check if an event is canceled. |
| 24 | + * |
| 25 | + * The callback is invoked synchronously, which allows for better integration |
| 26 | + * with synchronous platform APIs (like `<form>` or `<label>` clicking). |
| 27 | + * |
| 28 | + * Note: `setupDispatchHooks()` must be called on the element before adding any |
| 29 | + * other event listeners. Call it in the constructor of an element or |
| 30 | + * controller. |
| 31 | + * |
| 32 | + * @example |
| 33 | + * ```ts |
| 34 | + * class MyControl extends LitElement { |
| 35 | + * constructor() { |
| 36 | + * super(); |
| 37 | + * setupDispatchHooks(this, 'click'); |
| 38 | + * this.addEventListener('click', event => { |
| 39 | + * afterDispatch(event, () => { |
| 40 | + * if (event.defaultPrevented) { |
| 41 | + * return |
| 42 | + * } |
| 43 | + * |
| 44 | + * // ... perform logic |
| 45 | + * }); |
| 46 | + * }); |
| 47 | + * } |
| 48 | + * } |
| 49 | + * ``` |
| 50 | + * |
| 51 | + * @example |
| 52 | + * ```ts |
| 53 | + * class MyController implements ReactiveController { |
| 54 | + * constructor(host: ReactiveElement) { |
| 55 | + * // setupDispatchHooks() may be called multiple times for the same |
| 56 | + * // element and events, making it safe for multiple controllers to use it. |
| 57 | + * setupDispatchHooks(host, 'click'); |
| 58 | + * host.addEventListener('click', event => { |
| 59 | + * afterDispatch(event, () => { |
| 60 | + * if (event.defaultPrevented) { |
| 61 | + * return; |
| 62 | + * } |
| 63 | + * |
| 64 | + * // ... perform logic |
| 65 | + * }); |
| 66 | + * }); |
| 67 | + * } |
| 68 | + * } |
| 69 | + * ``` |
| 70 | + * |
| 71 | + * @param event The event to add a hook to. |
| 72 | + * @param callback A hook that is called after the event finishes dispatching. |
| 73 | + */ |
| 74 | +export function afterDispatch(event: Event, callback: () => void) { |
| 75 | + const hooks = (event as EventWithDispatchHooks)[dispatchHooks]; |
| 76 | + if (!hooks) { |
| 77 | + throw new Error(`'${event.type}' event needs setupDispatchHooks().`); |
| 78 | + } |
| 79 | + |
| 80 | + hooks.addEventListener('after', callback); |
| 81 | +} |
| 82 | + |
| 83 | +/** |
| 84 | + * A lookup map of elements and event types that have a dispatch hook listener |
| 85 | + * set up. Used to ensure we don't set up multiple hook listeners on the same |
| 86 | + * element for the same event. |
| 87 | + */ |
| 88 | +const ELEMENT_DISPATCH_HOOK_TYPES = new WeakMap<Element, Set<string>>(); |
| 89 | + |
| 90 | +/** |
| 91 | + * Sets up an element to add dispatch hooks to given event types. This must be |
| 92 | + * called before adding any event listeners that need to use dispatch hooks |
| 93 | + * like `afterDispatch()`. |
| 94 | + * |
| 95 | + * This function is safe to call multiple times with the same element or event |
| 96 | + * types. Call it in the constructor of elements, mixins, and controllers to |
| 97 | + * ensure it is set up before external listeners. |
| 98 | + * |
| 99 | + * @example |
| 100 | + * ```ts |
| 101 | + * class MyControl extends LitElement { |
| 102 | + * constructor() { |
| 103 | + * super(); |
| 104 | + * setupDispatchHooks(this, 'click'); |
| 105 | + * this.addEventListener('click', this.listenerUsingAfterDispatch); |
| 106 | + * } |
| 107 | + * } |
| 108 | + * ``` |
| 109 | + * |
| 110 | + * @param element The element to set up event dispatch hooks for. |
| 111 | + * @param eventTypes The event types to add dispatch hooks to. |
| 112 | + */ |
| 113 | +export function setupDispatchHooks( |
| 114 | + element: Element, |
| 115 | + ...eventTypes: [string, ...string[]] |
| 116 | +) { |
| 117 | + let typesAlreadySetUp = ELEMENT_DISPATCH_HOOK_TYPES.get(element); |
| 118 | + if (!typesAlreadySetUp) { |
| 119 | + typesAlreadySetUp = new Set(); |
| 120 | + ELEMENT_DISPATCH_HOOK_TYPES.set(element, typesAlreadySetUp); |
| 121 | + } |
| 122 | + |
| 123 | + for (const eventType of eventTypes) { |
| 124 | + // Don't register multiple dispatch hook listeners. A second registration |
| 125 | + // would lead to the second listener re-dispatching a re-dispatched event, |
| 126 | + // which can cause an infinite loop inside the other one. |
| 127 | + if (typesAlreadySetUp.has(eventType)) { |
| 128 | + continue; |
| 129 | + } |
| 130 | + |
| 131 | + // When we re-dispatch the event, it's going to immediately trigger this |
| 132 | + // listener again. Use a flag to ignore it. |
| 133 | + let isRedispatching = false; |
| 134 | + element.addEventListener( |
| 135 | + eventType, |
| 136 | + (event: Event) => { |
| 137 | + if (isRedispatching) { |
| 138 | + return; |
| 139 | + } |
| 140 | + |
| 141 | + // Do not let the event propagate to any other listener (not just |
| 142 | + // bubbling listeners with `stopPropagation()`). |
| 143 | + event.stopImmediatePropagation(); |
| 144 | + // Make a copy. |
| 145 | + const eventCopy = Reflect.construct(event.constructor, [ |
| 146 | + event.type, |
| 147 | + event, |
| 148 | + ]); |
| 149 | + |
| 150 | + // Add hooks onto the event. |
| 151 | + const hooks = new EventTarget(); |
| 152 | + (eventCopy as EventWithDispatchHooks)[dispatchHooks] = hooks; |
| 153 | + |
| 154 | + // Re-dispatch the event. We can't reuse `redispatchEvent()` since we |
| 155 | + // need to add the hooks to the copy before it's dispatched. |
| 156 | + isRedispatching = true; |
| 157 | + const dispatched = element.dispatchEvent(eventCopy); |
| 158 | + isRedispatching = false; |
| 159 | + if (!dispatched) { |
| 160 | + event.preventDefault(); |
| 161 | + } |
| 162 | + |
| 163 | + // Synchronously call afterDispatch() hooks. |
| 164 | + hooks.dispatchEvent(new Event('after')); |
| 165 | + }, |
| 166 | + { |
| 167 | + // Ensure this listener runs before other listeners. |
| 168 | + // `setupDispatchHooks()` should be called in constructors to also |
| 169 | + // ensure they run before any other externally-added capture listeners. |
| 170 | + capture: true, |
| 171 | + }, |
| 172 | + ); |
| 173 | + |
| 174 | + typesAlreadySetUp.add(eventType); |
| 175 | + } |
| 176 | +} |
0 commit comments