reagent/atom REPL 的行为?

reagent/atom behaviour at REPL?

正在寻找 reagent/atom.

行为的说明

这是《Professional Clojure》第 5 章中的代码,带有图轮运行:

deps 中的试剂版本是 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])))

在 REPL 中,如果我将取消引用的 app-state 传递给项目板,我会得到一个错误:

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)

但是如果我通过普通 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"]]

如果我在 repl 打印出普通的 var app-state,它会打印出:

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}}}}}>

如果我在 repl 打印出 deref'd var app-state 它也会打印出来,但现在语法缩进:

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}}}}}

为什么第一个案例失败了? (如果某物是一个原子,我不应该期望总是取消引用它吗?)

在第一种情况下,您尝试 deref 两次,这就是它失败的原因。

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

意味着 project-board fn 现在收到一个映射作为参数,而不是原子。因此 (get-in @app-state ...) 失败,因为映射未实现 deref 协议。

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