From 1224bdf54f480df1a8960b4bd12a58961ad44bd9 Mon Sep 17 00:00:00 2001 From: Sarin-Udompanish <86759822+Sarin-Udompanish@users.noreply.github.com> Date: Thu, 26 Jan 2023 17:38:07 +0700 Subject: [PATCH] fix(translate): memory leak translate directive removing wrong observer key (#577) * fix: change key to WeakMap to prevent key be replaced when a new element created * fix: change WeakMap generic type * fix: change weakMap to Map * fix: add default value fro key Co-authored-by: Sarin Udompanish Co-authored-by: Wasuwat Limsuparhat <86233706+wsuwt@users.noreply.github.com> --- packages/translate/src/translate.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/translate/src/translate.ts b/packages/translate/src/translate.ts index 303d2160f0..095093a870 100644 --- a/packages/translate/src/translate.ts +++ b/packages/translate/src/translate.ts @@ -154,18 +154,19 @@ const translate = function (options?: string | DecoratorOptions): TranslateFunct // Cannot use an element itself as a key. // Element may have multiple translate directives with different scope // Therefore we need a truly unique key - let key: ObserverKey; + const keys = new Map(); const connectedCallback = prototype.connectedCallback; prototype.connectedCallback = function (): void { connectedCallback.call(this); - key = observeTranslations.call(this, scope); + keys.set(this, observeTranslations.call(this, scope)); }; const disconnectedCallback = prototype.disconnectedCallback; prototype.disconnectedCallback = function (): void { disconnectedCallback.call(this); - disconnectTranslations.call(this, key); + disconnectTranslations.call(this, keys.get(this) || ''); + keys.delete(this); }; const descriptor = mode === 'promise'