製品の次の開発段階では、Clojuescript/Reagent の使用を開始しています。基本的に、アトムに対して単一の操作を行う方法が必要であり、次のようなメソッドを思い付きました: (def app-state (r/atom {}))
(defn select [index]
(swap! app-state
#(-> %
(assoc-in [:storyboard :pages 0 :layers 0 :children 0 :is_selected] true)
(assoc-in (GET ....)) ;; ajax call
(assoc-in [:selected :selected_tab] "tab_properties")
(assoc-in [:selected :page_object_cursor] [0])
)))
基本的に、スワップは、必要なすべての操作が連鎖する関数を取得します。それは素晴らしいです。ただし、エラーを処理する方法を検討しています。エラー用に別のアトムが必要です。
(def errors (r/atom []))
エラーが発生した場合は、アプリの状態をスワップせずに、それをキャプチャしてエラーに追加します (アプリケーションは最後の安定した状態のままです)。そのため、チェーンからのスローと try cactch を使用します。
(defn change-title [title]
(try
(swap! app-state
#(-> %
(assoc :text title)
((fn [state]
(throw "there is an error")))
))
(catch js/Object e
(swap! errors conj e))))
したがって、エラーがキャッチされ、@errors がそれを持っていることを期待します。しかし、現実は次のとおりです。
※見落としエラーあり
weather_app$core$change_title @ core.cljs?rel=14502666738018:59(匿名関数) @ core.cljs?rel=1450266738018:108executeDispatch @ react-with-addons.inc.js:3311SimpleEventPlugin.executeDispatch @ react-with-addons. inc.js:17428forEachEventDispatch @ react-with-addons.inc.js:3299executeDispatchesInOrder @ react-with-addons.inc.js:3320executeDispatchesAndRelease @ react-with-addons.inc.js:2693forEachAccumulated @ react-with-addons.inc. js:19430EventPluginHub.processEventQueue @ react-with-addons.inc.js:2900runEventQueueInBatch @ react-with-addons.inc.js:11217ReactEventEmitterMixin.handleTopLevel @ react-with-addons.inc.js:11243handleTopLevelImpl @ react-with-addons. inc.js:11329Mixin.perform @ react-with-addons.inc.js:18402ReactDefaultBatchingStrategy.batchedUpdates @ react-with-addons.inc.js:9669batchedUpdates @ react-with-addons.inc.js:16633ReactEventListener.dispatchEvent @ react-with-addons.inc.js:11423*