Skip to content

Commit

Permalink
fix: lazy loading some modules (#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Jun 13, 2023
1 parent 3731a59 commit 3806c65
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 31 deletions.
7 changes: 7 additions & 0 deletions declarations/util/memorize.d.ts
@@ -0,0 +1,7 @@
export default memoize;
/**
* @template T
* @param fn {(function(): any) | undefined}
* @returns {function(): T}
*/
declare function memoize<T>(fn: (() => any) | undefined): () => T;
15 changes: 12 additions & 3 deletions src/ValidationError.js
@@ -1,4 +1,4 @@
const { stringHints, numberHints } = require("./util/hints");
import memoize from "./util/memorize";

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

const getUtilHints = memoize(() =>
// eslint-disable-next-line global-require
require("./util/hints")
);

/**
* @param {Schema} schema
* @param {boolean} logic
* @returns {string[]}
*/
function getHints(schema, logic) {
if (likeNumber(schema) || likeInteger(schema)) {
return numberHints(schema, logic);
const util = getUtilHints();

return util.numberHints(schema, logic);
} else if (likeString(schema)) {
return stringHints(schema, logic);
const util = getUtilHints();

return util.stringHints(schema, logic);
}

return [];
Expand Down
26 changes: 26 additions & 0 deletions src/util/memorize.js
@@ -0,0 +1,26 @@
/**
* @template T
* @param fn {(function(): any) | undefined}
* @returns {function(): T}
*/
const memoize = (fn) => {
let cache = false;
/** @type {T} */
let result;

return () => {
if (cache) {
return result;
}
result = /** @type {function(): any} */ (fn)();
cache = true;
// Allow to clean up memory for fn
// and all dependent resources
// eslint-disable-next-line no-undefined, no-param-reassign
fn = undefined;

return result;
};
};

export default memoize;
37 changes: 9 additions & 28 deletions src/validate.js
@@ -1,32 +1,5 @@
import addAbsolutePathKeyword from "./keywords/absolutePath";
import addUndefinedAsNullKeyword from "./keywords/undefinedAsNull";

import ValidationError from "./ValidationError";

/**
* @template T
* @param fn {(function(): any) | undefined}
* @returns {function(): T}
*/
const memoize = (fn) => {
let cache = false;
/** @type {T} */
let result;

return () => {
if (cache) {
return result;
}
result = /** @type {function(): any} */ (fn)();
cache = true;
// Allow to clean up memory for fn
// and all dependent resources
// eslint-disable-next-line no-undefined, no-param-reassign
fn = undefined;

return result;
};
};
import memoize from "./util/memorize";

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

// Custom keywords
// eslint-disable-next-line global-require
const addAbsolutePathKeyword = require("./keywords/absolutePath").default;

addAbsolutePathKeyword(ajv);

const addUndefinedAsNullKeyword =
// eslint-disable-next-line global-require
require("./keywords/undefinedAsNull").default;

addUndefinedAsNullKeyword(ajv);

return ajv;
Expand Down

0 comments on commit 3806c65

Please sign in to comment.