Back

resume (clj)

(source)

function

(resume config system) (resume config system keys)
Turn a config map into a system map, reusing resources from an existing system when it's possible to do so. Keys are traversed in dependency order, resumed with the resume-key multimethod, then the refs associated with the key are resolved.

Examples

integrant
(ns integrant.core-test
  (:require #?(:clj  [clojure.test :refer [are deftest is testing]]
               :cljs [cljs.test :refer-macros [are deftest is testing]])
            [integrant.core :as ig]
            [weavejester.dependency :as dep]))

(defmethod ig/init-key ::r [_ v] {:v v})
(defmethod ig/resolve-key ::r [_ {:keys [v]}] v)
(defmethod ig/resume-key ::r [k v _ _] (ig/init-key k v))

(defmethod ig/resume-key :default [k cfg cfg' sys]
  (swap! log conj [:resume k cfg cfg' sys])
  [cfg])

(defmethod ig/resume-key ::x [k cfg cfg' sys]
  (swap! log conj [:resume k cfg cfg' sys])
  :rx)

(deftest suspend-resume-test
  (testing "same configuration"
    (reset! log [])
    (let [c {::a (ig/ref ::b), ::b 1}
          m (ig/init c)]
      (ig/suspend! m)
      (ig/resume c m)
      (is (= @log [[:init ::b 1]
                   [:init ::a [1]]
                   [:suspend ::a [[1]]]
                   [:suspend ::b [1]]
                   [:resume ::b 1 1 [1]]
                   [:resume ::a [1] [1] [[1]]]]))))

  (testing "missing keys"
    (reset! log [])
    (let [c {::a (ig/ref ::b), ::b 1}
          m (ig/init c)]
      (ig/suspend! m)
      (ig/resume (dissoc c ::a) m)
      (is (= @log [[:init ::b 1]
                   [:init ::a [1]]
                   [:suspend ::a [[1]]]
                   [:suspend ::b [1]]
                   [:halt ::a [[1]]]
                   [:resume ::b 1 1 [1]]]))))

  (testing "missing refs"
    (reset! log [])
    (let [c {::a {:b (ig/ref ::b)}, ::b 1}
          m (ig/init c)]
      (ig/suspend! m)
      (ig/resume {::a []} m)
      (is (= @log [[:init ::b 1]
                   [:init ::a {:b [1]}]
                   [:suspend ::a [{:b [1]}]]
                   [:suspend ::b [1]]
                   [:halt ::b [1]]
                   [:resume ::a [] {:b [1]} [{:b [1]}]]]))))

  (testing "with custom resolve-key"
    (let [c  {::a (ig/ref ::r), ::r 1}
          m  (ig/init c)
          _  (ig/suspend! m)
          m' (ig/resume c m)]
      (is (= m m'))))

  (testing "composite keys"
    (reset! log [])
    (let [c {::a (ig/ref ::x), [::b ::x] 1}
          m (ig/init c)]
      (ig/suspend! m)
      (ig/resume c m)
      (is (= @log [[:init [::b ::x] 1]
                   [:init ::a :x]
                   [:suspend ::a [:x]]
                   [:suspend [::b ::x] :x]
                   [:resume [::b ::x] 1 1 :x]
                   [:resume ::a :rx :x [:x]]]))))

  (testing "resume key with dependencies"
    (reset! log [])
    (let [c {::a {:b (ig/ref ::b)}, ::b 1}
          m (ig/init c [::a])]
      (ig/suspend! m)
      (ig/resume c m [::a])
      (is (= @log
             [[:init ::b 1]
              [:init ::a {:b [1]}]
              [:suspend ::a [{:b [1]}]]
              [:suspend ::b [1]]
              [:resume ::b 1 1 [1]]
              [:resume ::a {:b [1]} {:b [1]} [{:b [1]}]]])))))
kit-clj/kit
(ns kit.edge.utils.nrepl
  (:require
    [clojure.tools.logging :as log]
    [integrant.core :as ig]
    [kit.ig-utils :as ig-utils]
    [nrepl.cmdline]
    [nrepl.server :as nrepl]))

(defmethod ig/resume-key :nrepl/server
  [key opts old-opts old-impl]
  (ig-utils/resume-handler key opts old-opts old-impl))
Zetawar/zetawar
(ns zetawar.system.datascript
  (:require
   [datascript.core :as d]
   [integrant.core :as ig]))

(defmethod ig/resume-key :zetawar.system/datascript [_ opts old-opts old-impl]
  (let [{:keys [schema]} opts
        old-schema (:schema old-opts)]
    (if (= schema old-schema)
      old-impl
      {:schema schema
       :conn (d/create-conn schema)})))
robert-stuttaford/bridge
(ns bridge.web.jetty
  (:require [integrant.core :as ig]
            [ring.adapter.jetty :as jetty]))

(defmethod ig/resume-key :adapter/jetty [key opts old-opts old-impl]
  (if (= (dissoc opts :handler) (dissoc old-opts :handler))
    (do (deliver @(:handler old-impl) (:handler opts))
        old-impl)
    (do (ig/halt-key! key old-impl)
        (ig/init-key key opts))))
Deraen/sass4clj
(ns sass4clj.integrant
  (:require [sass4clj.api :as api]
            [integrant.core :as ig]))

(defmethod ig/resume-key ::sass4clj [key opts old-opts old-impl]
  (if (= opts old-opts)
    old-impl
    (do
      (ig/halt-key! key old-impl)
      (ig/init-key key opts))))
Deraen/less4clj
(ns less4clj.integrant
  (:require [less4clj.api :as api]
            [integrant.core :as ig]))

(defmethod ig/resume-key ::less4clj [key opts old-opts old-impl]
  (if (= opts old-opts)
    old-impl
    (do
      (ig/halt-key! key old-impl)
      (ig/init-key key opts))))