Skip to content

Commit 4216697

Browse files
committed
feat: sync -> implement
1 parent e48eea5 commit 4216697

File tree

10 files changed

+528
-207
lines changed

10 files changed

+528
-207
lines changed

src/Node/FS/Aff/Dir.purs

+5-6
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ import Effect.Ref as Ref
1919
import Node.FS.Dir (Dir)
2020
import Node.FS.Dir as Dir
2121
import Node.FS.Dirent (Dirent, DirentNameTypeString)
22-
import Node.FS.Internal.AffUtils
22+
import Node.FS.Internal.AffUtils (toAff1)
2323

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

27-
close :: Dir -> Aff (Maybe Error)
27+
close :: Dir -> Aff Unit
2828
close dir = makeAff \k -> do
29-
Dir.close dir (k <<< Right)
29+
Dir.close dir k
3030
pure nonCanceler
3131

3232
entries :: Dir -> Aff (Array (Dirent DirentNameTypeString))
@@ -43,13 +43,12 @@ entries dir = do
4343
-- | Implementation - https://github.com/nodejs/node/blob/b2161d3a137e5a2582c71c798e140d2ba8f7c1d4/lib/internal/fs/dir.js#L257
4444
-- |
4545
-- | Nodejs version ignores errors on read, doesnt ignore errors on close.
46-
-- | This purs version ignores error at close, doesnt ignore errors at read.
47-
-- | But in reality - behavior should be the same (proved in tests).
46+
-- | Purescript version - the same (proved in tests).
4847
-- |
4948
-- | Possible errors:
5049
-- | - if dir is closed already - `read` and `close` will throw "Directory handle was closed"
5150
entriesIterate :: Dir -> ((Dirent DirentNameTypeString) -> Effect Unit) -> Aff Unit
52-
entriesIterate dir handleDirent = finally (void $ close dir) $ makeAff \(k :: Either Error Unit -> Effect Unit) -> do
51+
entriesIterate dir handleDirent = finally (close dir) $ makeAff \(k :: Either Error Unit -> Effect Unit) -> do
5352
stopRef <- Ref.new false
5453
go k stopRef
5554
pure $ effectCanceler $ Ref.write true stopRef

src/Node/FS/Async.purs

+98-93
Large diffs are not rendered by default.

src/Node/FS/Dir.purs

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import Effect (Effect)
99
import Effect.Exception (Error)
1010
import Effect.Uncurried (EffectFn1, EffectFn2, mkEffectFn1, runEffectFn1, runEffectFn2)
1111
import Node.FS.Dirent (Dirent, DirentNameTypeString)
12-
import Node.FS.Internal.Utils (Callback0, JSCallback0, JSCallback1, handleCallback1)
12+
import Node.FS.Internal.Utils (Callback0, JSCallback0, JSCallback1, Callback1, handleCallback0, handleCallback1)
1313
import Node.Path (FilePath)
1414

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

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

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

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

3838
-- | Synchronously read the next directory entry.
3939
readSync :: Dir -> Effect (Maybe (Dirent DirentNameTypeString))

src/Node/FS/Internal/Utils.purs

+7-2
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,21 @@ import Data.Time.Duration (Milliseconds(..))
1212
import Data.Tuple (Tuple(..))
1313
import Effect (Effect)
1414
import Effect.Exception (Error)
15-
import Effect.Uncurried (EffectFn1, EffectFn2, EffectFn3, mkEffectFn2, mkEffectFn3)
15+
import Effect.Uncurried (EffectFn1, EffectFn2, EffectFn3, mkEffectFn1, mkEffectFn2, mkEffectFn3)
1616

1717
type JSCallback0 = EffectFn1 (Nullable Error) Unit
1818
type JSCallback1 a = EffectFn2 (Nullable Error) a Unit
1919
type JSCallback2 a b = EffectFn3 (Nullable Error) a b Unit
2020

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

25+
handleCallback0 :: Callback0 -> JSCallback0
26+
handleCallback0 cb = mkEffectFn1 \err -> case toMaybe err of
27+
Nothing -> cb (Right unit)
28+
Just err' -> cb (Left err')
29+
2530
handleCallback1 :: forall a. Callback1 a -> JSCallback1 a
2631
handleCallback1 cb = mkEffectFn2 \err a -> case toMaybe err of
2732
Nothing -> cb (Right a)

src/Node/FS/Options.purs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module Node.FS.Options where
22

3-
import Node.FS.Types (BufferLength, BufferOffset, FileMode, FilePosition)
3+
import Node.FS.Types (BufferLength, BufferOffset, EncodingString, FileMode, FilePosition)
44
import Prelude
55

66
import Data.Function.Uncurried (Fn2, mkFn2)
@@ -11,7 +11,7 @@ import Foreign (Foreign)
1111
import Foreign as Foreign
1212
import Node.Buffer (Buffer)
1313
import Node.Encoding (Encoding(..), encodingToNode)
14-
import Node.FS.Constants (FileFlags(..), fileFlagsToNode)
14+
import Node.FS.Constants (FileFlags(..), fileFlagsToNode)
1515
import Node.FS.Dirent (Dirent, DirentNameTypeString)
1616
import Node.FS.Perms (Perms, all, mkPerms, permsToString, read, write)
1717
import Node.Path (FilePath)
@@ -39,7 +39,7 @@ mkdirOptionsToInternal { recursive, mode } = { recursive, mode: permsToString mo
3939

4040
---------
4141

42-
type RealpathOptionsInternal = { encoding :: String }
42+
type RealpathOptionsInternal = { encoding :: EncodingString }
4343
type RealpathOptions = { encoding :: Encoding }
4444

4545
realpathOptionsDefault :: RealpathOptions
@@ -318,7 +318,7 @@ globDirentOptionsToInternal { cwd, exclude } = { cwd: toNullable cwd, exclude: t
318318

319319
------------------
320320

321-
type OpendirOptionsInternal = { encoding :: String, bufferSize :: Int, recursive :: Boolean }
321+
type OpendirOptionsInternal = { encoding :: EncodingString, bufferSize :: Int, recursive :: Boolean }
322322
type OpendirOptions = { encoding :: Encoding, bufferSize :: Int, recursive :: Boolean }
323323

324324
opendirOptionsDefault :: OpendirOptions

src/Node/FS/Sync.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export {
2-
accessSync as accessImpl,
3-
copyFileSync as copyFileImpl,
4-
mkdtempSync as mkdtempImpl,
2+
accessSync as accessSyncImpl,
3+
copyFileSync as copyFileSyncImpl,
4+
mkdtempSync as mkdtempSyncImpl,
55
renameSync as renameSyncImpl,
66
truncateSync as truncateSyncImpl,
77
chownSync as chownSyncImpl,

0 commit comments

Comments
 (0)