Upgrade to Pro — share decks privately, control downloads, hide ads and more …

About Integrant

ayato
March 13, 2017

About Integrant

ayato

March 13, 2017
Tweet

More Decks by ayato

Other Decks in Programming

Transcript

  1. Multimethodによるコンポーネント定義 •レコードやマップに限らず関数やただの数値などもコン ポーネントにすることが可能 (require '[ring.jetty.adapter :as jetty] '[ring.util.response :as resp])

    (defmethod ig/init-key :adapter/jetty [_ {:keys [handler] :as opts}] (jetty/run-jetty handler (-> opts (dissoc :handler) (assoc :join? false)))) (defmethod ig/init-key :handler/greet [_ {:keys [name]}] (fn [_] (resp/response (str "Hello " name))))
  2. 一時停止と再開のサポート (defmethod ig/init-key :adapter/jetty [_ opts] (let [handler (atom (delay

    (:handler opts))) options (-> opts (dissoc :handler) (assoc :join? false))] {:handler handler :server (jetty/run-jetty (fn [req] (@@handler req)) options)})) (defmethod ig/halt-key! :adapter/jetty [_ {:keys [server]}] (.stop server)) (defmethod ig/suspend-key! :adapter/jetty [_ {:keys [handler]}] (reset! handler (promise))) (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))))
  3. 同じコンポーネントを複数作る (derive :demo/master-db :demo/db) (derive :demo/read-db-1 :demo/db) (derive :demo/read-db-2 :demo/db)

    (defmethod ig/init-key :demo/db [_ opts] ...) (ig/init {:demo/read-db-1 {...} :demo/read-db-2 {...} :demo/master-db {...} :demo/get-user-handler 
 {:db (ig/ref :demo/read-db-1)} :demo/get-company-handler {:db (ig/ref :demo/read-db-2)} :demo/update-user-handler {:db (ig/ref :demo/master-db)}}
  4. baumと組み合わせる {$override* "dev-resources/config-local.edn" :demo/server {:port 3000 :handler #igref :demo/endpoint} :demo/database

    {:dbtype "postgresql" :dbname "my_blog" :user #env :db-user :password #env :db-password} :demo/endpoint {:db #igref :demo/database}}