Skip to content

Commit

Permalink
[#73190] cleanup CollabClient when editor is removed
Browse files Browse the repository at this point in the history
  • Loading branch information
Trzcin committed Feb 18, 2025
1 parent 1758785 commit 29397fe
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
22 changes: 11 additions & 11 deletions src/MystEditor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -209,32 +209,32 @@ export default ({ additionalStyles, id, ...params }, /** @type {HTMLElement} */
}
window.myst_editor[editorId] = {};

// cleanup function
window.myst_editor[editorId].remove = () => {
delete window.myst_editor[editorId];
render(null, target.shadowRoot);
};

const form = target.closest("form");
if (form) {
form.addEventListener("formdata", (e) => e.formData.append(params.name, window.myst_editor[editorId].text));
}

const state = createMystState({ id: editorId, ...params });
window.myst_editor[editorId].state = state;

// cleanup function
function remove() {
state.cleanups.forEach((c) => c());
delete window.myst_editor[editorId];
render(null, target.shadowRoot);
}
window.myst_editor[editorId].remove = remove;
// runs Preact cleanup logic when target is removed
const observer = new MutationObserver((mutations) => {
for (const mutation of mutations) {
if (Array.prototype.some.call(mutation.removedNodes, (n) => n == target)) {
delete window.myst_editor[editorId];
render(null, target.shadowRoot);
remove();
observer.disconnect();
}
}
});
observer.observe(target.parentElement, { childList: true });

const state = createMystState({ id: editorId, ...params });
window.myst_editor[editorId].state = state;

render(
<MystState.Provider value={state}>
<MystEditor />
Expand Down
3 changes: 2 additions & 1 deletion src/mystState.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ export function createMystState(/** @type {typeof defaults} */ opts) {
});
}
const collab = signal(null);
effect(() => {
const collabCleanup = effect(() => {
if (!signalOptions.collaboration.value.enabled) return;
const client = new CollaborationClient(signalOptions.collaboration.value, {
id: signalOptions.id.value,
Expand All @@ -180,6 +180,7 @@ export function createMystState(/** @type {typeof defaults} */ opts) {
},
userSettings: signal(userSettings),
collab,
cleanups: [collabCleanup],
};
}

Expand Down

0 comments on commit 29397fe

Please sign in to comment.