Skip to content

Commit

Permalink
Switch from cljx to cljc
Browse files Browse the repository at this point in the history
This forces to upgrade to a newer version of codox
  • Loading branch information
fredZen committed Mar 4, 2016
1 parent 8cc6f4e commit 4c3f68a
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 87 deletions.
20 changes: 5 additions & 15 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,12 @@
:dependencies [[org.clojure/clojure "1.7.0"]
[org.clojure/core.async "0.2.374"]
[org.clojure/clojurescript "1.7.228"]]
:plugins [[codox "0.6.7"]
[lein-cljsbuild "1.1.2"]
[com.keminglabs/cljx "0.3.2"]]
:source-paths ["src/clojure" "src/cljs"]
:plugins [[lein-codox "0.9.4"]
[lein-cljsbuild "1.1.2"]]
:source-paths ["src/cljc"]
:test-paths ["test/clojure"]
:hooks [cljx.hooks]
:cljx
{:builds [{:source-paths ["src/cljx"]
:output-path "target/classes"
:rules :clj}
{:source-paths ["src/cljx"]
:output-path "target/classes"
:rules :cljs}]}
:codox {:sources ["target/classes"]}
:cljsbuild
{:builds [{:source-paths ["target/classes"]
{:builds [{:source-paths ["src/cljc"]
:compiler {:output-to "target/main.js"
:optimizations :whitespace}}]}
:profiles
Expand All @@ -32,7 +22,7 @@
:as a :refer [go go-loop <! >! <!! >!!]])}}
:test {:plugins [[com.cemerick/clojurescript.test "0.3.0"]]
:cljsbuild
{:builds ^:replace [{:source-paths ["target/classes" "test/cljs"]
{:builds ^:replace [{:source-paths ["src/cljc" "test/cljs"]
:compiler {:output-to "target/test.js"
:optimizations :whitespace}}]
:test-commands {"unit-tests" ["phantomjs" :runner "target/test.js"]}}}}
Expand Down
140 changes: 68 additions & 72 deletions src/cljx/reagi/core.cljx → src/cljc/reagi/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@
"Functions and types for functional reactive programming."
(:refer-clojure :exclude [constantly count cycle deliver filter flatten
map mapcat merge reduce remove time])
#+clj
(:import [clojure.lang IDeref IFn IPending])
#+clj
(:require [clojure.core :as core]
[clojure.core.async :as a :refer [go go-loop <! >! <!! >!!]])
#+cljs
(:require [cljs.core :as core]
[cljs.core.async :as a :refer [<! >!]])
#+cljs
(:require-macros [reagi.core :refer [behavior]]
[cljs.core.async.macros :refer [go go-loop]]))
#?@(:clj [(:import [clojure.lang IDeref IFn IPending])
(:require [clojure.core :as core]
[clojure.core.async :as a :refer [go go-loop <! >! <!! >!!]])]
:cljs [(:require [cljs.core :as core]
[cljs.core.async :as a :refer [<! >!]])
(:require-macros [reagi.core :refer [behavior]]
[cljs.core.async.macros :refer [go go-loop]])]))

(defprotocol ^:no-doc Signal
(complete? [signal]
Expand All @@ -30,7 +26,7 @@
Boxed
(unbox [_] x))

#+clj (ns-unmap *ns* '->Completed)
#?(:clj (ns-unmap *ns* '->Completed))

(defn completed
"Wraps x to guarantee that it will be the last value in a behavior or event
Expand All @@ -46,24 +42,24 @@
x
(reify Boxed (unbox [_] x))))

#+clj
(extend-protocol Boxed
Object (unbox [x] x)
nil (unbox [x] x))
#?(:clj
(extend-protocol Boxed
Object (unbox [x] x)
nil (unbox [x] x)))

#+cljs
(extend-protocol Boxed
default
(unbox [x] x))
#?(:cljs
(extend-protocol Boxed
default
(unbox [x] x)))

(deftype Behavior [func cache]
IDeref
(#+clj deref #+cljs -deref [behavior]
(#?(:clj deref :cljs -deref) [behavior]
(unbox (swap! cache #(if (instance? Completed %) % (func)))))
Signal
(complete? [_] (instance? Completed @cache)))

#+clj (ns-unmap *ns* '->Behavior)
#?(:clj (ns-unmap *ns* '->Behavior))

(defn behavior-call
"Takes a zero-argument function and yields a Behavior object that will
Expand All @@ -84,8 +80,8 @@

(def time
"A behavior that tracks the current time in seconds."
#+clj (behavior (/ (System/nanoTime) 1000000000.0))
#+cljs (behavior (/ (.getTime (js/Date.)) 1000.0)))
#?(:clj (behavior (/ (System/nanoTime) 1000000000.0))
:cljs (behavior (/ (.getTime (js/Date.)) 1000.0))))

(defn delta
"Return a behavior that tracks the time in seconds from when it was created."
Expand Down Expand Up @@ -145,39 +141,39 @@
(recur)))))
m))

