diff --git a/.serena/memories/common/core.md b/.serena/memories/common/core.md index 816d131e14..1c969b5ba5 100644 --- a/.serena/memories/common/core.md +++ b/.serena/memories/common/core.md @@ -26,6 +26,12 @@ Respect the intended abstraction direction in new/refactored code: Some legacy code violates this layering; do not copy those violations into new code when a focused refactor is practical. +## Build-mode dependent laziness + +`app.common.data.macros/lazy-delay` returns a delay whose expression is evaluated eagerly by default. Only the builds that set the `app.common.data.macros/lazy-delay?` closure define to `true` defer it to the first deref; every production build does (frontend `main` and `worker`, exporter, library), so the Closure compiler replaces the constant and drops the eager branch. On the JVM it is always eager. + +`app.common.schema/check-fn` resolves its schema through it: an invalid schema definition fails when the namespace loads in development and in tests, and only on the first check call in production. The other lazily built parts of `check-fn` (validator and explainer) are always deferred. + ## Focused memory routing Model, schema, and persistence shape: diff --git a/common/src/app/common/data/macros.cljc b/common/src/app/common/data/macros.cljc index 4a6bd5b2a9..39bba7db72 100644 --- a/common/src/app/common/data/macros.cljc +++ b/common/src/app/common/data/macros.cljc @@ -123,6 +123,27 @@ (list 'js* (c/str "(~{}?." (str/snake prop) "?? ~{})") obj (list 'cljs.core/get obj prop)) (list `c/get obj prop))) +#?(:cljs (goog-define lazy-delay? false)) + +(defmacro lazy-delay + "Like `delay`, but only lazy when the build asks for it. + + With the default value of `lazy-delay?` the expression is evaluated + immediately, so errors raised while computing it (an invalid schema + definition, for instance) surface when the namespace is loaded instead of + on the first deref. Production builds set `lazy-delay?` to `true` through + `:closure-defines`; then the expression is fully deferred and the compiler + drops the eager branch. On the JVM the expression is always evaluated + immediately." + [expr] + (if (:ns &env) + `(if lazy-delay? + (delay ~expr) + (let [value# ~expr] + (delay value#))) + `(let [value# ~expr] + (delay value#)))) + (defn runtime-assert [hint f] (try diff --git a/common/src/app/common/schema.cljc b/common/src/app/common/schema.cljc index f4fcab85da..14fa04f919 100644 --- a/common/src/app/common/schema.cljc +++ b/common/src/app/common/schema.cljc @@ -11,6 +11,7 @@ #?(:clj [malli.dev.pretty :as mdp]) #?(:clj [malli.dev.virhe :as v]) [app.common.data :as d] + [app.common.data.macros :as dm] [app.common.i18n :as i18n :refer [tr]] [app.common.json :as json] [app.common.math :as mth] @@ -320,25 +321,32 @@ ([s] (lookup sr/default-registry s)) ([registry s] (schema (mr/schema registry s)))) -(defn check-fn - "Create a predefined check function" - [s & {:keys [hint type code]}] - (let [s #?(:clj - (schema s) - :cljs - (try - (schema s) - (catch :default cause - (let [data (ex-data cause)] - (if (= :malli.core/invalid-schema (:type data)) - (throw (ex-info - (str "Invalid schema\n" - (pp/pprint-str (:data data))) - {})) - (throw cause)))))) +(defn- resolve-schema + "Resolves the schema definition. On ClojureScript it rewrites the malli + invalid schema error with a human readable message." + [s] + #?(:clj (schema s) + :cljs (try + (schema s) + (catch :default cause + (let [data (ex-data cause)] + (if (= :malli.core/invalid-schema (:type data)) + (throw (ex-info + (str "Invalid schema\n" + (pp/pprint-str (:data data))) + {})) + (throw cause))))))) - validator* (delay (m/validator s)) - explainer* (delay (m/explainer s)) +(defn check-fn + "Create a predefined check function. + + The schema definition is resolved lazily on production builds, and + eagerly on development ones, so invalid schemas are detected early + while developing. See `app.common.data.macros/lazy-delay`." + [s & {:keys [hint type code]}] + (let [s* (dm/lazy-delay (resolve-schema s)) + validator* (delay (m/validator @s*)) + explainer* (delay (m/explainer @s*)) hint (or ^boolean hint "check error") type (or ^boolean type :assertion) code (or ^boolean code :data-validation)] diff --git a/common/test/common_tests/data_macros_test.cljc b/common/test/common_tests/data_macros_test.cljc new file mode 100644 index 0000000000..8759cd7570 --- /dev/null +++ b/common/test/common_tests/data_macros_test.cljc @@ -0,0 +1,33 @@ +;; This Source Code Form is subject to the terms of the Mozilla Public +;; License, v. 2.0. If a copy of the MPL was not distributed with this +;; file, You can obtain one at http://mozilla.org/MPL/2.0/. +;; +;; Copyright (c) KALEIDOS SUBSIDIARY SL + +(ns common-tests.data-macros-test + (:require + [app.common.data.macros :as dm] + [clojure.test :as t])) + +(defn- eager? + "Mirrors the build mode check performed by `dm/lazy-delay`." + [] + #?(:clj true + :cljs (not dm/lazy-delay?))) + +(t/deftest lazy-delay-returns-a-delay-with-the-value + (let [value (dm/lazy-delay (str "foo" "bar"))] + (t/is (delay? value)) + (t/is (= "foobar" @value)))) + +(t/deftest lazy-delay-evaluates-immediately-by-default + (let [calls (atom 0) + _ (dm/lazy-delay (swap! calls inc))] + (t/is (= (if (eager?) 1 0) @calls)))) + +(t/deftest lazy-delay-evaluates-the-expression-only-once + (let [calls (atom 0) + value (dm/lazy-delay (swap! calls inc))] + (t/is (= 1 @value)) + (t/is (= 1 @value)) + (t/is (= 1 @calls)))) diff --git a/common/test/common_tests/runner.cljc b/common/test/common_tests/runner.cljc index 59305d5ec4..5bed751efb 100644 --- a/common/test/common_tests/runner.cljc +++ b/common/test/common_tests/runner.cljc @@ -17,6 +17,7 @@ [common-tests.attrs-test] [common-tests.buffer-test] [common-tests.colors-test] + [common-tests.data-macros-test] [common-tests.data-test] [common-tests.exceptions-test] [common-tests.files-builder-test] @@ -135,6 +136,7 @@ 'common-tests.attrs-test 'common-tests.buffer-test 'common-tests.colors-test + 'common-tests.data-macros-test 'common-tests.data-test 'common-tests.exceptions-test 'common-tests.files-builder-test diff --git a/common/test/common_tests/schema_test.cljc b/common/test/common_tests/schema_test.cljc index 08e26295a6..3e36900906 100644 --- a/common/test/common_tests/schema_test.cljc +++ b/common/test/common_tests/schema_test.cljc @@ -7,6 +7,7 @@ (ns common-tests.schema-test (:require [app.common.data :as d] + [app.common.data.macros :as dm] [app.common.schema :as sm] [app.common.schema.generators :as sg] [app.common.uuid :as uuid] @@ -283,3 +284,43 @@ (t/testing "generate" (t/is (true? (sm/validate ::sm/user-provided-uuid (sg/generate ::sm/user-provided-uuid))))))) + +(defn- catch-error + [f] + (try + (f) + nil + (catch #?(:clj Throwable :cljs :default) cause + cause))) + +(defn- lazy-resolution? + "Mirrors the build mode check performed by `app.common.data.macros/lazy-delay`." + [] + #?(:clj false + :cljs dm/lazy-delay?)) + +(t/deftest test-check-fn + (t/testing "returns the value when it is valid" + (let [check (sm/check-fn ::sm/uuid)] + (t/is (= uuid/zero (check uuid/zero))))) + + (t/testing "raises with the explain data when the value is invalid" + (let [check (sm/check-fn ::sm/uuid :hint "invalid uuid" + :type :custom :code :custom-code) + cause (catch-error #(check "not-a-uuid")) + data (ex-data cause)] + (t/is (some? cause)) + (t/is (= "invalid uuid" (ex-message cause))) + (t/is (= :custom (:type data))) + (t/is (= :custom-code (:code data))) + (t/is (= "invalid uuid" (:hint data))) + (t/is (map? (::sm/explain data))))) + + (t/testing "resolves the schema definition according to the build mode" + ;; A schema that does not exist raises only when it is resolved, so this + ;; checks when the resolution happens: on `check-fn` on non production + ;; builds, on the first call of the returned function on production ones. + (let [check (catch-error #(sm/check-fn ::unknown-schema))] + (if (lazy-resolution?) + (t/is (some? (catch-error #(check ::anything)))) + (t/is (some? check)))))) diff --git a/exporter/shadow-cljs.edn b/exporter/shadow-cljs.edn index 076e6a10f6..9bb46f47f6 100644 --- a/exporter/shadow-cljs.edn +++ b/exporter/shadow-cljs.edn @@ -24,7 +24,8 @@ :output-wrapper false} :release - {:compiler-options + {:closure-defines {app.common.data.macros/lazy-delay? true} + :compiler-options {:fn-invoke-direct true :source-map true :optimizations #shadow/env ["PENPOT_BUILD_OPTIMIZATIONS" :as :keyword :default :simple] diff --git a/frontend/shadow-cljs.edn b/frontend/shadow-cljs.edn index c18338fbf4..1b63abb96a 100644 --- a/frontend/shadow-cljs.edn +++ b/frontend/shadow-cljs.edn @@ -76,6 +76,7 @@ :release {:closure-defines {goog.DEBUG false goog.debug.LOGGING_ENABLED true + app.common.data.macros/lazy-delay? true app.config/compiled-version-tag #shadow/env ["VERSION_TAG" :default "develop"]} :compiler-options {:fn-invoke-direct true @@ -115,7 +116,8 @@ :warnings {:fn-deprecated false}} :release - {:compiler-options + {:closure-defines {app.common.data.macros/lazy-delay? true} + :compiler-options {:fn-invoke-direct true :optimizations #shadow/env ["PENPOT_BUILD_OPTIMIZATIONS" :as :keyword :default :advanced] :output-wrapper true diff --git a/library/shadow-cljs.edn b/library/shadow-cljs.edn index 772c09c854..a4516d2efb 100644 --- a/library/shadow-cljs.edn +++ b/library/shadow-cljs.edn @@ -35,7 +35,8 @@ :warnings {:fn-deprecated false}} :release - {:compiler-options + {:closure-defines {app.common.data.macros/lazy-delay? true} + :compiler-options {:fn-invoke-direct true :optimizations #shadow/env ["PENPOT_BUILD_OPTIMIZATIONS" :as :keyword :default :advanced] :pretty-print false