@@ -4,15 +4,14 @@ import qualified Data.Array as A
44import qualified Data.String as S
55import Data.Foldable
66import Data.Maybe
7+ import Control.Apply ((*>))
78import Control.Monad (unless , when )
89import Control.Monad.Eff
9- import Control.Monad.ST (ST (), STArray (), newSTArray , runSTArray )
1010
1111import Angular (copy , extend )
1212import Angular.Location (Location (), getPath , setPath )
1313import Angular.Scope (Scope (), watch , readScope , extendScope , modifyScope )
14- import Angular.This (readThis , writeThis )
15- import Angular.ST (readSTArray , pushSTArray , pushAllSTArray , writeSTArray , spliceSTArray )
14+ import Angular.This (readThis , writeThis )
1615
1716import Todomvc.Storage (Store (), Todo (), get , put )
1817
@@ -23,33 +22,31 @@ addTodo scope = do
2322 unless empty $ do
2423 let todo = { title: title, completed: false }
2524 todos <- get
26- put $ todos <> [todo]
27- pushSTArray s.todos todo
28- extendScope { newTodo: " "
25+ let res = todos <> [todo]
26+ put res
27+ extendScope { todos: res
28+ , newTodo: " "
2929 , remainingCount: s.remainingCount + 1 } scope
3030
3131todoCompleted scope todo
3232 = modifyScope (\s -> do
3333 let change = if todo.completed then -1 else 1
34- arr <- readSTArray s.todos
35- put arr
34+ put s.todos
3635 return { remainingCount: s.remainingCount + change }
3736 ) scope
3837
3938clearCompletedTodos scope = do
4039 s <- readScope scope
41- arr <- readSTArray s.todos
42- let res = A .filter (\a -> not a.completed) arr
40+ let res = A .filter (\a -> not a.completed) s.todos
4341 put res
44- writeSTArray s. todos res
42+ extendScope { todos: res } scope
4543
4644markAll scope compl
4745 = modifyScope (\s -> do
48- arr <- readSTArray s.todos
49- let res = (\a -> a { completed = not compl }) <$> arr
46+ let res = (\a -> a { completed = not compl }) <$> s.todos
5047 put res
51- writeSTArray s. todos res
52- return { remainingCount: if compl then A .length res else 0 }
48+ return { todos: res
49+ , remainingCount: if compl then A .length res else 0 }
5350 ) scope
5451
5552editTodo scope todo = do
@@ -62,31 +59,26 @@ doneEditing scope todo
6259 let title = S .trim todo.title
6360 when (S .length title == 0 ) $ removeTodo scope todo
6461 extend todo { title: title }
65- arr <- readSTArray s.todos
66- put arr
62+ put s.todos
6763 return { editedTodo: Nothing }
6864 ) scope
6965
7066removeTodo scope todo = do
7167 s <- readScope scope
72- arr <- readSTArray s.todos
73- let i = A .findIndex (\a -> refEq a todo) arr
74- unless (i == -1 ) do
68+ let res = A .filter (\a -> not $ refEq a todo) s.todos
69+ unless (A .length res == A .length s.todos) do
7570 let c = if todo.completed then 0 else -1
76- extendScope { remainingCount: s.remainingCount + c} scope
77- spliceSTArray s.todos i 1 []
78- put arr
71+ extendScope { todos: res
72+ , remainingCount: s.remainingCount + c } scope
73+ put res
7974
8075revertEditing scope todo = do
8176 s <- readScope scope
82- arr <- readSTArray s.todos
83- let i = A .findIndex (\a -> refEq a todo) arr
84- unless (i == -1 ) do
77+ let res = A .filter (\a -> not $ refEq a todo) s.todos
78+ unless (A .length res == A .length s.todos) do
8579 case s.originalTodo of
86- Just t -> do
87- spliceSTArray s.todos i 1 [t]
88- doneEditing scope t
89- return unit
80+ Just t -> extendScope { todos: res <> [t] } scope *>
81+ doneEditing scope t
9082 Nothing -> return unit
9183
9284watchRemainingCount scope =
@@ -108,15 +100,13 @@ todoctrl scope this location = do
108100 if S .length path == 0 then setPath " /" location else return " "
109101 watchRemainingCount scope
110102 watchLocationPath scope
111- todosRef <- newSTArray 0 { title: " " , completed: false }
112103 todos <- get
113- pushAllSTArray todosRef todos
114104 let remainingCount = foldl (\b a -> if a.completed then b else b + 1 ) 0 todos
115105 extendScope { fromMaybe: fromMaybe
116106 , newTodo: " "
117107 , editedTodo: Nothing
118108 , originalTodo: Nothing
119- , todos: todosRef
109+ , todos: todos
120110 , remainingCount: remainingCount
121111 , location: location
122112 , addTodo: addTodo scope
0 commit comments