Skip to content

Commit 3806c65

Browse files
fix: lazy loading some modules (#178)
1 parent 3731a59 commit 3806c65

File tree

4 files changed

+54
-31
lines changed

4 files changed

+54
-31
lines changed

declarations/util/memorize.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default memoize;
2+
/**
3+
* @template T
4+
* @param fn {(function(): any) | undefined}
5+
* @returns {function(): T}
6+
*/
7+
declare function memoize<T>(fn: (() => any) | undefined): () => T;

src/ValidationError.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { stringHints, numberHints } = require("./util/hints");
1+
import memoize from "./util/memorize";
22

33
/** @typedef {import("json-schema").JSONSchema6} JSONSchema6 */
44
/** @typedef {import("json-schema").JSONSchema7} JSONSchema7 */
@@ -384,16 +384,25 @@ function formatHints(hints) {
384384
return hints.length > 0 ? `(${hints.join(", ")})` : "";
385385
}
386386

387+
const getUtilHints = memoize(() =>
388+
// eslint-disable-next-line global-require
389+
require("./util/hints")
390+
);
391+
387392
/**
388393
* @param {Schema} schema
389394
* @param {boolean} logic
390395
* @returns {string[]}
391396
*/
392397
function getHints(schema, logic) {
393398
if (likeNumber(schema) || likeInteger(schema)) {
394-
return numberHints(schema, logic);
399+
const util = getUtilHints();
400+
401+
return util.numberHints(schema, logic);
395402
} else if (likeString(schema)) {
396-
return stringHints(schema, logic);
403+
const util = getUtilHints();
404+
405+
return util.stringHints(schema, logic);
397406
}
398407

399408
return [];

src/util/memorize.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @template T
3+
* @param fn {(function(): any) | undefined}
4+
* @returns {function(): T}
5+
*/
6+
const memoize = (fn) => {
7+
let cache = false;
8+
/** @type {T} */
9+
let result;
10+
11+
return () => {
12+
if (cache) {
13+
return result;
14+
}
15+
result = /** @type {function(): any} */ (fn)();
16+
cache = true;
17+
// Allow to clean up memory for fn
18+
// and all dependent resources
19+
// eslint-disable-next-line no-undefined, no-param-reassign
20+
fn = undefined;
21+
22+
return result;
23+
};
24+
};
25+
26+
export default memoize;

src/validate.js

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,5 @@
1-
import addAbsolutePathKeyword from "./keywords/absolutePath";
2-
import addUndefinedAsNullKeyword from "./keywords/undefinedAsNull";
3-
41
import ValidationError from "./ValidationError";
5-
6-
/**
7-
* @template T
8-
* @param fn {(function(): any) | undefined}
9-
* @returns {function(): T}
10-
*/
11-
const memoize = (fn) => {
12-
let cache = false;
13-
/** @type {T} */
14-
let result;
15-
16-
return () => {
17-
if (cache) {
18-
return result;
19-
}
20-
result = /** @type {function(): any} */ (fn)();
21-
cache = true;
22-
// Allow to clean up memory for fn
23-
// and all dependent resources
24-
// eslint-disable-next-line no-undefined, no-param-reassign
25-
fn = undefined;
26-
27-
return result;
28-
};
29-
};
2+
import memoize from "./util/memorize";
303

314
const getAjv = memoize(() => {
325
// Use CommonJS require for ajv libs so TypeScript consumers aren't locked into esModuleInterop (see #110).
@@ -51,7 +24,15 @@ const getAjv = memoize(() => {
5124
addFormats(ajv, { keywords: true });
5225

5326
// Custom keywords
27+
// eslint-disable-next-line global-require
28+
const addAbsolutePathKeyword = require("./keywords/absolutePath").default;
29+
5430
addAbsolutePathKeyword(ajv);
31+
32+
const addUndefinedAsNullKeyword =
33+
// eslint-disable-next-line global-require
34+
require("./keywords/undefinedAsNull").default;
35+
5536
addUndefinedAsNullKeyword(ajv);
5637

5738
return ajv;

0 commit comments

Comments
 (0)