From 88fd8c772afe66e8828c00c2520d957d515d86f0 Mon Sep 17 00:00:00 2001 From: Dominic Monroe Date: Sat, 9 Oct 2021 14:53:28 +0100 Subject: [PATCH] WIP: Make possible to supply custom chains Fix #3 Relates to #2 --- src/juxt/clip/core.cljc | 37 +++++++++++++++++++++++++++---------- src/juxt/clip/edn.clj | 10 ++++++++++ 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/src/juxt/clip/core.cljc b/src/juxt/clip/core.cljc index ad6ca38..db5b6b0 100644 --- a/src/juxt/clip/core.cljc +++ b/src/juxt/clip/core.cljc @@ -33,15 +33,29 @@ of requiring a code form, in addition the anaphoric variable `this` is available to refer to the started component." [system-config] - (let [{:keys [components executor] - :or {executor impl/exec-queue}} system-config + (let [{:keys [components executor chains] + :or {executor impl/exec-queue + chains {:start + (fn [component-chain] + (for [component component-chain + f [(impl/pre-starting-f components) + (impl/starting-f components) + (impl/post-starting-f components)]] + (f component)))}}} system-config [_ component-chain] (safely-derive-parts components [])] (executor - (for [component component-chain - f [(impl/pre-starting-f components) - (impl/starting-f components) - (impl/post-starting-f components)]] - (f component))))) + ((get chains :start) component-chain)))) + +(comment + (let [components {:a '{:pre-start (println "pre" 1) + :start (inc 2) + :post-start (println "iam" this)}}] + (start + {:components components + :chains {:start [#_(impl/pre-starting-f components) + (impl/starting-f components) + (impl/post-starting-f components)]}})) + ) (defn stop "Takes a system config to stop. @@ -51,13 +65,16 @@ the target is AutoClosable then .close will be called on it, otherwise nothing." [system-config running-system] - (let [{:keys [components executor] - :or {executor impl/exec-queue}} system-config + (let [{:keys [components executor chains] + :or {executor impl/exec-queue + chains {:stop + (fn [component-chain] + (map impl/stopping-f component-chain))}}} system-config [_ component-chain] (safely-derive-parts (select-keys components (keys running-system)) ())] (executor - (map impl/stopping-f component-chain) + ((get chains :stop) component-chain) running-system))) (comment diff --git a/src/juxt/clip/edn.clj b/src/juxt/clip/edn.clj index 2ef7c74..bb3b288 100644 --- a/src/juxt/clip/edn.clj +++ b/src/juxt/clip/edn.clj @@ -14,6 +14,16 @@ executor (requiring-resolve executor))) +(defmethod edn->clj :chains + [_k chains] + (reduce-kv + (fn [chains method chain] + (if (symbol? chain) + (assoc chains method (requiring-resolve chain)) + chains)) + chains + chains)) + (defn analyze [system-config] (reduce-kv