From 951682db81028addcbe06b21c7c53461e0da2e29 Mon Sep 17 00:00:00 2001 From: Sam Roberton Date: Thu, 21 Jul 2016 12:42:25 +1000 Subject: [PATCH] CLJC: address (some of) Daniel's review comments --- src/re_frame/interop.clj | 41 +++++++++++++++++++++++++++++---------- src/re_frame/interop.cljs | 4 ++-- src/re_frame/router.cljc | 2 +- 3 files changed, 34 insertions(+), 13 deletions(-) diff --git a/src/re_frame/interop.clj b/src/re_frame/interop.clj index 2e277af..9d8d6c8 100644 --- a/src/re_frame/interop.clj +++ b/src/re_frame/interop.clj @@ -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 diff --git a/src/re_frame/interop.cljs b/src/re_frame/interop.cljs index 94512ea..b0d5e9b 100644 --- a/src/re_frame/interop.cljs +++ b/src/re_frame/interop.cljs @@ -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)) diff --git a/src/re_frame/router.cljc b/src/re_frame/router.cljc index cd2cb0a..66352ad 100644 --- a/src/re_frame/router.cljc +++ b/src/re_frame/router.cljc @@ -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)