Skip to content

Commit

Permalink
fix: EventManager 로직 수정- 이벤트 제거안되는 이슈 해결
Browse files Browse the repository at this point in the history
  • Loading branch information
yeong30 committed Dec 26, 2024
1 parent 9905af4 commit 635d855
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 55 deletions.
70 changes: 17 additions & 53 deletions src/lib/eventManager.js
Original file line number Diff line number Diff line change
@@ -1,65 +1,29 @@
const eventManager = () => {
let events = [];
let rootElement = null;
const events = {};

const createScopedEventHandler = (addEvent) => {
return (e) => {
const target = e.target;
if (target !== addEvent.element) return;
addEvent.handler(e);
};
const scopedEventHandler = (e) => {
const targetgetEvent = events[e.type].get(e.target);
if (targetgetEvent) targetgetEvent(e);
};
const setupEventListeners = (root) => {
rootElement = root;

events.forEach(({ eventType, handler: convertHandler }) => {
rootElement.removeEventListener(eventType, convertHandler);
});

events.forEach((event) => {
rootElement.addEventListener(event.eventType, event.handler);
});
const setupEventListeners = (root) => {
for (const eventType in events) {
root.removeEventListener(eventType, scopedEventHandler);
root.addEventListener(eventType, scopedEventHandler);
}
};

const addEvent = (element, eventType, handler) => {
const isExistingEvent = events.some(
(event) =>
event.element == element &&
event.eventType == eventType &&
event.originalHandler == handler,
);
if (isExistingEvent) return;

const scopedEventHandler = createScopedEventHandler({
element,
eventType,
handler,
});
events.push({
element,
eventType,
handler: scopedEventHandler,
originalHandler: handler,
});
if (!events[eventType]) {
events[eventType] = new WeakMap();
}
events[eventType].set(element, handler);
};
const removeEvent = (element, eventType, handler) => {
const sameEvent = events.filter(
(prevEvent) =>
prevEvent.element === element &&
prevEvent.eventType === eventType &&
prevEvent.originalHandler === handler,
);

sameEvent.forEach(({ eventType, handler: convertHandler }) => {
rootElement.removeEventListener(eventType, convertHandler);
});

events = events.filter(
(prevEvent) =>
prevEvent.element !== element ||
prevEvent.eventType !== eventType ||
prevEvent.originalHandler !== handler,
);
const removeEvent = (element, eventType, handler) => {
if (events[eventType].get(element) === handler) {
events[eventType].delete(element);
}
};

return { setupEventListeners, addEvent, removeEvent };
Expand Down
4 changes: 2 additions & 2 deletions src/stores/globalStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ export const globalStore = createStore(
addPost(state, content) {
const { currentUser, posts } = state;
const newPost = {
id: posts.length > 0 ? posts[posts.length - 1].id + 1 : 1,
author: currentUser.username,
time: Date.now(),
content,
id: posts.length > 0 ? posts[posts.length - 1].id + 1 : 1,
likeUsers: [],
time: Date.now(),
};
return { ...state, posts: [...posts, newPost] };
},
Expand Down

0 comments on commit 635d855

Please sign in to comment.