From 98496d63d80f9a5b055b75554dfc77a699cfbcc7 Mon Sep 17 00:00:00 2001 From: anatoly Date: Mon, 22 Feb 2016 12:15:19 -0500 Subject: [PATCH] #47 alpha draft for composable states --- src/mount/core.cljc | 48 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/src/mount/core.cljc b/src/mount/core.cljc index 95f6816..ea125cc 100644 --- a/src/mount/core.cljc +++ b/src/mount/core.cljc @@ -248,6 +248,54 @@ (dorun (map rollback! states)) ;; restore to origin from "start-with" {:stopped stopped})) +;; composable set of states + +(defn only + ([states] + states) + ([states these] + (filter these states))) + +(defn with-args + ([args] + (with-args (find-all-states) args)) + ([states args] + (reset! -args args) ;; TODO localize + states)) + +(defn except + ([states] + (except (find-all-states) states)) + ([states these] + (remove these states))) + +(defn swap + ([with] + (swap (find-all-states) with)) + ([states with] + (doseq [[from to] with] + (substitute! (var-to-str from) + to :value)) + states)) + +(defn swap-states + ([with] + (swap-states (find-all-states) with)) + ([states with] + (doseq [[from to] with] + (substitute! (var-to-str from) + (var-to-str to) :state)) + states)) + +#_(-> (only #{1 2 3 4}) + (with-args {}) + (except #{2 1}) + (swap {2 42 1 34}) + (swap-states {4 "#'foo.bar/42"}) + (start)) + +;; explicit, not composable (subject to depreciate?) + (defn stop-except [& states] (let [all (set (find-all-states)) states (map var-to-str states)