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

fix: lazy loading some modules #178

Merged
merged 2 commits into from Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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