Bug Fix for undo tests

This commit is contained in:
Stuart Mitchell 2016-06-22 14:30:15 +12:00
parent addc55658e
commit 5c7d4ea3d1
1 changed files with 27 additions and 23 deletions

View File

@ -12,7 +12,7 @@
;; ;;
;; ;;
(def ^:private config (atom {:max-undos 50 ;; Maximum number of undo states maintained (def ^:private config (atom {:max-undos 50 ;; Maximum number of undo states maintained
:path []})) ;; undo and redos will apply to only this path within app-db :path nil})) ;; undo and redos will apply to only this path within app-db
(defn set-max-undos! (defn set-max-undos!
[n] [n]
(swap! config assoc :max-undos n)) (swap! config assoc :max-undos n))
@ -131,13 +131,19 @@
(defn- undo (defn- undo
[undos cur redos] [undos cur redos]
(let [u @undos (let [u @undos
r (cons @cur @redos)] r (cons @cur @redos)
path (undo-path-ks)]
(if path
(swap! cur assoc-in path (last u))
(reset! cur (last u)))
(reset! redos r)
(reset! undos (pop u))))
(.log js/console "in undo") (defn- undo-explain
(.log js/console cur) [undos cur redos]
(.log js/console (undo-path-ks)) (let [u @undos
(.log js/console (last u)) r (cons @cur @redos)]
(swap! cur assoc-in (undo-path-ks) (last u)) (reset! cur (last u))
(reset! redos r) (reset! redos r)
(reset! undos (pop u)))) (reset! undos (pop u))))
@ -146,7 +152,7 @@
[n] [n]
(when (and (pos? n) (undos?)) (when (and (pos? n) (undos?))
(undo undo-list app-db redo-list) (undo undo-list app-db redo-list)
(undo undo-explain-list app-explain redo-explain-list) (undo-explain undo-explain-list app-explain redo-explain-list)
(recur (dec n)))) (recur (dec n))))
(handlers/register-base ;; not a pure handler (handlers/register-base ;; not a pure handler
@ -160,10 +166,21 @@
(defn- redo (defn- redo
[undos cur redos]
(let [u (conj @undos @cur)
r @redos
path (undo-path-ks)]
(if path
(swap! cur assoc-in path (first r))
(reset! cur (first r)))
(reset! redos (rest r))
(reset! undos u)))
(defn- redo-explain
[undos cur redos] [undos cur redos]
(let [u (conj @undos @cur) (let [u (conj @undos @cur)
r @redos] r @redos]
(swap! cur assoc-in (undo-path-ks) (first r)) (reset! cur (first r))
(reset! redos (rest r)) (reset! redos (rest r))
(reset! undos u))) (reset! undos u)))
@ -172,7 +189,7 @@
[n] [n]
(when (and (pos? n) (redos?)) (when (and (pos? n) (redos?))
(redo undo-list app-db redo-list) (redo undo-list app-db redo-list)
(redo undo-explain-list app-explain redo-explain-list) (redo-explain undo-explain-list app-explain redo-explain-list)
(recur (dec n)))) (recur (dec n))))
(handlers/register-base ;; not a pure handler (handlers/register-base ;; not a pure handler
@ -216,16 +233,3 @@
(handler db event-vec))))) (handler db event-vec)))))
(def undoable (with-meta undoable_ {:re-frame-factory-name "undoable"})) (def undoable (with-meta undoable_ {:re-frame-factory-name "undoable"}))
;; middleware
(defn fsm-trigger
[trigger update-fn fsm-path]
(fn fsm-middleware
[handler]
(fn fsm-handler
[db event-v]
(let [new-db (handler db event-v)]
(update-fn new-db event-v trigger fsm-path))))) ;; think about access to event-v