0 | ||| JSIO based on idris2-dom JSIO utilities, which is adjusted to this library.
 1 | module Node.JS.IO
 2 |
 3 | import public Control.Monad.Either
 4 | import Node.Error
 5 | import Node.Internal.Support
 6 |
 7 | public export
 8 | JSIO : Type -> Type
 9 | JSIO = EitherT Error IO
10 |
11 | export %inline
12 | runJSIO : JSIO a -> IO $ Either Error a
13 | runJSIO = runEitherT
14 |
15 | %foreign """
16 |   javascript:lambda:
17 |   (tya, cb) => {
18 |     try {
19 |       const result = cb()
20 |       return _right(result)
21 |     } catch(e) {
22 |       if (e instanceof Error) {
23 |         return _left(e)
24 |       }
25 |       return _left(new Error(e))
26 |     }
27 |   }
28 |   """
29 | ffi_catchIO : PrimIO a -> PrimIO $ Either Error a
30 |
31 | export
32 | catchIO : IO a -> JSIO a
33 | catchIO cb = MkEitherT $ primIO $ ffi_catchIO $ toPrim cb
34 |
35 | ||| Support Javascript exception handling when lifting IO to JSIO
36 | export
37 | implementation [JS] HasIO JSIO where
38 |   liftIO = catchIO
39 |
40 |