├── .gitignore ├── LICENSE ├── README.md ├── project.clj ├── src └── reagent │ └── cursor.cljs └── test ├── cursor_impl.cljs ├── runtests.cljs ├── test.html └── testcursor.cljs /.gitignore: -------------------------------------------------------------------------------- 1 | pom.xml 2 | pom.xml.asc 3 | *jar 4 | /lib/ 5 | /classes/ 6 | /target/ 7 | /checkouts/ 8 | .lein-deps-sum 9 | .lein-repl-history 10 | .lein-plugins/ 11 | .lein-failures 12 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Eclipse Public License - v 1.0 2 | 3 | THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC 4 | LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM 5 | CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT. 6 | 7 | 1. DEFINITIONS 8 | 9 | "Contribution" means: 10 | 11 | a) in the case of the initial Contributor, the initial code and documentation 12 | distributed under this Agreement, and 13 | b) in the case of each subsequent Contributor: 14 | i) changes to the Program, and 15 | ii) additions to the Program; 16 | 17 | where such changes and/or additions to the Program originate from and are 18 | distributed by that particular Contributor. A Contribution 'originates' 19 | from a Contributor if it was added to the Program by such Contributor 20 | itself or anyone acting on such Contributor's behalf. Contributions do not 21 | include additions to the Program which: (i) are separate modules of 22 | software distributed in conjunction with the Program under their own 23 | license agreement, and (ii) are not derivative works of the Program. 24 | 25 | "Contributor" means any person or entity that distributes the Program. 26 | 27 | "Licensed Patents" mean patent claims licensable by a Contributor which are 28 | necessarily infringed by the use or sale of its Contribution alone or when 29 | combined with the Program. 30 | 31 | "Program" means the Contributions distributed in accordance with this 32 | Agreement. 33 | 34 | "Recipient" means anyone who receives the Program under this Agreement, 35 | including all Contributors. 36 | 37 | 2. GRANT OF RIGHTS 38 | a) Subject to the terms of this Agreement, each Contributor hereby grants 39 | Recipient a non-exclusive, worldwide, royalty-free copyright license to 40 | reproduce, prepare derivative works of, publicly display, publicly 41 | perform, distribute and sublicense the Contribution of such Contributor, 42 | if any, and such derivative works, in source code and object code form. 43 | b) Subject to the terms of this Agreement, each Contributor hereby grants 44 | Recipient a non-exclusive, worldwide, royalty-free patent license under 45 | Licensed Patents to make, use, sell, offer to sell, import and otherwise 46 | transfer the Contribution of such Contributor, if any, in source code and 47 | object code form. This patent license shall apply to the combination of 48 | the Contribution and the Program if, at the time the Contribution is 49 | added by the Contributor, such addition of the Contribution causes such 50 | combination to be covered by the Licensed Patents. The patent license 51 | shall not apply to any other combinations which include the Contribution. 52 | No hardware per se is licensed hereunder. 53 | c) Recipient understands that although each Contributor grants the licenses 54 | to its Contributions set forth herein, no assurances are provided by any 55 | Contributor that the Program does not infringe the patent or other 56 | intellectual property rights of any other entity. Each Contributor 57 | disclaims any liability to Recipient for claims brought by any other 58 | entity based on infringement of intellectual property rights or 59 | otherwise. As a condition to exercising the rights and licenses granted 60 | hereunder, each Recipient hereby assumes sole responsibility to secure 61 | any other intellectual property rights needed, if any. For example, if a 62 | third party patent license is required to allow Recipient to distribute 63 | the Program, it is Recipient's responsibility to acquire that license 64 | before distributing the Program. 65 | d) Each Contributor represents that to its knowledge it has sufficient 66 | copyright rights in its Contribution, if any, to grant the copyright 67 | license set forth in this Agreement. 68 | 69 | 3. REQUIREMENTS 70 | 71 | A Contributor may choose to distribute the Program in object code form under 72 | its own license agreement, provided that: 73 | 74 | a) it complies with the terms and conditions of this Agreement; and 75 | b) its license agreement: 76 | i) effectively disclaims on behalf of all Contributors all warranties 77 | and conditions, express and implied, including warranties or 78 | conditions of title and non-infringement, and implied warranties or 79 | conditions of merchantability and fitness for a particular purpose; 80 | ii) effectively excludes on behalf of all Contributors all liability for 81 | damages, including direct, indirect, special, incidental and 82 | consequential damages, such as lost profits; 83 | iii) states that any provisions which differ from this Agreement are 84 | offered by that Contributor alone and not by any other party; and 85 | iv) states that source code for the Program is available from such 86 | Contributor, and informs licensees how to obtain it in a reasonable 87 | manner on or through a medium customarily used for software exchange. 88 | 89 | When the Program is made available in source code form: 90 | 91 | a) it must be made available under this Agreement; and 92 | b) a copy of this Agreement must be included with each copy of the Program. 93 | Contributors may not remove or alter any copyright notices contained 94 | within the Program. 95 | 96 | Each Contributor must identify itself as the originator of its Contribution, 97 | if 98 | any, in a manner that reasonably allows subsequent Recipients to identify the 99 | originator of the Contribution. 100 | 101 | 4. COMMERCIAL DISTRIBUTION 102 | 103 | Commercial distributors of software may accept certain responsibilities with 104 | respect to end users, business partners and the like. While this license is 105 | intended to facilitate the commercial use of the Program, the Contributor who 106 | includes the Program in a commercial product offering should do so in a manner 107 | which does not create potential liability for other Contributors. Therefore, 108 | if a Contributor includes the Program in a commercial product offering, such 109 | Contributor ("Commercial Contributor") hereby agrees to defend and indemnify 110 | every other Contributor ("Indemnified Contributor") against any losses, 111 | damages and costs (collectively "Losses") arising from claims, lawsuits and 112 | other legal actions brought by a third party against the Indemnified 113 | Contributor to the extent caused by the acts or omissions of such Commercial 114 | Contributor in connection with its distribution of the Program in a commercial 115 | product offering. The obligations in this section do not apply to any claims 116 | or Losses relating to any actual or alleged intellectual property 117 | infringement. In order to qualify, an Indemnified Contributor must: 118 | a) promptly notify the Commercial Contributor in writing of such claim, and 119 | b) allow the Commercial Contributor to control, and cooperate with the 120 | Commercial Contributor in, the defense and any related settlement 121 | negotiations. The Indemnified Contributor may participate in any such claim at 122 | its own expense. 123 | 124 | For example, a Contributor might include the Program in a commercial product 125 | offering, Product X. That Contributor is then a Commercial Contributor. If 126 | that Commercial Contributor then makes performance claims, or offers 127 | warranties related to Product X, those performance claims and warranties are 128 | such Commercial Contributor's responsibility alone. Under this section, the 129 | Commercial Contributor would have to defend claims against the other 130 | Contributors related to those performance claims and warranties, and if a 131 | court requires any other Contributor to pay any damages as a result, the 132 | Commercial Contributor must pay those damages. 133 | 134 | 5. NO WARRANTY 135 | 136 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS PROVIDED ON AN 137 | "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR 138 | IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, 139 | NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each 140 | Recipient is solely responsible for determining the appropriateness of using 141 | and distributing the Program and assumes all risks associated with its 142 | exercise of rights under this Agreement , including but not limited to the 143 | risks and costs of program errors, compliance with applicable laws, damage to 144 | or loss of data, programs or equipment, and unavailability or interruption of 145 | operations. 146 | 147 | 6. DISCLAIMER OF LIABILITY 148 | 149 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT NOR ANY 150 | CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, 151 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION 152 | LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 153 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 154 | ARISING IN ANY WAY OUT OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE 155 | EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY 156 | OF SUCH DAMAGES. 157 | 158 | 7. GENERAL 159 | 160 | If any provision of this Agreement is invalid or unenforceable under 161 | applicable law, it shall not affect the validity or enforceability of the 162 | remainder of the terms of this Agreement, and without further action by the 163 | parties hereto, such provision shall be reformed to the minimum extent 164 | necessary to make such provision valid and enforceable. 165 | 166 | If Recipient institutes patent litigation against any entity (including a 167 | cross-claim or counterclaim in a lawsuit) alleging that the Program itself 168 | (excluding combinations of the Program with other software or hardware) 169 | infringes such Recipient's patent(s), then such Recipient's rights granted 170 | under Section 2(b) shall terminate as of the date such litigation is filed. 171 | 172 | All Recipient's rights under this Agreement shall terminate if it fails to 173 | comply with any of the material terms or conditions of this Agreement and does 174 | not cure such failure in a reasonable period of time after becoming aware of 175 | such noncompliance. If all Recipient's rights under this Agreement terminate, 176 | Recipient agrees to cease use and distribution of the Program as soon as 177 | reasonably practicable. However, Recipient's obligations under this Agreement 178 | and any licenses granted by Recipient relating to the Program shall continue 179 | and survive. 180 | 181 | Everyone is permitted to copy and distribute copies of this Agreement, but in 182 | order to avoid inconsistency the Agreement is copyrighted and may only be 183 | modified in the following manner. The Agreement Steward reserves the right to 184 | publish new versions (including revisions) of this Agreement from time to 185 | time. No one other than the Agreement Steward has the right to modify this 186 | Agreement. The Eclipse Foundation is the initial Agreement Steward. The 187 | Eclipse Foundation may assign the responsibility to serve as the Agreement 188 | Steward to a suitable separate entity. Each new version of the Agreement will 189 | be given a distinguishing version number. The Program (including 190 | Contributions) may always be distributed subject to the version of the 191 | Agreement under which it was received. In addition, after a new version of the 192 | Agreement is published, Contributor may elect to distribute the Program 193 | (including its Contributions) under the new version. Except as expressly 194 | stated in Sections 2(a) and 2(b) above, Recipient receives no rights or 195 | licenses to the intellectual property of any Contributor under this Agreement, 196 | whether expressly, by implication, estoppel or otherwise. All rights in the 197 | Program not expressly granted under this Agreement are reserved. 198 | 199 | This Agreement is governed by the laws of the State of New York and the 200 | intellectual property laws of the United States of America. No party to this 201 | Agreement will bring a legal action under this Agreement more than one year 202 | after the cause of action arose. Each party waives its rights to a jury trial in 203 | any resulting litigation. 204 | 205 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Deprecated 2 | ============== 3 | 4 | As part of the migration to the new **reagent-project** organization, an earlier version of this **cursors** library was folded into Reagent's core. As part of the Reagent 0.5.0 release, that implementation of cursors has been substantially enhanced (both in performance and in generalization) so this standalone cursor library should be considered **deprecated**. 5 | 6 | Note that the argument order in Reagent core is different - atom first, path second - so switching from this library to Reagent core will involve code changes but they are straightforward. 7 | 8 | Cursors 9 | ============== 10 | 11 | Cursors can be seen as a kind of *pointer* to a particular part of an 12 | atom, which behaves exactly like a normal atom. This means that you 13 | use the same functions you would on an atom (`reset!`, `swap!`, 14 | `deref`, `add-watch`, etc) but affect only the part you are interested 15 | in. 16 | 17 | This enables you to create reusable functions and components by 18 | abstracting away complex paths and getter/setter functions. 19 | 20 | ```clj 21 | ;; what was... 22 | (swap! my-atom update-in [:some :path :that :might :be :quite :deep] my-fn) 23 | 24 | ;; ...can now become 25 | 26 | (swap! my-cursor my-fn) 27 | 28 | ;; Notice that the path is no longer hardcoded; it could be a simple 29 | ;; atom, or a cursor pointing to the 10th level of a complex nested 30 | ;; hashmap. 31 | 32 | ;; How about associating a value into the nested structure? No 33 | ;; problem! Just `reset!` the cursor: 34 | 35 | (reset! my-cursor "my-new-value") 36 | 37 | ;; Now just deref it: 38 | 39 | @my-cursor 40 | 41 | => "my-new-value" 42 | 43 | ``` 44 | 45 | Usage 46 | ----- 47 | 48 | Add `[reagent/reagent-cursor "0.1.2"]` to `:dependencies` in `project.clj`. 49 | 50 | In your Reagent application `(:require [reagent.cursor :as rc])`. 51 | 52 | There are two main functions available to create cursors: `cursor` and `cur`. 53 | 54 | ## cursor 55 | 56 | `cursor` has two arities. 57 | 58 | When given a single argument (a path), it returns a function that can 59 | create a cursor when given an atom. *Useful to create mutliple cursors 60 | with the same path.* 61 | 62 | 63 | ```clj 64 | (def my-custom-cursor-fn (rc/cursor [:some :arbitrary :path])) 65 | 66 | (map my-custom-cursor-fn [atom1 atom2 atom3]) 67 | 68 | ;; this will return a collection of [cursor1 cursor2 cursor3] 69 | ``` 70 | 71 | When given two arguments, `cursor` will return a cursor. 72 | 73 | ```clj 74 | (def c1 (rc/cursor [:some :arbitrary :path] atom1)) 75 | 76 | ``` 77 | 78 | 79 | ## cur 80 | 81 | 82 | `cur` is the little brother of `cursor`. It will *only* accept 2 83 | arguments (the atom and a path), but is guaranteed to return a 84 | cursor. 85 | 86 | Note that the atom argument is placed on the left, allowing 87 | you to use a thread-first macro (`cursor` requires a thread-last 88 | macro). 89 | 90 | ```clj 91 | 92 | (-> my-atom 93 | (rc/cur [:some :path]) ;; <---- create the cursor 94 | (add-watch :my-watch #(println "updated!")) 95 | (historian/record! :my-state) 96 | (ls/local-storage :my-state)) 97 | 98 | ``` 99 | 100 | License 101 | ------- 102 | 103 | Copyright (c) 2014 Sean Corfield 104 | 105 | Distributed under the Eclipse Public License, the same as Clojure. 106 | -------------------------------------------------------------------------------- /project.clj: -------------------------------------------------------------------------------- 1 | (defproject reagent/reagent-cursor "0.1.2" 2 | :description "Provide Om-inspired cursors for Reagent" 3 | :url "http://github.com/reagent-project/reagent-cursor" 4 | :license {:name "Eclipse Public License" 5 | :url "http://www.eclipse.org/legal/epl-v10.html"} 6 | :dependencies [[org.clojure/clojure "1.7.0-alpha4"] 7 | [org.clojure/clojurescript "0.0-2356"] 8 | [reagent "0.4.3"]] 9 | :plugins [[lein-cljsbuild "1.0.3"] 10 | [com.cemerick/clojurescript.test "0.2.2"]] 11 | :profiles {:prod {:cljsbuild 12 | {:builds 13 | {:client {:compiler 14 | {:optimizations :advanced 15 | :elide-asserts true 16 | :preamble ^:replace ["reagent/react.min.js"] 17 | :pretty-print false}}}}} 18 | :test {:cljsbuild 19 | {:builds 20 | {:client {:source-paths ^:replace 21 | ["test" "src"]}}}} 22 | :srcmap {:cljsbuild 23 | {:builds 24 | {:client 25 | {:compiler 26 | {:source-map "target/cljs-client.js.map" 27 | :source-map-path "client"}}}}}} 28 | :source-paths ["src"] 29 | :cljsbuild 30 | {:builds 31 | {:client {:source-paths ["src"] 32 | :compiler 33 | {:preamble ["reagent/react.js"] 34 | :output-dir "target/client" 35 | :output-to "target/cljs-client.js" 36 | :pretty-print true}}}}) 37 | -------------------------------------------------------------------------------- /src/reagent/cursor.cljs: -------------------------------------------------------------------------------- 1 | ;; copyright (c) 2014 Sean Corfield 2 | 3 | (ns reagent.cursor 4 | "Optional cursor support for Reagent. 5 | 6 | (:require [reagent.cursor :as rc]) 7 | 8 | (rc/cursor [:path :to :some :item] some-ratom) 9 | 10 | The cursor behaves in all ways like a regular Reagent atom except 11 | that it dereferences to the specified path inside the atom 12 | and updates to it, cause updates to the atom relative to the 13 | specified path.") 14 | 15 | ;; Implementation based on RAtom by delegation 16 | 17 | 18 | (deftype RCursor [path ratom] 19 | IAtom 20 | 21 | IEquiv 22 | (-equiv [o other] (identical? o other)) 23 | 24 | IDeref 25 | (-deref [this] 26 | (get-in @ratom path)) 27 | 28 | IReset 29 | (-reset! [a new-value] 30 | (swap! ratom assoc-in path new-value) 31 | new-value) 32 | 33 | ISwap 34 | (-swap! [a f] 35 | (-> (swap! ratom update-in path f) 36 | (get-in path))) 37 | (-swap! [a f x] 38 | (-> (swap! ratom update-in path f x) 39 | (get-in path))) 40 | (-swap! [a f x y] 41 | (-> (swap! ratom update-in path f x y) 42 | (get-in path))) 43 | (-swap! [a f x y more] 44 | (-> (swap! ratom update-in path 45 | (fn [v] (apply f v x y more))) 46 | (get-in path))) 47 | 48 | IMeta 49 | (-meta [_] 50 | (-meta ratom)) 51 | 52 | IPrintWithWriter 53 | (-pr-writer [a writer opts] 54 | ;; not sure about how this should be implemented? 55 | ;; should it print as an atom focused on the appropriate part of 56 | ;; the ratom - (pr-writer (get-in @ratom path)) - or should it be 57 | ;; a completely separate type? and do we need a reader for it? 58 | 59 | ;; Until further investigation, it should simply be REPL friendly. 60 | (-write writer "#")) 65 | 66 | IWatchable 67 | (-notify-watches [this oldval newval] 68 | (-notify-watches ratom oldval newval)) 69 | (-add-watch [this key f] 70 | (-add-watch ratom [path key] 71 | (fn [k r o n] 72 | (f key 73 | this 74 | (get-in o path) 75 | (get-in n path))))) 76 | (-remove-watch [this key] 77 | (-remove-watch ratom [path key])) 78 | 79 | IHash 80 | (-hash [this] (goog/getUid this))) 81 | 82 | ;; RCursor 83 | 84 | (defn cursor 85 | "Provide a cursor into an atom. 86 | 87 | When called with a single argument, return a function that will 88 | create a cursor given an atom. 89 | 90 | Behaves like an atom but focuses updates and derefs to the specified 91 | path within the wrapped atom. e.g., 92 | (let [c (cursor [:nested :content] ra)] 93 | ... @c ;; equivalent to (get-in @ra [:nested :content]) 94 | ... (reset! c 42) ;; equivalent to (swap! ra assoc-in [:nested :content] 42) 95 | ... (swap! c inc) ;; equivalence to (swap! ra update-in [:nested :content] inc) 96 | )" 97 | ([path] (fn [a] (cursor path a))) 98 | ([path a] 99 | (if (seq path) 100 | (RCursor. path a) 101 | a))) 102 | 103 | 104 | (defn cur 105 | "Create a cursor. 106 | 107 | Behaves like a normal atom for the value at the specified path." 108 | [a path] 109 | (cursor path a)) 110 | -------------------------------------------------------------------------------- /test/cursor_impl.cljs: -------------------------------------------------------------------------------- 1 | ;; copyright (c) 2014 Frozenlock 2 | (ns cursor-impl 3 | (:require-macros [cemerick.cljs.test 4 | :refer (is deftest with-test run-tests testing)]) 5 | (:require [cemerick.cljs.test :as t] 6 | [reagent.cursor :as c])) 7 | 8 | ;; Here we test if the cursors act as a normal atom 9 | 10 | (deftest values 11 | (let [test-atom (atom {:a {:b {:c {:d 1}}}}) 12 | test-cursor (c/cur test-atom [:a :b :c :d]) 13 | test-cursor2 (c/cur test-atom [])] ;; nasty edge case 14 | 15 | (is (= cljs.core/Atom (type test-cursor2))) 16 | 17 | ;; get the initial values 18 | (is (= (get-in @test-atom [:a :b :c :d]) 19 | @test-cursor)) 20 | 21 | (is (= (get-in @test-atom []) 22 | @test-cursor2)) 23 | 24 | ;; now we update the cursor with a reset 25 | (reset! test-cursor 2) 26 | (is (= @test-cursor 2)) 27 | (is (= (get-in @test-atom [:a :b :c :d]) 2)) 28 | 29 | (reset! test-cursor2 3) 30 | (is (= @test-cursor2 3)) 31 | (is (= @test-atom 3)) 32 | (reset! test-atom {:a {:b {:c {:d 1}}}}) ;; restore test-atom 33 | 34 | ;; swap 35 | (reset! test-cursor {}) ;; empty map 36 | (swap! test-cursor assoc :z 3) 37 | (is (= @test-cursor {:z 3})) 38 | (is (= (get-in @test-atom [:a :b :c :d]) 39 | {:z 3})) 40 | 41 | (reset! test-cursor2 {}) ;; empty map 42 | (swap! test-cursor2 assoc :z 3) 43 | (is (= @test-cursor2 {:z 3})) 44 | (is (= (get-in @test-atom []) 45 | {:z 3})))) 46 | 47 | 48 | (deftest atom-behaviors 49 | (let [test-atom (atom {:a {:b {:c {:d 1}}}}) 50 | test-cursor (c/cur test-atom [:a :b :c :d]) 51 | witness (atom nil)] 52 | ;; per the description, reset! should return the new values 53 | (is (= {} 54 | (reset! test-cursor {}))) 55 | 56 | ;; per the description, swap! should return the new values 57 | (is (= {:z [1 2 3]} 58 | (swap! test-cursor assoc :z [1 2 3]))) 59 | 60 | ;; watches should behave like with a normal atom 61 | (reset! test-cursor "old") 62 | (add-watch test-cursor :w #(reset! witness {:key %1 :ref %2 :old %3 :new %4})) 63 | (reset! test-cursor "new") ;; this should trigger the watch function 64 | (is (= (:key @witness) :w)) 65 | (is (= (:ref @witness) test-cursor)) 66 | (is (= (:old @witness) "old")) 67 | (is (= (:new @witness) "new")) 68 | ;; can we remove the watch? 69 | (remove-watch test-cursor :w) 70 | (reset! test-cursor "removed") 71 | (is (= (:new @witness) "new")) ;; shouldn't have changed 72 | )) 73 | -------------------------------------------------------------------------------- /test/runtests.cljs: -------------------------------------------------------------------------------- 1 | ;; lifted from main reagent project and simplified a bit 2 | 3 | (ns runtests 4 | (:require [reagent.core :as reagent :refer [atom]] 5 | [reagent.debug :refer-macros [println]] 6 | [cemerick.cljs.test :as t])) 7 | 8 | (enable-console-print!) 9 | 10 | (def test-results (atom nil)) 11 | 12 | (defn test-output [] 13 | (let [res @test-results] 14 | [:div {:style {:margin-top "40px"}} 15 | (if-not res 16 | [:div "waiting for tests to run"] 17 | [:div 18 | [:p (str "Ran " (:test res) " tests containing " 19 | (+ (:pass res) (:fail res) (:error res)) 20 | " assertions.")] 21 | [:p (:fail res) " failures, " (:error res) " errors."]])])) 22 | 23 | (defn test-output-mini [] 24 | (let [res @test-results] 25 | (if res 26 | (if (zero? (+ (:fail res) (:error res))) 27 | [:div "Tests ok"] 28 | [test-output]) 29 | [:div "."]))) 30 | 31 | (defn test-demo [] 32 | [:div 33 | [test-output]]) 34 | 35 | (defn ^:export mounttests [] 36 | (reagent/render-component (fn [] [test-demo]) 37 | (.-body js/document))) 38 | 39 | (defn ^:export run-all-tests [] 40 | (println "-----------------------------------------") 41 | (try 42 | (reset! test-results (t/run-all-tests)) 43 | (catch js/Object e 44 | (do 45 | (println "Testrun failed\n" e "\n" (.-stack e)) 46 | (reset! test-results {:error e})))) 47 | (println "-----------------------------------------")) 48 | 49 | (if reagent/is-client 50 | (do 51 | (reset! test-results nil) 52 | (js/setTimeout run-all-tests 1000)) 53 | (run-all-tests)) 54 | -------------------------------------------------------------------------------- /test/test.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Testing Reagent Cursor 5 | 8 | 9 | 10 |

