Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ClojureScript support #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 0 additions & 94 deletions src/integrant/repl.clj

This file was deleted.

115 changes: 115 additions & 0 deletions src/integrant/repl.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
(ns integrant.repl
(:require [integrant.core :as ig]
[integrant.repl.state :as state]
#?(:clj [clojure.tools.namespace.repl :as repl])))

#?(:clj (repl/disable-reload! (find-ns 'integrant.core)))

(defn set-prep! [prep]
#?(:clj (alter-var-root #'state/preparer (constantly prep))
:cljs (set! state/preparer prep)))

#?(:clj (defn error [s] (Error. s))
:cljs (defn error [s] (js/Error. s)) )

(defn- prep-error []
(error "No system preparer function found."))

(defn prep []
(if-let [prep state/preparer]
(do #?(:clj (alter-var-root #'state/config (fn [_] (prep)))
:cljs (set! state/config (prep)))
:prepped)
(throw (prep-error))))

(defn- halt-system [system]
(when system (ig/halt! system)))

(defn- build-system [build wrap-ex]
(try
(build)
(catch #?(:clj clojure.lang.ExceptionInfo
:cljs ExceptionInfo) ex
(if-let [system (:system (ex-data ex))]
(try
(ig/halt! system)
(catch #?(:clj clojure.lang.ExceptionInfo
:cljs ExceptionInfo)
halt-ex
(throw (wrap-ex ex halt-ex)))))
(throw ex))))

(defn- init-system [config keys]
(build-system
(if keys
#(ig/init config keys)
#(ig/init config))
#(ex-info "Config failed to init; also failed to halt failed system"
{:init-exception %1}
%2)))

(defn- resume-system [config system]
(build-system
#(ig/resume config system)
#(ex-info "Config failed to resume; also failed to halt failed system"
{:resume-exception %1}
%2)))

(defn init
([] (init nil))
([keys]
#?(:clj (alter-var-root #'state/system (fn [sys]
(halt-system sys)
(init-system state/config keys)))
:cljs (set! state/system (do (halt-system state/system)
(init-system state/config keys))))
:initiated))

(defn go
([] (go nil))
([keys]
(prep)
(init keys)))

(defn clear []
#?(:clj (do (alter-var-root #'state/system (fn [sys] (halt-system sys) nil))
(alter-var-root #'state/config (constantly nil)))
:cljs (do (set! state/system (do (halt-system state/system) nil))
(set! state/config nil)))
:cleared)

(defn halt []
(halt-system state/system)
#?(:clj (alter-var-root #'state/system (constantly nil))
:cljs (set! state/system nil))
:halted)

(defn suspend []
(when state/system (ig/suspend! state/system))
:suspended)

(defn resume []
(if-let [prep state/preparer]
(let [cfg (prep)]
#?(:clj (do (alter-var-root #'state/config (constantly cfg))
(alter-var-root #'state/system (fn [sys]
(if sys
(resume-system cfg sys)
(init-system cfg nil)))))
:cljs (do (set! state/config cfg)
(set! state/system (if state/system
(resume-system cfg state/system)
(init-system cfg nil)))))
:resumed)
(throw (prep-error))))

(defn reset []
(suspend)
#?(:clj (repl/refresh :after 'integrant.repl/resume)
:cljs (integrant.repl/resume)))


(defn reset-all []
(suspend)
#?(:clj (repl/refresh-all :after 'integrant.repl/resume)
:cljs (integrant.repl/resume)))
8 changes: 0 additions & 8 deletions src/integrant/repl/state.clj

This file was deleted.

8 changes: 8 additions & 0 deletions src/integrant/repl/state.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(ns integrant.repl.state
#?(:clj (:require [clojure.tools.namespace.repl :as repl])))

#?(:clj (repl/disable-reload!))

(def config nil)
(def system nil)
(def preparer nil)