Skip to content

Commit e5248e6

Browse files
authored
Merge pull request theam#115 from mdibaiee/save-every-x-seconds
(111) save code every x seconds
2 parents bee7b37 + 6fff616 commit e5248e6

File tree

6 files changed

+40
-11
lines changed

6 files changed

+40
-11
lines changed

client-stack.yaml

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ packages:
2929
commit: ee39119817ff05ed7385ced4c8c0e7ee0ed7f682
3030

3131

32-
extra-deps: []
32+
extra-deps:
33+
- datetime-0.3.1
3334

3435
flags: {}
3536

package.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ dependencies:
2323
- directory
2424
- filepath
2525
- system-fileio
26+
- datetime
2627

2728
ghc-options:
2829
- -Wall

src/common/HaskellDo/CodeMirror/State.hs

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ module HaskellDo.CodeMirror.State where
1717

1818
import Transient.Move
1919
import HaskellDo.CodeMirror.Types
20+
import Data.DateTime
2021

2122
initialState :: State
2223
initialState = State
23-
{ content = ""
24+
{ content = ""
25+
, lastSave = startOfTime
2426
}
2527

2628
update :: Action -> State -> Cloud State

src/common/HaskellDo/CodeMirror/Types.hs

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414
- limitations under the License.
1515
-}
1616
module HaskellDo.CodeMirror.Types where
17+
import Data.DateTime
1718

1819
data Action
1920
= NewContent String
2021
deriving (Read, Show)
2122

2223
data State = State
23-
{ content :: String
24+
{ content :: String
25+
, lastSave :: DateTime
2426
} deriving (Read, Show)

src/common/HaskellDo/State.hs

+29-7
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ module HaskellDo.State where
1818

1919
import Control.Exception (try, SomeException)
2020
import Control.Monad (when)
21+
import Data.DateTime
2122

2223
import Transient.Move
2324

@@ -52,19 +53,40 @@ _preUpdate _ appState = do
5253
_postUpdate :: Action -> AppState -> Cloud AppState
5354
_postUpdate _ = return
5455

56+
saveEvery :: Integer
57+
saveEvery = 3 -- seconds
58+
5559
_update :: Action -> AppState -> Cloud AppState
5660
_update (CodeMirrorAction action) appState = do
5761
newCodeMirrorState <- CodeMirror.update action (codeMirrorState appState)
5862
let newContent = CodeMirror.content newCodeMirrorState
59-
_ <- atRemote $ Compilation.update
60-
(Compilation.WriteWorkingFile newContent)
61-
(compilationState appState)
63+
6264
compileShortcutPressed <- localIO CodeMirror.cmdOrCtrlReturnPressed
63-
let newState = appState
64-
{ codeMirrorState = newCodeMirrorState
65-
}
65+
66+
currentTime <- localIO getCurrentTime
67+
let lastSave = CodeMirror.lastSave $ codeMirrorState appState
68+
69+
newState <- if diffSeconds currentTime lastSave > saveEvery
70+
then do
71+
_ <- atRemote $ Compilation.update
72+
(Compilation.WriteWorkingFile newContent)
73+
(compilationState appState)
74+
75+
return appState {
76+
codeMirrorState = newCodeMirrorState { CodeMirror.lastSave = currentTime }
77+
}
78+
else
79+
return appState {
80+
codeMirrorState = newCodeMirrorState
81+
}
82+
6683
if compileShortcutPressed
67-
then update (ToolbarAction Toolbar.Compile) newState
84+
then do
85+
_ <- atRemote $ Compilation.update
86+
(Compilation.WriteWorkingFile newContent)
87+
(compilationState appState)
88+
89+
update (ToolbarAction Toolbar.Compile) newState
6890
else return newState
6991

7092
_update (ToolbarAction Toolbar.Compile) appState = do

stack.yaml

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ packages:
1818
git: https://github.com/transient-haskell/axiom
1919
commit: ee39119817ff05ed7385ced4c8c0e7ee0ed7f682
2020

21-
extra-deps: []
21+
extra-deps:
22+
- datetime-0.3.1
2223

2324
flags: {}
2425

0 commit comments

Comments
 (0)