CLJC: address (some of) Daniel's review comments

This commit is contained in:
Sam Roberton 2016-07-21 12:42:25 +10:00
parent cc33fe5a95
commit 951682db81
3 changed files with 34 additions and 13 deletions

View File

@ -1,7 +1,31 @@
(ns re-frame.interop)
(ns re-frame.interop
(:import java.util.concurrent.Executors))
;; The purpose of this file is to provide JVM-runnable implementations of the
;; CLJS equivalents in interop.cljs.
;;
;; These implementations are to enable you to bring up a re-frame app on the JVM
;; in order to run tests, or to develop at a JVM REPL instead of a CLJS one.
;;
;; Please note, though, that the purpose here *isn't* to fully replicate all of
;; re-frame's behaviour in a real CLJS environment. We don't have Reagent or
;; React on the JVM, and we don't try to mimic the stateful lifecycles that they
;; embody.
;;
;; In particular, if you're performing side effects in any code that's triggered
;; by a change to a Ratom's value, and not via a call to `dispatch`, then you're
;; going to have a hard time getting any accurate tests with this code.
;; However, if your subscriptions and Reagent render functions are pure, and
;; your side-effects are all managed by event handlers, then hopefully this will
;; allow you to write some useful tests that can run on the JVM.
(defonce ^:private executor (Executors/newSingleThreadExecutor))
(defn next-tick [f]
(.start (Thread. f)))
(.execute executor f)
nil)
(def empty-queue clojure.lang.PersistentQueue/EMPTY)
@ -10,17 +34,14 @@
(def debug-enabled? true)
(defn ratom [x]
(let [result (atom x)]
(alter-meta! result assoc ::ratom? true)
result))
(atom x))
(defn ratom? [maybe-ratom]
(and (satisfies? clojure.lang.IAtom maybe-ratom)
(::ratom? (meta maybe-ratom))))
(defn ratom? [x]
(satisfies? clojure.lang.IAtom x))
(defn make-reaction
"On JVM Clojure, return an atom-like thing which invokes the given function on
every deref. That is, `make-reaction` here provides precisely none of the
"On JVM Clojure, return a `deref`-able thing which invokes the given function
on every `deref`. That is, `make-reaction` here provides precisely none of the
benefits of `reagent.ratom/make-reaction` (which only invokes its function if
the reactions that the function derefs have changed value). But so long as `f`
only depends on other reactions (which also behave themselves), the only

View File

@ -14,8 +14,8 @@
(defn ratom [x]
(reagent.core/atom x))
(defn ratom? [maybe-ratom]
(satisfies? reagent.ratom/IReactiveAtom maybe-ratom))
(defn ratom? [x]
(satisfies? reagent.ratom/IReactiveAtom x))
(defn make-reaction [f]
(reagent.ratom/make-reaction f))

View File

@ -227,7 +227,7 @@
stack #?(:cljs (->> (js/Error. (str "Event " (first event-v) " dispatched from here:"))
.-stack
clojure.string/split-lines
(remove #(re-find #"react.inc.js|\(native\)" %))
(remove #(re-find #"react\.inc\.js|\(native\)" %))
(clojure.string/join "\n"))
:clj "n/a")]
(if (nil? event-v)