Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix memory leaks and failed cleanups in destroyables Mutation Observer #17687

Merged
merged 6 commits into from
Dec 14, 2024
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 88 additions & 41 deletions src/util/destroyables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import { ErrorPF2e } from "./misc.ts";
class DestroyableManager {
#bodyObserver: MutationObserver;

#appObservers = new Map<Node, MutationObserver>();

#destroyables = new Map<Node, Destroyable[]>();
#appObservers = new Map<Node, MutationObserverContext>();

declare static instance: DestroyableManager;

Expand All @@ -23,62 +21,97 @@ class DestroyableManager {
}

constructor() {
this.#bodyObserver = new MutationObserver(this.#onMutate.bind(this));
this.#bodyObserver = new MutationObserver(this.#onMutateBody.bind(this));
this.#bodyObserver.observe(document.body, DestroyableManager.#OBSERVE_OPTIONS);
}

observe(destroyable: Destroyable): void {
const contentEl =
const destroyableEl =
destroyable instanceof Sortable
? destroyable.el.closest(".app, .application")?.querySelector(".window-content")
: destroyable instanceof TooltipsterTarget
? destroyable.element
: destroyable.DOM.input.closest(".app, .application")?.querySelector(".window-content");
? destroyable.el
: "elementOrigin" in destroyable
? destroyable.elementOrigin()
: destroyable.DOM.input;
const contentEl = destroyableEl?.closest(".app, .application")?.querySelector(".window-content");
if (!contentEl) return console.warn(ErrorPF2e("No application element found").message);

const destroyables = this.#destroyables.get(contentEl) ?? [];
destroyables.push(destroyable);
this.#destroyables.set(contentEl, destroyables);

if (!this.#appObservers.has(contentEl)) {
const observer = new MutationObserver(this.#onMutate.bind(this));
observer.observe(contentEl, DestroyableManager.#OBSERVE_OPTIONS);
this.#appObservers.set(contentEl, observer);
let context = this.#appObservers.get(contentEl);
if (context) {
context.elements.add({ node: destroyableEl, destroyable });
return;
}

context = {
observer: null,
contextKey: contentEl,
elements: new Set([{ node: destroyableEl, destroyable }]),
};
const observer = new MutationObserver(this.#onMutateContent(context));
context.observer = observer;

this.#appObservers.set(contentEl, context);

observer.observe(contentEl, DestroyableManager.#OBSERVE_OPTIONS);
}

/** Destroy destroyable instances in closed applications and replaced window content. */
#onMutate(mutations: MutationRecord[]): void {
#onMutateContent(context: MutationObserverContext): (mutations: MutationRecord[]) => void {
return (mutations: MutationRecord[]) => {
for (const mutation of mutations) {
for (const removedNode of mutation.removedNodes) {
for (const element of context.elements) {
if (removedNode.contains(element.node)) {
element.destroyable.destroy();
context.elements.delete(element);
}
}
if (context.elements.size > 0) {
continue;
}
if (context.observer) {
context.observer.disconnect();
}
this.#appObservers.delete(context.contextKey);
context.observer = null;
return;
}
}
};
}

#onMutateBody(mutations: MutationRecord[]) {
for (const mutation of mutations) {
for (const element of mutation.removedNodes) {
for (const destroyable of this.#destroyables.get(element) ?? []) {
destroyable.destroy();
for (const removedNode of mutation.removedNodes) {
for (const [node, context] of this.#appObservers.entries()) {
if (!removedNode.contains(node)) {
continue;
}
for (const element of context.elements) {
element.destroyable.destroy();
}
if (context.observer) {
context.observer.disconnect();
}
this.#appObservers.delete(node);
context.observer = null;
}
this.#destroyables.delete(element);
this.#appObservers.delete(element);
}
}
}
}

type Destroyable = Tagify<{ id: string; value: string }> | Tagify<Tagify.TagData> | Sortable | TooltipsterTarget;

class TooltipsterTarget {
$element: JQuery;

constructor($element: JQuery) {
this.$element = $element;
}

get element(): HTMLElement {
return this.$element[0];
}
interface MutationObserverContext {
observer: MutationObserver | null;
contextKey: Node;
elements: Set<{ node: Node; destroyable: Destroyable }>;
}

destroy(): void {
this.$element.tooltipster("destroy");
}
interface Tooltipster {
destroy(): void;
elementOrigin(): HTMLElement;
}

type Destroyable = Tagify<{ id: string; value: string }> | Tagify<Tagify.TagData> | Sortable | Tooltipster;

function createSortable(list: HTMLElement, options: Sortable.Options): Sortable {
const sortable = new Sortable(list, options);
DestroyableManager.instance.observe(sortable);
Expand All @@ -87,8 +120,22 @@ function createSortable(list: HTMLElement, options: Sortable.Options): Sortable

function createTooltipster(target: HTMLElement, options: JQueryTooltipster.ITooltipsterOptions): JQuery {
const $element = $(target);
stwlam marked this conversation as resolved.
Show resolved Hide resolved
DestroyableManager.instance.observe(new TooltipsterTarget($element));
return $element.tooltipster(options);
const $tooltipsterEl = $element.tooltipster(options);
// get tooltipster namespace key
const tooltipsterNs: string | undefined = $tooltipsterEl.data("tooltipster-ns")?.[0];
if (!tooltipsterNs) {
console.warn(ErrorPF2e("No tooltipster namespace found").message);
return $tooltipsterEl;
}
// get internal tooltipster instance
const tooltipsterInstance: Destroyable | undefined = $tooltipsterEl.data(tooltipsterNs);
if (!tooltipsterInstance) {
console.warn(ErrorPF2e("No tooltipster instance found").message);
return $tooltipsterEl;
}
// create wrapper of instance and tooltipster element for cleanup after element has been removed from DOM
DestroyableManager.instance.observe(tooltipsterInstance);
return $tooltipsterEl;
stwlam marked this conversation as resolved.
Show resolved Hide resolved
}

export { DestroyableManager, createSortable, createTooltipster };
Loading