Skip to content

Commit 413a363

Browse files
authored
Merge pull request #397 from stasm/scope-memoize
Move memoizeIntlObject to Scope
2 parents 97047be + 1d7a9ad commit 413a363

File tree

4 files changed

+19
-22
lines changed

4 files changed

+19
-22
lines changed

fluent/src/bundle.js

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -235,16 +235,4 @@ export default class FluentBundle {
235235
throw err;
236236
}
237237
}
238-
239-
_memoizeIntlObject(ctor, opts) {
240-
const cache = this._intls.get(ctor) || {};
241-
const id = JSON.stringify(opts);
242-
243-
if (!cache[id]) {
244-
cache[id] = new ctor(this.locales, opts);
245-
this._intls.set(ctor, cache);
246-
}
247-
248-
return cache[id];
249-
}
250238
}

fluent/src/resolver.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ const PDI = "\u2069";
4848

4949

5050
// Helper: match a variant key to the given selector.
51-
function match(bundle, selector, key) {
51+
function match(scope, selector, key) {
5252
if (key === selector) {
5353
// Both are strings.
5454
return true;
@@ -62,8 +62,8 @@ function match(bundle, selector, key) {
6262
}
6363

6464
if (selector instanceof FluentNumber && typeof key === "string") {
65-
let category = bundle
66-
._memoizeIntlObject(Intl.PluralRules, selector.opts)
65+
let category = scope
66+
.memoizeIntlObject(Intl.PluralRules, selector.opts)
6767
.select(selector.value);
6868
if (key === category) {
6969
return true;
@@ -240,7 +240,7 @@ function SelectExpression(scope, {selector, variants, star}) {
240240
// Match the selector against keys of each variant, in order.
241241
for (const variant of variants) {
242242
const key = resolveExpression(scope, variant.key);
243-
if (match(scope.bundle, sel, key)) {
243+
if (match(scope, sel, key)) {
244244
return resolvePattern(scope, variant.value);
245245
}
246246
}

fluent/src/scope.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,17 @@ export default class Scope {
2626
}
2727
this.errors.push(error);
2828
}
29+
30+
memoizeIntlObject(ctor, opts) {
31+
let cache = this.bundle._intls.get(ctor);
32+
if (!cache) {
33+
cache = {};
34+
this.bundle._intls.set(ctor, cache);
35+
}
36+
let id = JSON.stringify(opts);
37+
if (!cache[id]) {
38+
cache[id] = new ctor(this.bundle.locales, opts);
39+
}
40+
return cache[id];
41+
}
2942
}

fluent/src/types.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,7 @@ export class FluentNumber extends FluentType {
6161

6262
toString(scope) {
6363
try {
64-
const nf = scope.bundle._memoizeIntlObject(
65-
Intl.NumberFormat, this.opts
66-
);
64+
const nf = scope.memoizeIntlObject(Intl.NumberFormat, this.opts);
6765
return nf.format(this.value);
6866
} catch (e) {
6967
// XXX Report the error.
@@ -79,9 +77,7 @@ export class FluentDateTime extends FluentType {
7977

8078
toString(scope) {
8179
try {
82-
const dtf = scope.bundle._memoizeIntlObject(
83-
Intl.DateTimeFormat, this.opts
84-
);
80+
const dtf = scope.memoizeIntlObject(Intl.DateTimeFormat, this.opts);
8581
return dtf.format(this.value);
8682
} catch (e) {
8783
// XXX Report the error.

0 commit comments

Comments
 (0)