Skip to content

Commit

Permalink
Merge pull request #397 from stasm/scope-memoize
Browse files Browse the repository at this point in the history
Move memoizeIntlObject to Scope
  • Loading branch information
stasm authored Jul 22, 2019
2 parents 97047be + 1d7a9ad commit 413a363
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 22 deletions.
12 changes: 0 additions & 12 deletions fluent/src/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,16 +235,4 @@ export default class FluentBundle {
throw err;
}
}

_memoizeIntlObject(ctor, opts) {
const cache = this._intls.get(ctor) || {};
const id = JSON.stringify(opts);

if (!cache[id]) {
cache[id] = new ctor(this.locales, opts);
this._intls.set(ctor, cache);
}

return cache[id];
}
}
8 changes: 4 additions & 4 deletions fluent/src/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const PDI = "\u2069";


// Helper: match a variant key to the given selector.
function match(bundle, selector, key) {
function match(scope, selector, key) {
if (key === selector) {
// Both are strings.
return true;
Expand All @@ -62,8 +62,8 @@ function match(bundle, selector, key) {
}

if (selector instanceof FluentNumber && typeof key === "string") {
let category = bundle
._memoizeIntlObject(Intl.PluralRules, selector.opts)
let category = scope
.memoizeIntlObject(Intl.PluralRules, selector.opts)
.select(selector.value);
if (key === category) {
return true;
Expand Down Expand Up @@ -240,7 +240,7 @@ function SelectExpression(scope, {selector, variants, star}) {
// Match the selector against keys of each variant, in order.
for (const variant of variants) {
const key = resolveExpression(scope, variant.key);
if (match(scope.bundle, sel, key)) {
if (match(scope, sel, key)) {
return resolvePattern(scope, variant.value);
}
}
Expand Down
13 changes: 13 additions & 0 deletions fluent/src/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,17 @@ export default class Scope {
}
this.errors.push(error);
}

memoizeIntlObject(ctor, opts) {
let cache = this.bundle._intls.get(ctor);
if (!cache) {
cache = {};
this.bundle._intls.set(ctor, cache);
}
let id = JSON.stringify(opts);
if (!cache[id]) {
cache[id] = new ctor(this.bundle.locales, opts);
}
return cache[id];
}
}
8 changes: 2 additions & 6 deletions fluent/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ export class FluentNumber extends FluentType {

toString(scope) {
try {
const nf = scope.bundle._memoizeIntlObject(
Intl.NumberFormat, this.opts
);
const nf = scope.memoizeIntlObject(Intl.NumberFormat, this.opts);
return nf.format(this.value);
} catch (e) {
// XXX Report the error.
Expand All @@ -79,9 +77,7 @@ export class FluentDateTime extends FluentType {

toString(scope) {
try {
const dtf = scope.bundle._memoizeIntlObject(
Intl.DateTimeFormat, this.opts
);
const dtf = scope.memoizeIntlObject(Intl.DateTimeFormat, this.opts);
return dtf.format(this.value);
} catch (e) {
// XXX Report the error.
Expand Down

0 comments on commit 413a363

Please sign in to comment.