@@ -2,7 +2,6 @@ module Main where
2
2
3
3
import Prelude
4
4
5
- import Control.Monad.Cont.Trans (ContT (..), runContT )
6
5
import Control.Monad.Except.Trans (runExceptT )
7
6
import Data.Array (mapMaybe )
8
7
import Data.Array as Array
@@ -11,6 +10,8 @@ import Data.Foldable (elem, fold, for_, intercalate, traverse_)
11
10
import Data.FoldableWithIndex (forWithIndex_ )
12
11
import Data.Maybe (Maybe (..), fromMaybe )
13
12
import Effect (Effect )
13
+ import Effect.Aff (Aff , launchAff_ )
14
+ import Effect.Class (liftEffect )
14
15
import Effect.Console (error )
15
16
import Effect.Uncurried (EffectFn1 , EffectFn2 , EffectFn5 , mkEffectFn1 , runEffectFn1 , runEffectFn2 , runEffectFn5 )
16
17
import Foreign (renderForeignError )
@@ -132,31 +133,32 @@ compile = do
132
133
displayLoadingMessage
133
134
clearAnnotations
134
135
135
- runContT ( runExceptT (API .compile Config .compileUrl code)) \res_ ->
136
+ launchAff_ $ runExceptT (API .compile Config .compileUrl code) >>= \res_ ->
136
137
case res_ of
137
- Left err -> displayPlainText err
138
+ Left err -> liftEffect $ displayPlainText err
138
139
Right res -> do
139
- cleanUpMarkers
140
+ liftEffect cleanUpMarkers
140
141
141
142
case res of
142
143
Right (CompileSuccess (SuccessResult { js, warnings })) -> do
143
- showJs <- isShowJsChecked
144
- if showJs
145
- then do hideLoadingMessage
146
- displayPlainText js
147
- else runContT (runExceptT $ runLoader loader (JS js)) \sources -> do
148
- hideLoadingMessage
149
- for_ warnings \warnings_ -> do
150
- let toAnnotation (CompileWarning { errorCode, position, message }) =
151
- position <#> \(ErrorPosition pos) ->
152
- { row: pos.startLine - 1
153
- , column: pos.startColumn - 1
154
- , type: " warning"
155
- , text: message
156
- }
157
- runEffectFn1 setAnnotations (mapMaybe toAnnotation warnings_)
158
- for_ sources (execute (JS js))
159
- Right (CompileFailed (FailedResult { error })) -> do
144
+ showJs <- liftEffect isShowJsChecked
145
+ if showJs then liftEffect do
146
+ hideLoadingMessage
147
+ displayPlainText js
148
+ else do
149
+ sources <- runExceptT $ runLoader loader (JS js)
150
+ liftEffect hideLoadingMessage
151
+ for_ warnings \warnings_ -> liftEffect do
152
+ let toAnnotation (CompileWarning { errorCode, position, message }) =
153
+ position <#> \(ErrorPosition pos) ->
154
+ { row: pos.startLine - 1
155
+ , column: pos.startColumn - 1
156
+ , type: " warning"
157
+ , text: message
158
+ }
159
+ runEffectFn1 setAnnotations (mapMaybe toAnnotation warnings_)
160
+ for_ sources (liftEffect <<< execute (JS js))
161
+ Right (CompileFailed (FailedResult { error })) -> liftEffect do
160
162
hideLoadingMessage
161
163
case error of
162
164
CompilerErrors errs -> do
@@ -180,7 +182,7 @@ compile = do
180
182
pos.endLine
181
183
pos.endColumn
182
184
OtherError err -> displayPlainText err
183
- Left errs -> do
185
+ Left errs -> liftEffect do
184
186
hideLoadingMessage
185
187
displayPlainText " Unable to parse the response from the server"
186
188
traverse_ (error <<< renderForeignError) errs
@@ -196,7 +198,6 @@ execute js modules = do
196
198
setupEditor :: forall r . { code :: String | r } -> Effect Unit
197
199
setupEditor { code } = do
198
200
loadOptions
199
-
200
201
setTextAreaContent code
201
202
runEffectFn1 setEditorContent code
202
203
@@ -214,34 +215,32 @@ setupEditor { code } = do
214
215
compile
215
216
216
217
JQuery .select " #gist_save" >>= JQuery .on " click" \e _ ->
217
- publishNewGist
218
+ launchAff_ publishNewGist
218
219
219
220
compile
220
221
cacheCurrentCode
221
222
222
223
loadFromGist
223
224
:: String
224
- -> ({ code :: String } -> Effect Unit )
225
- -> Effect Unit
226
- loadFromGist id_ k = do
227
- runContT (runExceptT (getGistById id_ >>= \gi -> tryLoadFileFromGist gi " Main.purs" )) $
228
- case _ of
229
- Left err -> do
230
- window >>= alert err
231
- k { code: " " }
232
- Right code -> k { code }
225
+ -> Aff { code :: String }
226
+ loadFromGist id = do
227
+ runExceptT (getGistById id >>= \gi -> tryLoadFileFromGist gi " Main.purs" ) >>= case _ of
228
+ Left err -> do
229
+ liftEffect $ window >>= alert err
230
+ pure { code: " " }
231
+ Right code ->
232
+ pure { code }
233
233
234
234
withSession
235
235
:: String
236
- -> ({ code :: String } -> Effect Unit )
237
- -> Effect Unit
238
- withSession sessionId k = do
239
- state <- tryRetrieveSession sessionId
236
+ -> Aff { code :: String }
237
+ withSession sessionId = do
238
+ state <- liftEffect $ tryRetrieveSession sessionId
240
239
case state of
241
- Just state' -> k state'
240
+ Just state' -> pure state'
242
241
Nothing -> do
243
- gist <- fromMaybe Config .mainGist <$> getQueryStringMaybe " gist"
244
- loadFromGist gist k
242
+ gist <- liftEffect $ fromMaybe Config .mainGist <$> getQueryStringMaybe " gist"
243
+ loadFromGist gist
245
244
246
245
-- | Cache the current code in the session state
247
246
cacheCurrentCode :: Effect Unit
@@ -254,22 +253,21 @@ cacheCurrentCode = do
254
253
Nothing -> error " No session ID"
255
254
256
255
-- | Create a new Gist using the current content
257
- publishNewGist :: Effect Unit
256
+ publishNewGist :: Aff Unit
258
257
publishNewGist = do
259
- ok <- window >>= confirm (intercalate " \n "
258
+ ok <- liftEffect $ window >>= confirm (intercalate " \n "
260
259
[ " Do you really want to publish this code as an anonymous Gist?"
261
260
, " "
262
261
, " Note: this code will be available to anyone with a link to the Gist."
263
262
])
264
263
when ok do
265
- content <- getTextAreaContent
266
- runContT (runExceptT (uploadGist content)) $
267
- case _ of
268
- Left err -> do
269
- window >>= alert " Failed to create gist"
270
- error (" Failed to create gist: " <> err)
271
- Right gistId -> do
272
- setQueryStrings (Object .singleton " gist" gistId)
264
+ content <- liftEffect $ getTextAreaContent
265
+ runExceptT (uploadGist content) >>= case _ of
266
+ Left err -> liftEffect do
267
+ window >>= alert " Failed to create gist"
268
+ error (" Failed to create gist: " <> err)
269
+ Right gistId -> liftEffect do
270
+ setQueryStrings (Object .singleton " gist" gistId)
273
271
274
272
-- | Navigate to the specified URL.
275
273
navigateTo :: String -> Effect Unit
@@ -308,5 +306,6 @@ main = JQuery.ready do
308
306
viewMode <- JQueryExtras .filter jq " :checked" >>= JQueryExtras .getValueMaybe
309
307
changeViewMode viewMode
310
308
311
- runContT (do sessionId <- ContT createSessionIdIfNecessary
312
- ContT (withSession sessionId)) setupEditor
309
+ createSessionIdIfNecessary \sessionId -> launchAff_ do
310
+ code <- withSession sessionId
311
+ liftEffect $ setupEditor code
0 commit comments