mirror of
https://github.com/Stremio/stremio-web.git
synced 2026-04-20 23:12:13 +00:00
fix(GamepadProvider): prevent emitting more than one event handler at a time
This commit is contained in:
parent
9c34a23888
commit
bc81bd1fc8
1 changed files with 10 additions and 2 deletions
|
|
@ -27,12 +27,20 @@ const GamepadProvider: React.FC<{
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const off = useCallback((event: string, id: string) => {
|
const off = useCallback((event: string, id: string) => {
|
||||||
eventHandlers.current.get(event)?.delete(id);
|
const handlersMap = eventHandlers.current.get(event);
|
||||||
|
handlersMap?.delete(id);
|
||||||
|
if (handlersMap?.size === 0) {
|
||||||
|
eventHandlers.current.delete(event);
|
||||||
|
}
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const emit = (event: string, data?: any) => {
|
const emit = (event: string, data?: any) => {
|
||||||
if (eventHandlers.current.has(event)) {
|
if (eventHandlers.current.has(event)) {
|
||||||
eventHandlers.current.get(event)!.forEach((callback) => callback(data));
|
const handlersMap = eventHandlers.current.get(event)!;
|
||||||
|
const latestHandler = Array.from(handlersMap.values()).at(-1);
|
||||||
|
if (latestHandler) {
|
||||||
|
latestHandler(data);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue