Skip to content

Commit 0afa8aa

Browse files
added localization caching and removed redundant fromjs calls (#195)
* added localization caching and removed redundant fromjs calls * Cleanup localization cache implementation --------- Co-authored-by: Jason Desrosiers <jdesrosi@gmail.com>
1 parent 7d36d17 commit 0afa8aa

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

src/json-schema-errors.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ import { Localization } from "./localization.js";
1414

1515
/** @type API.jsonSchemaErrors */
1616
export const jsonSchemaErrors = async (errorOutput, schemaUri, instance, options = {}) => {
17-
const normalizedErrors = await normalizedOutput(instance, errorOutput, schemaUri);
1817
const rootInstance = Instance.fromJs(instance);
19-
const localization = await Localization.forLocale(options.locale ?? "en-US");
18+
const [normalizedErrors, localization] = await Promise.all([
19+
normalizedOutput(rootInstance, errorOutput, schemaUri),
20+
Localization.forLocale(options.locale ?? "en-US")
21+
]);
2022
return await getErrors(normalizedErrors, rootInstance, localization);
2123
};
2224

@@ -28,12 +30,11 @@ export const setNormalizationHandler = (schemaUri, handler) => {
2830
normalizationHandlers[schemaUri] = handler;
2931
};
3032

31-
/** @type (instance: API.Json, errorOutput: API.OutputUnit, subjectUri: string) => Promise<API.NormalizedOutput> */
32-
async function normalizedOutput(instance, errorOutput, subjectUri) {
33+
/** @type (value: JsonNode, errorOutput: API.OutputUnit, subjectUri: string) => Promise<API.NormalizedOutput> */
34+
async function normalizedOutput(value, errorOutput, subjectUri) {
3335
const schema = await getSchema(subjectUri);
3436
const errorIndex = await constructErrorIndex(errorOutput, schema);
3537
const { schemaUri, ast } = await compile(schema);
36-
const value = Instance.fromJs(instance);
3738
return evaluateSchema(schemaUri, value, { ast, errorIndex, plugins: [...ast.plugins] });
3839
}
3940

src/localization.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import { FluentBundle, FluentResource } from "@fluent/bundle";
66
* @import { ContainsRange, Json } from "./index.d.ts"
77
*/
88

9+
/** @type Map<string, Localization> */
10+
const localizationCache = new Map();
11+
912
export class Localization {
1013
/**
1114
* @param {string} locale
@@ -20,16 +23,19 @@ export class Localization {
2023

2124
/** @type (locale: string) => Promise<Localization> */
2225
static async forLocale(locale) {
23-
try {
24-
const ftl = await readFile(`${import.meta.dirname}/translations/${locale}.ftl`, "utf-8");
25-
const resource = new FluentResource(ftl);
26-
const bundle = new FluentBundle(locale);
27-
bundle.addResource(resource);
28-
29-
return new Localization(locale, bundle);
30-
} catch (error) {
31-
throw Error(`The ${locale} locale is not supported.`, { cause: error });
26+
if (!localizationCache.has(locale)) {
27+
try {
28+
const ftl = await readFile(`${import.meta.dirname}/translations/${locale}.ftl`, "utf-8");
29+
const resource = new FluentResource(ftl);
30+
const bundle = new FluentBundle(locale);
31+
bundle.addResource(resource);
32+
localizationCache.set(locale, new Localization(locale, bundle));
33+
} catch (error) {
34+
throw Error(`The ${locale} locale is not supported.`, { cause: error });
35+
}
3236
}
37+
38+
return /** @type Localization */ (localizationCache.get(locale));
3339
}
3440

3541
/** @type (messageId: string, args: Record<string, FluentVariable>) => string */

0 commit comments

Comments
 (0)