#+clj
(defn- peek!! [mult time-ms]
(let [ch (a/chan)]
(a/tap mult ch)
(try
(if time-ms
(first (a/alts!! [ch (a/timeout time-ms)]))
(<!! ch))
(finally
(a/untap mult ch)))))

#+clj
(def ^:private dependencies
(java.util.Collections/synchronizedMap (java.util.WeakHashMap.)))
#?(:clj
(defn- peek!! [mult time-ms]
(let [ch (a/chan)]
(a/tap mult ch)
(try
(if time-ms
(first (a/alts!! [ch (a/timeout time-ms)]))
(<!! ch))
(finally
(a/untap mult ch))))))

#?(:clj
(def ^:private dependencies
(java.util.Collections/synchronizedMap (java.util.WeakHashMap.))))

(defn- depend-on
"Protect a collection of child objects from being GCed before the parent."
[parent children]
#+clj (.put dependencies parent children))

#+clj
(defn- deref-events [mult head ms timeout-val]
(if-let [hd @head]
(unbox hd)
(if-let [val (peek!! mult ms)]
(unbox val)
timeout-val)))

#+cljs
(defn- deref-events [head]
(if-let [hd @head]
(unbox hd)
js/undefined))
#?(:clj (.put dependencies parent children)))

#?(:clj
(defn- deref-events [mult head ms timeout-val]
(if-let [hd @head]
(unbox hd)
(if-let [val (peek!! mult ms)]
(unbox val)
timeout-val))))

#?(:cljs
(defn- deref-events [head]
(if-let [hd @head]
(unbox hd)
js/undefined)))

(defprotocol ^:no-doc Disposable
(dispose [x]
Expand All @@ -189,19 +185,19 @@

(deftype Events [ch mult head closed disposers]
IPending
#+clj (isRealized [_] (not (nil? @head)))
#+cljs (-realized? [_] (not (nil? @head)))
#?(:clj (isRealized [_] (not (nil? @head)))
:cljs (-realized? [_] (not (nil? @head))))

IDeref
#+clj (deref [self] (deref-events mult head nil nil))
#+cljs (-deref [self] (deref-events head))
#?(:clj (deref [self] (deref-events mult head nil nil))
:cljs (-deref [self] (deref-events head)))

#+clj clojure.lang.IBlockingDeref
#+clj (deref [_ ms timeout-val] (deref-events mult head ms timeout-val))
#?@(:clj [clojure.lang.IBlockingDeref
(deref [_ ms timeout-val] (deref-events mult head ms timeout-val))])

IFn
#+clj (invoke [stream msg] (do (>!! ch (box msg)) stream))
#+cljs (-invoke [stream msg] (do (go (>! ch (box msg))) stream))
#?(:clj (invoke [stream msg] (do (>!! ch (box msg)) stream))
:cljs (-invoke [stream msg] (do (go (>! ch (box msg))) stream)))

Observable
(port [_] ch)
Expand All @@ -217,16 +213,16 @@
(dispose [_] (doseq [d @disposers] (d)) (a/close! ch))
(on-dispose [_ d] (swap! disposers conj d))

#+clj Object
#+clj (finalize [stream] (dispose stream)))
#?@(:clj [Object
(finalize [stream] (dispose stream))]))

#+clj (ns-unmap *ns* '->Events)
#?(:clj (ns-unmap *ns* '->Events))

(defn- no-op [])

(def ^:private no-value
#+clj (Object.)
#+cljs (js/Object.))
#?(:clj (Object.)
:cljs (js/Object.)))

(defn- no-value? [x]
(identical? x no-value))
Expand Down Expand Up @@ -402,8 +398,8 @@
(reduce #(%2 %1) init stream))

(def ^:private empty-queue
#+clj clojure.lang.PersistentQueue/EMPTY
#+cljs cljs.core.PersistentQueue.EMPTY)
#?(:clj clojure.lang.PersistentQueue/EMPTY
:cljs cljs.core.PersistentQueue.EMPTY))

(defn buffer
"Buffer all the events in the stream. A maximum buffer size may be specified,
Expand Down Expand Up @@ -443,8 +439,8 @@
(map first)))

(defn- time-ms []
#+clj (System/currentTimeMillis)
#+cljs (.getTime (js/Date.)))
#?(:clj (System/currentTimeMillis)
:cljs (.getTime (js/Date.))))

(defn- throttle-ch [timeout-ms in out]
(go-loop [t0 0]
Expand All @@ -470,8 +466,8 @@
(let [[_ port] (a/alts! [stop (a/timeout interval)])]
(when (not= port stop)
(let [val @ref]
#+clj (>! out (box val))
#+cljs (when-not (undefined? val) (>! out (box val)))
#?(:clj (>! out (box val))
:cljs (when-not (undefined? val) (>! out (box val))))
(when-not (and (signal? ref) (complete? ref))
(recur))))))
(a/close! out)))
Expand Down

0 comments on commit 4c3f68a

Please sign in to comment.