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

Testing out disabling Reagent ratom/rendering queue #612

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
2 changes: 2 additions & 0 deletions demo/reagentdemo/intro.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
[:p "Change it here: " [atom-input val]]])))

(defn timer-component []
;; FIXME: For some reason these timeouts trigger multiple
;; renders and each render triggers a new timeout.
(let [seconds-elapsed (r/atom 0)]
(fn []
(js/setTimeout #(swap! seconds-elapsed inc) 1000)
Expand Down
18 changes: 3 additions & 15 deletions src/reagent/impl/batching.cljs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
(ns reagent.impl.batching
(:refer-clojure :exclude [flush])
(:require [reagent.debug :refer-macros [assert-some]]
[reagent.impl.util :refer [is-client]]))
(:require [reagent.debug :refer-macros [assert-some]]))

;;; Update batching

Expand All @@ -10,19 +9,8 @@
(defn next-mount-count []
(set! mount-count (inc mount-count)))

(defn fake-raf [f]
(js/setTimeout f 16))

(def next-tick
(if-not is-client
fake-raf
(let [w js/window]
(.bind (or (.-requestAnimationFrame w)
(.-webkitRequestAnimationFrame w)
(.-mozRequestAnimationFrame w)
(.-msRequestAnimationFrame w)
fake-raf)
w))))
(defn next-tick [f]
(f))

(defn compare-mount-order
[^clj c1 ^clj c2]
Expand Down
22 changes: 3 additions & 19 deletions src/reagent/impl/template.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
[clojure.string :as string]
[reagent.impl.util :as util :refer [named?]]
[reagent.impl.component :as comp]
[reagent.impl.input :as input]
[reagent.impl.protocols :as p]
[reagent.ratom :as ratom]
[reagent.debug :refer-macros [dev? warn]]
Expand Down Expand Up @@ -206,24 +205,9 @@
jsprops (or (convert-props (if hasprops props) parsed)
#js {})
first-child (+ first (if hasprops 1 0))]
(if (input/input-component? component)
(let [;; Also read :key from props map, because
;; input wrapper will not place the key in useful place.
react-key (util/get-react-key props)
input-class (or (.-reagentInput compiler)
(let [x (comp/create-class input/input-spec compiler)]
(set! (.-reagentInput compiler) x)
x))]
(p/as-element
compiler
(with-meta [input-class argv component jsprops first-child compiler]
(merge (when react-key
{:key react-key})
(meta argv)))))
(do
(when-some [key (-> (meta argv) util/get-react-key)]
(set! (.-key jsprops) key))
(p/make-element compiler argv component jsprops first-child)))))
(when-some [key (-> (meta argv) util/get-react-key)]
(set! (.-key jsprops) key))
(p/make-element compiler argv component jsprops first-child)))

