@@ -2,7 +2,6 @@ module Main where
22
33import Prelude
44
5- import Control.Monad.Cont.Trans (ContT (..), runContT )
65import Control.Monad.Except.Trans (runExceptT )
76import Data.Array (mapMaybe )
87import Data.Array as Array
@@ -11,6 +10,8 @@ import Data.Foldable (elem, fold, for_, intercalate, traverse_)
1110import Data.FoldableWithIndex (forWithIndex_ )
1211import Data.Maybe (Maybe (..), fromMaybe )
1312import Effect (Effect )
13+ import Effect.Aff (Aff , launchAff_ )
14+ import Effect.Class (liftEffect )
1415import Effect.Console (error )
1516import Effect.Uncurried (EffectFn1 , EffectFn2 , EffectFn5 , mkEffectFn1 , runEffectFn1 , runEffectFn2 , runEffectFn5 )
1617import Foreign (renderForeignError )
@@ -132,31 +133,32 @@ compile = do
132133 displayLoadingMessage
133134 clearAnnotations
134135
135- runContT ( runExceptT (API .compile Config .compileUrl code)) \res_ ->
136+ launchAff_ $ runExceptT (API .compile Config .compileUrl code) >>= \res_ ->
136137 case res_ of
137- Left err -> displayPlainText err
138+ Left err -> liftEffect $ displayPlainText err
138139 Right res -> do
139- cleanUpMarkers
140+ liftEffect cleanUpMarkers
140141
141142 case res of
142143 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
160162 hideLoadingMessage
161163 case error of
162164 CompilerErrors errs -> do
@@ -180,7 +182,7 @@ compile = do
180182 pos.endLine
181183 pos.endColumn
182184 OtherError err -> displayPlainText err
183- Left errs -> do
185+ Left errs -> liftEffect do
184186 hideLoadingMessage
185187 displayPlainText " Unable to parse the response from the server"
186188 traverse_ (error <<< renderForeignError) errs
@@ -196,7 +198,6 @@ execute js modules = do
196198setupEditor :: forall r . { code :: String | r } -> Effect Unit
197199setupEditor { code } = do
198200 loadOptions
199-
200201 setTextAreaContent code
201202 runEffectFn1 setEditorContent code
202203
@@ -214,34 +215,32 @@ setupEditor { code } = do
214215 compile
215216
216217 JQuery .select " #gist_save" >>= JQuery .on " click" \e _ ->
217- publishNewGist
218+ launchAff_ publishNewGist
218219
219220 compile
220221 cacheCurrentCode
221222
222223loadFromGist
223224 :: 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 }
233233
234234withSession
235235 :: 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
240239 case state of
241- Just state' -> k state'
240+ Just state' -> pure state'
242241 Nothing -> do
243- gist <- fromMaybe Config .mainGist <$> getQueryStringMaybe " gist"
244- loadFromGist gist k
242+ gist <- liftEffect $ fromMaybe Config .mainGist <$> getQueryStringMaybe " gist"
243+ loadFromGist gist
245244
246245-- | Cache the current code in the session state
247246cacheCurrentCode :: Effect Unit
@@ -254,22 +253,21 @@ cacheCurrentCode = do
254253 Nothing -> error " No session ID"
255254
256255-- | Create a new Gist using the current content
257- publishNewGist :: Effect Unit
256+ publishNewGist :: Aff Unit
258257publishNewGist = do
259- ok <- window >>= confirm (intercalate " \n "
258+ ok <- liftEffect $ window >>= confirm (intercalate " \n "
260259 [ " Do you really want to publish this code as an anonymous Gist?"
261260 , " "
262261 , " Note: this code will be available to anyone with a link to the Gist."
263262 ])
264263 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)
273271
274272-- | Navigate to the specified URL.
275273navigateTo :: String -> Effect Unit
@@ -308,5 +306,6 @@ main = JQuery.ready do
308306 viewMode <- JQueryExtras .filter jq " :checked" >>= JQueryExtras .getValueMaybe
309307 changeViewMode viewMode
310308
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