Skip to content

Commit

Permalink
Proof of concept fix memory leak ember-intl#1848
Browse files Browse the repository at this point in the history
  • Loading branch information
johanrd committed Mar 10, 2024
1 parent 3eb8030 commit 665f404
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions packages/ember-intl/addon/helpers/t.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getOwner } from '@ember/application';
import Helper from '@ember/component/helper';
import { inject as service } from '@ember/service';
import { isEmpty } from '@ember/utils';

import type IntlService from '../services/intl';
Expand All @@ -17,16 +17,20 @@ interface TSignature {
}

export default class THelper extends Helper<TSignature> {
@service declare intl: IntlService;

allowEmpty = false;
intl?: IntlService;
unsubscribeLocaleChanged?: () => void;

constructor() {
// eslint-disable-next-line prefer-rest-params
super(...arguments);

this.intl = getOwner(this).lookup('service:intl');

// @ts-expect-error: Property 'onLocaleChanged' is private and only accessible within class 'IntlService'.
this.intl.onLocaleChanged(this.recompute, this);
this.unsubscribeLocaleChanged = this.intl.onLocaleChanged(
this.recompute,
this,
);
}

compute(
Expand All @@ -49,4 +53,9 @@ export default class THelper extends Helper<TSignature> {

return this.intl.t(value!, options) as unknown as string;
}

willDestroy() {
super.willDestroy();
this.unsubscribeLocaleChanged();
}
}

0 comments on commit 665f404

Please sign in to comment.