(defn raw-element [comp argv compiler]
(let [props (nth argv 2 nil)
Expand Down
6 changes: 3 additions & 3 deletions src/reagent/ratom.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@

(defn- rea-enqueue [r]
(when (nil? rea-queue)
(set! rea-queue #js [])
(batch/schedule))
(.push rea-queue r))
(set! rea-queue #js []))
(.push rea-queue r)
(batch/schedule))

;;; Atom

Expand Down
1 change: 1 addition & 0 deletions test/reagenttest/testcursor.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

(def testite 10)

#_
(deftest basic-cursor
(let [runs (running)
start-base (rv/atom {:a {:b {:c 0}}})
Expand Down
3 changes: 3 additions & 0 deletions test/reagenttest/testratom.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
(enable-console-print!)
;; (ratom-perf)

#_
(deftest basic-ratom
(let [runs (running)
start (rv/atom 0)
Expand Down Expand Up @@ -423,6 +424,7 @@
(dispose r)
(is (= runs (running)))))

#_
(deftest exception-side-effect
(let [runs (running)
state (r/atom {:val 1})
Expand All @@ -447,6 +449,7 @@
(dispose r3)
(is (= runs (running)))))

#_
(deftest exception-reporting
(let [runs (running)
state (r/atom {:val 1})
Expand Down
6 changes: 5 additions & 1 deletion test/reagenttest/testratomasync.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

(defn ar [f] (rv/track! f))

#_
(deftest basic-ratom
(sync)
(let [runs (running)
Expand Down Expand Up @@ -51,6 +52,7 @@
(sync)
(is (= (running) runs) "should not awaken")))

#_
(deftest double-dependency
(sync)
(let [runs (running)
Expand Down Expand Up @@ -102,7 +104,7 @@
(dispose !co))
(is (= runs (running)))))


#_
(deftest test-unsubscribe
(sync)
(dotimes [x testite]
Expand Down Expand Up @@ -193,6 +195,7 @@
(dispose d))
(is (= runs (running)))))

#_
(deftest test-dispose
(dotimes [x testite]
(let [runs (running)
Expand Down Expand Up @@ -276,6 +279,7 @@
(is (= @b 6))
(is (= runs (running)))))

#_
(deftest catching
(let [runs (running)
a (rv/atom false)
Expand Down
3 changes: 3 additions & 0 deletions test/reagenttest/testreagent.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,7 @@
(defn foo []
[:div])

#_
(u/deftest ^:dom test-err-messages
(when (dev?)
(is (thrown-with-msg?
Expand Down Expand Up @@ -1065,6 +1066,8 @@
(is (= "<p>#object[reagent.ratom.RAtom {:val 1}]</p>"
(as-string [:p (r/atom 1)]))))

;; FIXME: r/after-render won't work
#_
(u/deftest ^:dom test-after-render
(let [spy (atom 0)
val (atom 0)
Expand Down
3 changes: 2 additions & 1 deletion test/reagenttest/testtrack.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

(enable-console-print!)


#_
(deftest basic-ratom
(let [runs (running)
start (rv/atom 0)
Expand All @@ -51,6 +51,7 @@
(dispose const)
(is (= (running) runs))))

#_
(deftest test-track!
(sync)
(let [runs (running)
Expand Down
4 changes: 3 additions & 1 deletion test/reagenttest/testwithlet.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
(r/flush)
(rv/running))

#_
(deftest basic-with-let
(let [runs (running)
n1 (atom 0)
Expand Down Expand Up @@ -58,7 +59,7 @@

(is (= runs (running)))))


#_
(deftest test-with-let-args
(let [runs (running)
n1 (atom 0)
Expand Down Expand Up @@ -175,6 +176,7 @@
(is (= 1 @n4))
(is (= [[3 3 nil] 3 3 3] (tst f5)))))

#_
(deftest with-let-args
(let [runs (running)
active (atom 0)
Expand Down
1 change: 1 addition & 0 deletions test/reagenttest/testwrap.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
[reagent.core :as r]
[reagenttest.utils :as u]))

#_
(deftest test-wrap-basic
(let [state (r/atom {:foo 1})
ws (fn [] (r/wrap (:foo @state)
Expand Down
73 changes: 43 additions & 30 deletions test/reagenttest/utils.cljs
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
(ns reagenttest.utils
(:require-macros reagenttest.utils)
(:require [promesa.core :as p]
(:require [react :as react]
[promesa.core :as p]
[reagent.core :as r]
[reagent.debug :as debug]
[reagent.dom :as rdom]
[reagent.debug :as debug :refer [dev?]]
[reagent.dom.server :as server]
[reagent.impl.template :as tmpl]))
[reagent.dom.client :as rdomc]
[reagent.impl.template :as tmpl]
promesa.core))

;; Should be only set for tests....
;; (set! (.-IS_REACT_ACT_ENVIRONMENT js/window) true)

;; Silence ReactDOM.render warning
(defonce original-console-error (.-error js/console))

(set! (.-error js/console)
(fn [& [first-arg :as args]]
(cond
(and (string? first-arg) (.startsWith first-arg "Warning: ReactDOM.render is no longer supported in React 18."))
nil

(and (string? first-arg) (.startsWith first-arg "Warning: The current testing environment is not configured to support"))
nil

Expand Down Expand Up @@ -80,23 +78,42 @@
(defn act*
"Run f to trigger Reagent updates,
will return Promise which will resolve after
Reagent and React render."
Reagent and React render.

In production builds, the React.act isn't available,
so just mock with 17ms timeout... Hopefully that usually
is enough time for React to flush the queue?"
[f]
(let [p (p/deferred)]
(f)
(r/flush)
(r/after-render (fn []
(p/resolve! p)))
p))
;; async act doesn't return a real promise (with chainable then),
;; so wrap it.
(if (dev?)
(js/Promise.
(fn [resolve reject]
(try
(.then (react/act f)
resolve
reject)
(catch :default e
(reject e)))))
(js/Promise.
(fn [resolve reject]
(try
(f)
(js/setTimeout (fn []
(resolve))
;; 16.6ms is one animation frame @ 60hz
17)
(catch :default e
(reject e)))))))

(def ^:dynamic *render-error* nil)

(defn with-render*
"Render the given component to a DOM node,
after the the component is mounted to DOM,
run the given function and wait for the Promise
returned from the function to be resolved
before unmounting the component from DOM."
"Run initial render with React/act and then run
given function to check the results. If the function
also returns a Promise or thenable, this function
waits until that is resolved, before unmounting the
root and resolving the Promise this function returns."
([comp f]
(with-render* comp *test-compiler* f))
([comp options f]
Expand All @@ -107,18 +124,14 @@
compiler (:compiler options)
restore-error-handlers (when (:capture-errors options)
(init-capture))
root (rdomc/create-root div)
;; Magic setup to make exception from render available to the
;; with-render body.
render-error (atom nil)]
(try
(if compiler
(rdom/render comp div {:compiler compiler
:callback callback})
(rdom/render comp div callback))
(catch :default e
(reset! render-error e)
nil))
(-> first-render
(-> (act* (fn []
(if compiler
(rdomc/render root comp compiler)
(rdomc/render root comp))))
;; The callback is called even if render throws an error,
;; so this is always resolved.
(p/then (fn []
Expand All @@ -130,7 +143,7 @@
;; Not sure if this makes sense.
(p/catch (fn [] nil))
(p/then (fn []
(rdom/unmount-component-at-node div)
(.unmount root)
;; Need to wait for reagent tick after unmount
;; for the ratom watches to be removed?
(let [ratoms-cleaned (p/deferred)]
Expand Down
Loading