Skip to content

Commit

Permalink
Preserve event listener ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
smoores-dev committed Nov 17, 2023
1 parent 526548a commit 7462291
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .yarn/versions/d438c5f0.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
releases:
"@nytimes/react-prosemirror": patch
8 changes: 4 additions & 4 deletions src/hooks/useComponentEventListeners.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ import {
*/
export function useComponentEventListeners() {
const [registry, setRegistry] = useState(
new Map<keyof DOMEventMap, Set<EventHandler>>()
new Map<keyof DOMEventMap, Array<EventHandler>>()
);

const registerEventListener = useCallback(
(eventType: keyof DOMEventMap, handler: EventHandler) => {
const handlers = registry.get(eventType) ?? new Set<EventHandler>();
handlers.add(handler);
const handlers = registry.get(eventType) ?? [];
handlers.unshift(handler);
if (!registry.has(eventType)) {
registry.set(eventType, handlers);
setRegistry(new Map(registry));
Expand All @@ -52,7 +52,7 @@ export function useComponentEventListeners() {
const unregisterEventListener = useCallback(
(eventType: keyof DOMEventMap, handler: EventHandler) => {
const handlers = registry.get(eventType);
handlers?.delete(handler);
handlers?.splice(handlers.indexOf(handler), 1);
},
[registry]
);
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/componentEventListeners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export type EventHandler<
) => boolean | void;

export function componentEventListeners(
eventHandlerRegistry: Map<keyof DOMEventMap, Set<EventHandler>>
eventHandlerRegistry: Map<keyof DOMEventMap, Iterable<EventHandler>>
) {
const domEventHandlers: Record<keyof DOMEventMap, EventHandler> = {};

Expand Down

0 comments on commit 7462291

Please sign in to comment.