Skip to content

Commit

Permalink
feat: sync -> implement
Browse files Browse the repository at this point in the history
  • Loading branch information
srghma committed Oct 6, 2024
1 parent e48eea5 commit 89628fb
Show file tree
Hide file tree
Showing 9 changed files with 520 additions and 198 deletions.
11 changes: 5 additions & 6 deletions src/Node/FS/Aff/Dir.purs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ import Effect.Ref as Ref
import Node.FS.Dir (Dir)
import Node.FS.Dir as Dir
import Node.FS.Dirent (Dirent, DirentNameTypeString)
import Node.FS.Internal.AffUtils
import Node.FS.Internal.AffUtils (toAff1)

read :: Dir -> Aff (Maybe (Dirent DirentNameTypeString))
read = toAff1 Dir.read

close :: Dir -> Aff (Maybe Error)
close :: Dir -> Aff Unit
close dir = makeAff \k -> do
Dir.close dir (k <<< Right)
Dir.close dir k
pure nonCanceler

entries :: Dir -> Aff (Array (Dirent DirentNameTypeString))
Expand All @@ -43,13 +43,12 @@ entries dir = do
-- | Implementation - https://github.com/nodejs/node/blob/b2161d3a137e5a2582c71c798e140d2ba8f7c1d4/lib/internal/fs/dir.js#L257
-- |
-- | Nodejs version ignores errors on read, doesnt ignore errors on close.
-- | This purs version ignores error at close, doesnt ignore errors at read.
-- | But in reality - behavior should be the same (proved in tests).
-- | Purescript version - the same (proved in tests).
-- |
-- | Possible errors:
-- | - if dir is closed already - `read` and `close` will throw "Directory handle was closed"
entriesIterate :: Dir -> ((Dirent DirentNameTypeString) -> Effect Unit) -> Aff Unit
entriesIterate dir handleDirent = finally (void $ close dir) $ makeAff \(k :: Either Error Unit -> Effect Unit) -> do
entriesIterate dir handleDirent = finally (close dir) $ makeAff \(k :: Either Error Unit -> Effect Unit) -> do
stopRef <- Ref.new false
go k stopRef
pure $ effectCanceler $ Ref.write true stopRef
Expand Down
185 changes: 95 additions & 90 deletions src/Node/FS/Async.purs

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions src/Node/FS/Dir.purs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Effect (Effect)
import Effect.Exception (Error)
import Effect.Uncurried (EffectFn1, EffectFn2, mkEffectFn1, runEffectFn1, runEffectFn2)
import Node.FS.Dirent (Dirent, DirentNameTypeString)
import Node.FS.Internal.Utils (Callback0, JSCallback0, JSCallback1, handleCallback1)
import Node.FS.Internal.Utils (Callback0, JSCallback0, JSCallback1, Callback1, handleCallback0, handleCallback1)
import Node.Path (FilePath)

-- Foreign imports for the Dir class
Expand All @@ -25,15 +25,15 @@ foreign import path :: Dir -> FilePath

-- | Asynchronously close the directory's underlying resource handle.
close :: Dir -> Callback0 -> Effect Unit
close dir callback = runEffectFn2 closeImpl dir (mkEffectFn1 $ (callback <<< toMaybe))
close dir cb = runEffectFn2 closeImpl dir (handleCallback0 cb)

-- | Synchronously close the directory's underlying resource handle.
closeSync :: Dir -> Effect Unit
closeSync = runEffectFn1 closeSyncImpl

-- | Asynchronously read the next directory entry.
read :: Dir -> (Either Error (Maybe (Dirent DirentNameTypeString)) -> Effect Unit) -> Effect Unit
read dir callback = runEffectFn2 readImpl dir (handleCallback1 (callback <<< map toMaybe))
read :: Dir -> (Callback1 (Maybe (Dirent DirentNameTypeString))) -> Effect Unit
read dir cb = runEffectFn2 readImpl dir (handleCallback1 (cb <<< map toMaybe))

-- | Synchronously read the next directory entry.
readSync :: Dir -> Effect (Maybe (Dirent DirentNameTypeString))
Expand Down
9 changes: 7 additions & 2 deletions src/Node/FS/Internal/Utils.purs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,21 @@ import Data.Time.Duration (Milliseconds(..))
import Data.Tuple (Tuple(..))
import Effect (Effect)
import Effect.Exception (Error)
import Effect.Uncurried (EffectFn1, EffectFn2, EffectFn3, mkEffectFn2, mkEffectFn3)
import Effect.Uncurried (EffectFn1, EffectFn2, EffectFn3, mkEffectFn1, mkEffectFn2, mkEffectFn3)

type JSCallback0 = EffectFn1 (Nullable Error) Unit
type JSCallback1 a = EffectFn2 (Nullable Error) a Unit
type JSCallback2 a b = EffectFn3 (Nullable Error) a b Unit

-- | Type synonym for callback functions.
type Callback0 = Maybe Error -> Effect Unit
type Callback0 = Either Error Unit -> Effect Unit -- TODO: better Maybe Error -> Unit?
type Callback1 a = Either Error a -> Effect Unit

handleCallback0 :: Callback0 -> JSCallback0
handleCallback0 cb = mkEffectFn1 \err -> case toMaybe err of
Nothing -> cb (Right unit)
Just err' -> cb (Left err')

handleCallback1 :: forall a. Callback1 a -> JSCallback1 a
handleCallback1 cb = mkEffectFn2 \err a -> case toMaybe err of
Nothing -> cb (Right a)
Expand Down
6 changes: 3 additions & 3 deletions src/Node/FS/Options.purs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Node.FS.Options where

import Node.FS.Types (BufferLength, BufferOffset, FileMode, FilePosition)
import Node.FS.Types
import Prelude

import Data.Function.Uncurried (Fn2, mkFn2)
Expand Down Expand Up @@ -39,7 +39,7 @@ mkdirOptionsToInternal { recursive, mode } = { recursive, mode: permsToString mo

---------

type RealpathOptionsInternal = { encoding :: String }
type RealpathOptionsInternal = { encoding :: EncodingString }
type RealpathOptions = { encoding :: Encoding }

realpathOptionsDefault :: RealpathOptions
Expand Down Expand Up @@ -318,7 +318,7 @@ globDirentOptionsToInternal { cwd, exclude } = { cwd: toNullable cwd, exclude: t

------------------

type OpendirOptionsInternal = { encoding :: String, bufferSize :: Int, recursive :: Boolean }
type OpendirOptionsInternal = { encoding :: EncodingString, bufferSize :: Int, recursive :: Boolean }
type OpendirOptions = { encoding :: Encoding, bufferSize :: Int, recursive :: Boolean }

opendirOptionsDefault :: OpendirOptions
Expand Down
6 changes: 3 additions & 3 deletions src/Node/FS/Sync.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export {
accessSync as accessImpl,
copyFileSync as copyFileImpl,
mkdtempSync as mkdtempImpl,
accessSync as accessSyncImpl,
copyFileSync as copyFileSyncImpl,
mkdtempSync as mkdtempSyncImpl,
renameSync as renameSyncImpl,
truncateSync as truncateSyncImpl,
chownSync as chownSyncImpl,
Expand Down
Loading

0 comments on commit 89628fb

Please sign in to comment.