Skip to content

Commit

Permalink
Remove the listener when component is unmounted
Browse files Browse the repository at this point in the history
  • Loading branch information
kudlajz committed Sep 17, 2024
1 parent f15d268 commit 8d6df46
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ export function CoverageElement({
}
}

function handleCoverageUpdated({ coverage }: { coverage: Pick<CoverageEntry, 'id'>}) {
if (element.coverage.id === coverage.id) {
loadCoverage();
useEffect(() => {
function handleCoverageUpdated({ coverage }: { coverage: Pick<CoverageEntry, 'id'>}) {
if (element.coverage.id === coverage.id) {
loadCoverage();
}
}
}

useEffect(() => {
EventsEditor.addEventListener(editor, 'coverage-updated', handleCoverageUpdated);
return EventsEditor.addEventListener(editor, 'coverage-updated', handleCoverageUpdated);
}, []);

return (
Expand Down
8 changes: 6 additions & 2 deletions packages/slate-editor/src/modules/events/EventsEditor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type { RemoveListener } from '@prezly/events';
import { Events } from '@prezly/events';
import { noop } from '@technically/lodash';
import { Editor } from 'slate';

import { EVENTS_PROPERTY } from './constants';
Expand All @@ -10,10 +12,12 @@ export abstract class EventsEditor {
editor: Editor,
event: Event,
listener: EditorEventHandlers[Event],
): void {
): RemoveListener {
if (EventsEditor.isEventsEditor(editor)) {
editor[EVENTS_PROPERTY].addEventListener(event, listener);
return editor[EVENTS_PROPERTY].addEventListener(event, listener);
}

return noop;
}

static dispatchEvent<Event extends keyof EditorEventMap>(
Expand Down

0 comments on commit 8d6df46

Please sign in to comment.