This will show tests when compiled

11 | 12 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /test/testcursor.cljs: -------------------------------------------------------------------------------- 1 | ;; copyright (c) 2014 Sean Corfield 2 | ;; 3 | ;; based directly on @holmsand tests for ratom 4 | 5 | (ns testcursor 6 | (:require-macros [cemerick.cljs.test 7 | :refer (is deftest with-test run-tests testing)] 8 | [reagent.ratom :refer [run! reaction]] 9 | [reagent.debug :refer [dbg]]) 10 | (:require [cemerick.cljs.test :as t] 11 | [reagent.ratom :as rv])) 12 | 13 | ;; this repeats all the atom tests but using cursors instead 14 | 15 | (set! rv/debug true) 16 | 17 | (defn running [] (rv/running)) 18 | (defn dispose [v] (rv/dispose! v)) 19 | 20 | (defn ratom-perf [] 21 | (dbg "ratom-perf") 22 | (let [a (rv/atom {}) 23 | mid (reaction (inc @a)) 24 | res (run! 25 | (inc @mid))] 26 | (time (dotimes [x 100000] 27 | (swap! a inc))) 28 | (dispose res))) 29 | 30 | ;; (ratom-perf) 31 | 32 | (deftest basic-cursor 33 | (let [runs (running) 34 | start-base (rv/atom {:a {:b {:c 0}}}) 35 | start (rv/cursor [:a :b :c] start-base) 36 | sv (reaction @start) 37 | comp (reaction @sv (+ 2 @sv)) 38 | c2 (reaction (inc @comp)) 39 | count (rv/atom 0) 40 | out (rv/atom 0) 41 | res (reaction 42 | (swap! count inc) 43 | @sv @c2 @comp) 44 | const (run! 45 | (reset! out @res))] 46 | (is (= @count 1) "constrain ran") 47 | (is (= @out 2)) 48 | (reset! start 1) 49 | (is (= @out 3)) 50 | (is (= @count 4)) 51 | (dispose const) 52 | (is (= @start-base {:a {:b {:c 1}}})) 53 | (is (= (running) runs)))) 54 | 55 | (deftest double-dependency 56 | (let [runs (running) 57 | start-base (rv/atom {:a {:b {:c 0}}}) 58 | start (rv/cursor [:a :b :c] start-base) 59 | c3-count (rv/atom 0) 60 | c1 (reaction @start 1) 61 | c2 (reaction @start) 62 | c3 (rv/make-reaction 63 | (fn [] 64 | (swap! c3-count inc) 65 | (+ @c1 @c2)) 66 | :auto-run true)] 67 | (is (= @c3-count 0)) 68 | (is (= @c3 1)) 69 | (is (= @c3-count 1) "t1") 70 | (swap! start inc) 71 | (is (= @c3-count 2) "t2") 72 | (is (= @c3 2)) 73 | (is (= @c3-count 2) "t3") 74 | (is (= @start-base {:a {:b {:c 1}}})) 75 | (dispose c3) 76 | (is (= (running) runs)))) 77 | 78 | (deftest test-from-reflex 79 | (let [runs (running)] 80 | (let [!ctr-base (rv/atom {:x {:y 0 :z 0}}) 81 | !counter (rv/cursor [:x :y] !ctr-base) 82 | !signal (rv/atom "All I do is change") 83 | co (run! 84 | ;;when I change... 85 | @!signal 86 | ;;update the counter 87 | (swap! !counter inc))] 88 | (is (= 1 @!counter) "Constraint run on init") 89 | (reset! !signal "foo") 90 | (is (= 2 @!counter) 91 | "Counter auto updated") 92 | (is (= @!ctr-base {:x {:y 2 :z 0}})) 93 | (dispose co)) 94 | (let [!x-base (rv/atom {:a {:b 0 :c {:d 0}}}) 95 | !x (rv/cursor [:a :c :d] !x-base) 96 | !co (rv/make-reaction #(inc @!x) :auto-run true)] 97 | (is (= 1 @!co) "CO has correct value on first deref") 98 | (swap! !x inc) 99 | (is (= 2 @!co) "CO auto-updates") 100 | (is (= {:a {:b 0 :c {:d 1}}} @!x-base)) 101 | (dispose !co)) 102 | (is (= runs (running))))) 103 | 104 | 105 | (deftest test-unsubscribe 106 | (dotimes [x 10] 107 | (let [runs (running) 108 | a-base (rv/atom {:test {:unsubscribe 0 :value 42}}) 109 | a (rv/cursor [:test :unsubscribe] a-base) 110 | a1 (reaction (inc @a)) 111 | a2 (reaction @a) 112 | b-changed (rv/atom 0) 113 | c-changed (rv/atom 0) 114 | b (reaction 115 | (swap! b-changed inc) 116 | (inc @a1)) 117 | c (reaction 118 | (swap! c-changed inc) 119 | (+ 10 @a2)) 120 | res (run! 121 | (if (< @a2 1) @b @c))] 122 | (is (= @res (+ 2 @a))) 123 | (is (= @b-changed 1)) 124 | (is (= @c-changed 0)) 125 | 126 | (reset! a -1) 127 | (is (= @res (+ 2 @a))) 128 | (is (= @b-changed 2)) 129 | (is (= @c-changed 0)) 130 | (is (= @a-base {:test {:unsubscribe -1 :value 42}})) 131 | 132 | (reset! a 2) 133 | (is (= @res (+ 10 @a))) 134 | (is (<= 2 @b-changed 3)) 135 | (is (= @c-changed 1)) 136 | (is (= @a-base {:test {:unsubscribe 2 :value 42}})) 137 | 138 | (reset! a 3) 139 | (is (= @res (+ 10 @a))) 140 | (is (<= 2 @b-changed 3)) 141 | (is (= @c-changed 2)) 142 | (is (= @a-base {:test {:unsubscribe 3 :value 42}})) 143 | 144 | (reset! a 3) 145 | (is (= @res (+ 10 @a))) 146 | (is (<= 2 @b-changed 3)) 147 | (is (= @c-changed 2)) 148 | (is (= @a-base {:test {:unsubscribe 3 :value 42}})) 149 | 150 | (reset! a -1) 151 | (is (= @res (+ 2 @a))) 152 | (is (= @a-base {:test {:unsubscribe -1 :value 42}})) 153 | (dispose res) 154 | (is (= runs (running)))))) 155 | 156 | (deftest maybe-broken 157 | (let [runs (running)] 158 | (let [runs (running) 159 | a-base (rv/atom {:a {:b 0 :c {:d 42}}}) 160 | a (rv/cursor [:a :b] a-base) 161 | b (reaction (inc @a)) 162 | c (reaction (dec @a)) 163 | d (reaction (str @b)) 164 | res (rv/atom 0) 165 | cs (run! 166 | (reset! res @d))] 167 | (is (= @res "1")) 168 | (dispose cs)) 169 | ;; should be broken according to https://github.com/lynaghk/reflex/issues/1 170 | ;; but isnt 171 | (let [a-base (rv/atom {:a 0}) 172 | a (rv/cursor [:a] a-base) 173 | b (reaction (inc @a)) 174 | c (reaction (dec @a)) 175 | d (run! [@b @c])] 176 | (is (= @d [1 -1])) 177 | (dispose d)) 178 | (let [a-base (rv/atom 0) 179 | a (rv/cursor [] a-base) 180 | b (reaction (inc @a)) 181 | c (reaction (dec @a)) 182 | d (run! [@b @c]) 183 | res (rv/atom 0)] 184 | (is (= @d [1 -1])) 185 | (let [e (run! (reset! res @d))] 186 | (is (= @res [1 -1])) 187 | (dispose e)) 188 | (dispose d)) 189 | (is (= runs (running))))) 190 | 191 | (deftest test-dispose 192 | (dotimes [x 10] 193 | (let [runs (running) 194 | a-base (rv/atom {:a 0 :b 0}) 195 | a (rv/cursor [:a] a-base) 196 | disposed (rv/atom nil) 197 | disposed-c (rv/atom nil) 198 | disposed-cns (rv/atom nil) 199 | count-b (rv/atom 0) 200 | b (rv/make-reaction (fn [] 201 | (swap! count-b inc) 202 | (inc @a)) 203 | :on-dispose #(reset! disposed true)) 204 | c (rv/make-reaction #(if (< @a 1) (inc @b) (dec @a)) 205 | :on-dispose #(reset! disposed-c true)) 206 | res (rv/atom nil) 207 | cns (rv/make-reaction #(reset! res @c) 208 | :auto-run true 209 | :on-dispose #(reset! disposed-cns true))] 210 | @cns 211 | (is (= @res 2)) 212 | (is (= (+ 3 runs) (running))) 213 | (is (= @count-b 1)) 214 | (is (= {:a 0 :b 0} @a-base)) 215 | (reset! a -1) 216 | (is (= @res 1)) 217 | (is (= @disposed nil)) 218 | (is (= @count-b 2)) 219 | (is (= (+ 3 runs) (running)) "still running") 220 | (is (= {:a -1 :b 0} @a-base)) 221 | (reset! a 2) 222 | (is (= @res 1)) 223 | (is (= @disposed true)) 224 | (is (= (+ 2 runs) (running)) "less running count") 225 | (is (= {:a 2 :b 0} @a-base)) 226 | 227 | (reset! disposed nil) 228 | (reset! a -1) 229 | ;; This fails sometimes on node. I have no idea why. 230 | (is (= 1 @res) "should be one again") 231 | (is (= @disposed nil)) 232 | (is (= {:a -1 :b 0} @a-base)) 233 | (reset! a 2) 234 | (is (= @res 1)) 235 | (is (= @disposed true)) 236 | (dispose cns) 237 | (is (= @disposed-c true)) 238 | (is (= @disposed-cns true)) 239 | (is (= {:a 2 :b 0} @a-base)) 240 | (is (= runs (running)))))) 241 | 242 | (deftest test-on-set 243 | (let [runs (running) 244 | a-base (rv/atom {:set 0}) 245 | a (rv/cursor [:set] a-base) 246 | b (rv/make-reaction #(+ 5 @a) 247 | :auto-run true 248 | :on-set (fn [oldv newv] 249 | (reset! a (+ 10 newv))))] 250 | @b 251 | (is (= 5 @b)) 252 | (is (= {:set 0} @a-base)) 253 | (reset! a 1) 254 | (is (= 6 @b)) 255 | (is (= {:set 1} @a-base)) 256 | (reset! b 1) 257 | (is (= 11 @a)) 258 | (is (= 16 @b)) 259 | (dispose b) 260 | (is (= {:set 11} @a-base)) 261 | (is (= runs (running))))) 262 | 263 | --------------------------------------------------------------------------------