diff --git a/lib/schemaProxy.js b/lib/schemaProxy.js index 643f0f7f..2f796960 100644 --- a/lib/schemaProxy.js +++ b/lib/schemaProxy.js @@ -34,7 +34,7 @@ function loadExamples(file, num = 1) { } const handler = ({ - root = '', fullpath = null, filename = '.', schemas, parent = null, slugger, + root = '', fullpath = null, filename = '.', schemas, subschemas, parent = null, slugger, }) => { const meta = {}; @@ -162,14 +162,23 @@ const handler = ({ } // console.log('making new proxy from', target, prop, 'receiver', receiver[symbols.id]); - const subschema = new Proxy(retval, handler({ - root: `${root}/${prop}`, - parent: receiver, - fullpath, - filename, - schemas, - slugger, - })); + let subschema; + if (subschemas.has(retval)) { + subschema = subschemas.get(retval); + } + else { + subschema = new Proxy(retval, handler({ + root: `${root}/${prop}`, + parent: receiver, + fullpath, + filename, + schemas, + subschemas, + slugger, + })); + + subschemas.set(retval, subschema); + } if (subschema[keyword`$id`]) { // stow away the schema for lookup @@ -190,6 +199,8 @@ export default function loader() { files: {}, }; + const subschemas = new Map(); + const slugger = new GhSlugger(); return (/** @type {string} */ name, /** @type {any} */ schema) => { @@ -197,7 +208,7 @@ export default function loader() { const filename = path.basename(name); const fullpath = name === filename ? undefined : name; const proxied = new Proxy(schema, handler({ - filename, fullpath, schemas, slugger, + filename, fullpath, schemas, subschemas, slugger, })); schemas.loaded.push(proxied); if (proxied[keyword`$id`]) {