Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an IntlMemoizer as an argument that can be shared between MessageContexts #254

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 5 additions & 11 deletions fluent/src/context.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import resolve from "./resolver";
import FluentResource from "./resource";
import IntlMemoizer from "./intl_memoizer";

/**
* Message contexts are single-language stores of translations. They are
Expand Down Expand Up @@ -52,7 +53,8 @@ export class MessageContext {
constructor(locales, {
functions = {},
useIsolating = true,
transform = v => v
transform = v => v,
intlMemoizer = new IntlMemoizer(),
} = {}) {
this.locales = Array.isArray(locales) ? locales : [locales];

Expand All @@ -61,7 +63,7 @@ export class MessageContext {
this._functions = functions;
this._useIsolating = useIsolating;
this._transform = transform;
this._intls = new WeakMap();
this._intlMemoizer = intlMemoizer;
}

/*
Expand Down Expand Up @@ -210,14 +212,6 @@ export class MessageContext {
}

_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];
return this._intlMemoizer.get(ctor, this.locales, opts);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove this method completely and use ctx._intlMemoizer directly in the resolver (in types.js).

}
}
17 changes: 17 additions & 0 deletions fluent/src/intl_memoizer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export default class IntlMemoizer {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can remove some boilerplate by subclassing WeakMap here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, should we export IntlMemoizer in index.js?

constructor() {
this._intls = new WeakMap();
}

get(ctor, locales, opts) {
const cache = this._intls.get(ctor) || new Map();
const id = {locales, ...opts};

if (!cache.has(id)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks rather Pythonic to me :) Sadly, it won't work in JS. id is a new object literal every time this code runs. cache.has(id) will always be false.

cache.set(id, new ctor(locales, opts));
this._intls.set(ctor, cache);
}

return cache.get(id);
}
}