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(other): fixed memory leak in click outside decorator #453

Merged
merged 1 commit into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-unsafe-argument */
/* eslint-disable @typescript-eslint/no-unsafe-return */
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/unbound-method */
Expand Down Expand Up @@ -31,59 +32,57 @@ export function OnClickOutside(
(BUILD as any).connectedCallback = true;
(BUILD as any).disconnectedCallback = true;

// eslint-disable-next-line
const { connectedCallback, disconnectedCallback } = proto;

const store = new Map<ComponentInterface, EventListener>();

proto.connectedCallback = function () {
const host = getElement(this);
const method = this[methodName];
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
registerOnClickOutside(this, host, method, opt);

registerOnClickOutside(store, this, host, method, opt);

return connectedCallback && connectedCallback.call(this);
};

proto.disconnectedCallback = function () {
const host = getElement(this);
const method = this[methodName];
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
removeOnClickOutside(this, host, method, opt);
removeOnClickOutside(store, this, opt);

return disconnectedCallback && disconnectedCallback.call(this);
};
};
}

export function registerOnClickOutside(
store: Map<ComponentInterface, EventListener>,
component: ComponentInterface,
element: HTMLElement,
callback: OnClickOutsideCallback,
opt: OnClickOutsideOptions = OnClickOutsideOptionsDefaults
): void {
const excludedNodes = getExcludedNodes(opt);
const listener = (e: Event) => {
initOnClickOutside(e, component, element, callback, excludedNodes);
};

store.set(component, listener);

getTriggerEvents(opt).forEach(triggerEvent => {
window.addEventListener(
triggerEvent,
(e: Event) => {
initOnClickOutside(e, component, element, callback, excludedNodes);
},
false
);
window.addEventListener(triggerEvent, listener, false);
});
}

export function removeOnClickOutside(
store: Map<ComponentInterface, EventListener>,
component: ComponentInterface,
element: HTMLElement,
callback: OnClickOutsideCallback,
opt: OnClickOutsideOptions = OnClickOutsideOptionsDefaults
): void {
const listener = store.get(component);

store.delete(component);

getTriggerEvents(opt).forEach(triggerEvent => {
window.removeEventListener(
triggerEvent,
(e: Event) => {
initOnClickOutside(e, component, element, callback);
},
false
);
window.removeEventListener(triggerEvent, listener, false);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function OnMutation(options: MutationObserverInit): OnMutationDecorator {

const { connectedCallback, disconnectedCallback } = proto;

const store = new Map<unknown, MutationObserver>();
const store = new Map<ComponentInterface, MutationObserver>();

proto.connectedCallback = function () {
const method = this[methodName];
Expand All @@ -41,8 +41,8 @@ export function OnMutation(options: MutationObserverInit): OnMutationDecorator {
}

function registerObserver(
store: Map<unknown, MutationObserver>,
key: unknown,
store: Map<ComponentInterface, MutationObserver>,
key: ComponentInterface,
observer: MutationObserver,
options: MutationObserverInit
) {
Expand All @@ -56,8 +56,8 @@ function registerObserver(
}

function deregisterObserver(
store: Map<unknown, MutationObserver>,
key: unknown
store: Map<ComponentInterface, MutationObserver>,
key: ComponentInterface
) {
if (store.has(key)) {
store.get(key).disconnect();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function OnResize(): OnResizeDecorator {

const { connectedCallback, disconnectedCallback } = proto;

const store = new Map<unknown, ResizeObserver>();
const store = new Map<ComponentInterface, ResizeObserver>();

proto.connectedCallback = function () {
const method = this[methodName];
Expand All @@ -41,8 +41,8 @@ export function OnResize(): OnResizeDecorator {
}

function registerObserver(
store: Map<unknown, ResizeObserver>,
key: unknown,
store: Map<ComponentInterface, ResizeObserver>,
key: ComponentInterface,
observer: ResizeObserver
) {
if (store.has(key)) {
Expand All @@ -54,7 +54,10 @@ function registerObserver(
observer.observe(getElement(key));
}

function deregisterObserver(store: Map<unknown, ResizeObserver>, key: unknown) {
function deregisterObserver(
store: Map<ComponentInterface, ResizeObserver>,
key: ComponentInterface
) {
if (store.has(key)) {
store.get(key).disconnect();
}
Expand Down
Loading