Skip to content

Commit

Permalink
fix(translate): memory leak translate directive removing wrong observ…
Browse files Browse the repository at this point in the history
…er 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 <[email protected]>
Co-authored-by: Wasuwat Limsuparhat <[email protected]>
  • Loading branch information
3 people authored Jan 26, 2023
1 parent 592aebc commit 1224bdf
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions packages/translate/src/translate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<BasicElement, ObserverKey>();
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'
Expand Down

0 comments on commit 1224bdf

Please sign in to comment.