关于快速开发的构思
API
CLJC
提供的API.
defquery 定义query
(defquery <-stack-detail {:db/id s/Int :size s/Int :author s/Keyword})
defcommand 定义command
(defcommand publish-stack-> {:stack/content s/Str :stack/title s/Str})
CLJS
需要提供的配置.
- get, 用来发送query/fetch, query/subscribe请求.
- post, 用来发送command/dispatch请求.
- ws-handler, 用来处理update/push, command/sync.
(let [{:keys [ws-handler] (setup {:get get-fn :post post-fn})}] ;; Start Websocket loop (sente/start-chsk-router! ch-recv ws-handler))
提供的API.
- dispatch 发送command/dispatch. POST请求到服务器. 响应时同步command的状态.
- fetch 发送query/fetch. GET请求到服务器, 返回查询结果.
- subscribe 发送query/subscribe. GET请求到服务器, 返回查询结果.
;; Dispatch Command (dispatch publish-stack-> {:stack/content "Clojure is awesome!" :stack/title "Hello"}) ;; Fetch Query (fetch <-stack-detail {:db/id 10 :size 50 :author :all}) ;; Subscribe Query (subscribe <-stack-detail {:db/id 10 :size 30 :author :all})
CLJ
需要提供的配置.
- get-handler, 用来对接query/fetch
- post-handler, 用来对接command/dispatch请求.
- ws-send, 用来推送update/push和command/sync.
(let [{:keys [get-handler post-handler tx-handler sync-command]} (setup {:ws-send ws-send})] (defroute route (GET "/api" get-handler) (POST "/api" post-handler)))
回调
- register-query-handler
- register-command-handler
(register-query-handler <-stack-detail {:query (fn [env params] ;; do sth {:value xxx} ) :query-tx (fn [env params] ;; do sth {:delta xxx} )}) (register-command-handler publish-stack-> (fn [env params] ;; do sth {:status xxx} )) ;; Other Situations. (sync-command publish-stack-> :finish)