Search code examples
clojurescriptreagent

reagent/atom behaviour at REPL?


Looking for clarification of behaviour of reagent/atom.

This is code from the book Professional Clojure, Chapter 5, with figwheel running:

reagent version in deps is 0.8.1

(ns whip.core
    (:require [reagent.core :as reagent :refer [atom]]))

(defonce app-state                                          
  (reagent/atom
    {:projects                
     {"aaa"                   
      {:title "Build Whip"
       :stories
       {1 {:title "Design a data model for projects and stories"    
           :status "done"                                           
           :order 1}                                                
        2 {:title "Create a story title entry form"                 
           :order 2}                                                
        3 {:title "Implement a way to finish stories"
           :order 3}}}}}))

(defn project-board [app-state project-id]                          
  (into [:ul]
    (for [[id {:keys [title]}] (get-in @app-state [:projects project-id :stories])]
      [:li title])))

At the REPL, If I pass deref'd app-state to project-board I get an error:

dev:whip.core=> (project-board @app-state "aaa")
#object[Error Error: No protocol method IDeref.-deref defined for type cljs.core/PersistentArrayMap: {:projects {"aaa" {:title "Build Whip", :stories {1 {:title "Design a data model for projects and stories", :status "done", :order 1}, 2 {:title "Create a story title entry form", :order 2}, 3 {:title "Implement a way to finish stories", :order 3}}}}}]
   cljs.core/missing-protocol (jar:file:/Users/m/.m2/repository/org/clojure/clojurescript/1.10.520/clojurescript-1.10.520.jar!/cljs/core.cljs:316:4)
   cljs.core/-deref (jar:file:/Users/m/.m2/repository/org/clojure/clojurescript/1.10.520/clojurescript-1.10.520.jar!/cljs/core.cljs:671:1)
   cljs$core$deref (jar:file:/Users/m/.m2/repository/org/clojure/clojurescript/1.10.520/clojurescript-1.10.520.jar!/cljs/core.cljs:1452:4)
   whip.core.project_board_reagentRender (file:/Users/m/clj-pro-clojure/code-and-notes/whip/src/whip/core.cljs:62:3)

But If I pass ordinary var app-state:

dev:whip.core=> (project-board app-state "aaa")
[:ul
 [:li "Design a data model for projects and stories"]
 [:li "Create a story title entry form"]
 [:li "Implement a way to finish stories"]]

If I print out ordinary var app-state at repl it prints out:

dev:whip.core=> app-state
#<Atom: {:projects {"aaa" {:title "Build Whip", :stories {1 {:title "Design a data model for projects and stories", :status "done", :order 1}, 2 {:title "Create a story title entry form", :order 2}, 3 {:title "Implement a way to finish stories", :order 3}}}}}>

If I print out deref'd var app-state at repl it also prints out, but now syntax indented:

dev:whip.core=> @app-state
{:projects
 {"aaa"
  {:title "Build Whip",
   :stories
   {1
    {:title "Design a data model for projects and stories",
     :status "done",
     :order 1},
    2 {:title "Create a story title entry form", :order 2},
    3 {:title "Implement a way to finish stories", :order 3}}}}}

Why does the first case fail? (if something is an atom should I not expect always to deref it?)


Solution

  • In the first case you are trying to deref twice, which is why it fails.

    (project-board @app-state "aaa")
    

    means that the project-board fn now received a map as an argument, not the atom. So the (get-in @app-state ...) fails since maps to not implement the deref protocol.

    No protocol method IDeref.-deref defined for type cljs.core/PersistentArrayMap