From 0a3b163772ab56ec0c0e775deec0ec3bed2b2825 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Sun, 11 Jun 2023 11:25:34 -0300 Subject: infra: don't perform redundant checks in expectDependencies --- src/content-function.js | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/content-function.js b/src/content-function.js index 73e4629e..2eb12f0e 100644 --- a/src/content-function.js +++ b/src/content-function.js @@ -9,6 +9,22 @@ export default function contentFunction({ data, generate, }) { + // Initial checks. These only need to be run once per description of a + // content function, and don't depend on any mutable context (e.g. which + // dependencies have been fulfilled so far). + + if (!generate) { + throw new Error(`Expected generate function`); + } + + if (sprawl && !extraDependencies.includes('wikiData')) { + throw new Error(`Content functions which sprawl must specify wikiData in extraDependencies`); + } + + // Pass all the details to expectDependencies, which will recursively build + // up a set of fulfilled dependencies and make functions like `relations` + // and `generate` callable only with sufficient fulfilled dependencies. + return expectDependencies({ sprawl, relations, @@ -33,18 +49,10 @@ export function expectDependencies({ expectedExtraDependencyKeys, fulfilledDependencies, }) { - if (!generate) { - throw new Error(`Expected generate function`); - } - const hasSprawlFunction = !!sprawl; const hasRelationsFunction = !!relations; const hasDataFunction = !!data; - if (hasSprawlFunction && !expectedExtraDependencyKeys.includes('wikiData')) { - throw new Error(`Content functions which sprawl must specify wikiData in extraDependencies`); - } - const fulfilledDependencyKeys = Object.keys(fulfilledDependencies); const invalidatingDependencyKeys = Object.entries(fulfilledDependencies) -- cgit 1.3.0-6-gf8a5