Skip to content

Commit b631e2c

Browse files
author
Colin Wahl
authored
Use EffectFnX (#70)
* Use EffectFnX * Update CHANGELOG * Make JSCallback an EffectFn2, remove handleCallbackImpl FFI, implement handleCallback in PureScript
1 parent 2629cb3 commit b631e2c

File tree

6 files changed

+126
-154
lines changed

6 files changed

+126
-154
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ New features:
1111
Bugfixes:
1212

1313
Other improvements:
14+
- Use `EffectFn` throughout instead of unsafe `mkEffect` utility (#70 by @colinwahl)
1415

1516
## [v8.1.0](https://github.com/purescript-node/purescript-node-fs/releases/tag/v8.1.0) - 2022-06-10
1617

src/Node/FS/Async.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,3 @@ export {
2323
write as writeImpl,
2424
close as closeImpl
2525
} from "fs";
26-
27-
export function handleCallbackImpl(left, right, f) {
28-
return function (err, value) {
29-
if (err) {
30-
f(left(err))();
31-
} else {
32-
f(right(value))();
33-
}
34-
};
35-
}

src/Node/FS/Async.purs

Lines changed: 57 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -39,67 +39,61 @@ import Prelude
3939
import Data.DateTime (DateTime)
4040
import Data.DateTime.Instant (fromDateTime, unInstant)
4141
import Data.Either (Either(..))
42-
import Data.Function.Uncurried (Fn2, Fn6, Fn4, Fn3, runFn2, runFn6, runFn4, runFn3)
4342
import Data.Int (round)
4443
import Data.Maybe (Maybe(..))
45-
import Data.Nullable (Nullable, toNullable)
44+
import Data.Nullable (Nullable, toMaybe, toNullable)
4645
import Data.Time.Duration (Milliseconds(..))
4746
import Effect (Effect)
4847
import Effect.Exception (Error)
48+
import Effect.Uncurried (EffectFn2, EffectFn3, EffectFn4, EffectFn6, mkEffectFn2, runEffectFn2, runEffectFn3, runEffectFn4, runEffectFn6)
4949
import Node.Buffer (Buffer, size)
5050
import Node.Encoding (Encoding)
5151
import Node.FS (FileDescriptor, ByteCount, FilePosition, BufferLength, BufferOffset, FileMode, FileFlags, SymlinkType, fileFlagsToNode, symlinkTypeToNode)
52-
import Node.FS.Internal (mkEffect)
5352
import Node.FS.Perms (Perms, permsToString, all, mkPerms)
5453
import Node.FS.Stats (StatsObj, Stats(..))
5554
import Node.Path (FilePath)
5655

57-
type JSCallback a = Fn2 (Nullable Error) a Unit
58-
59-
foreign import handleCallbackImpl ::
60-
forall a. Fn3 (Error -> Either Error a)
61-
(a -> Either Error a)
62-
(Callback a)
63-
(JSCallback a)
64-
65-
handleCallback :: forall a. (Callback a) -> JSCallback a
66-
handleCallback cb = runFn3 handleCallbackImpl Left Right cb
56+
type JSCallback a = EffectFn2 (Nullable Error) a Unit
6757

58+
handleCallback :: forall a. Callback a -> JSCallback a
59+
handleCallback cb = mkEffectFn2 \err a -> case toMaybe err of
60+
Nothing -> cb (Right a)
61+
Just err' -> cb (Left err')
6862

6963
-- | Type synonym for callback functions.
7064
type Callback a = Either Error a -> Effect Unit
7165

72-
foreign import renameImpl :: Fn3 FilePath FilePath (JSCallback Unit) Unit
73-
foreign import truncateImpl :: Fn3 FilePath Int (JSCallback Unit) Unit
74-
foreign import chownImpl :: Fn4 FilePath Int Int (JSCallback Unit) Unit
75-
foreign import chmodImpl :: Fn3 FilePath String (JSCallback Unit) Unit
76-
foreign import statImpl :: Fn2 FilePath (JSCallback StatsObj) Unit
77-
foreign import lstatImpl :: Fn2 FilePath (JSCallback StatsObj) Unit
78-
foreign import linkImpl :: Fn3 FilePath FilePath (JSCallback Unit) Unit
79-
foreign import symlinkImpl :: Fn4 FilePath FilePath String (JSCallback Unit) Unit
80-
foreign import readlinkImpl :: Fn2 FilePath (JSCallback FilePath) Unit
81-
foreign import realpathImpl :: forall cache. Fn3 FilePath { | cache } (JSCallback FilePath) Unit
82-
foreign import unlinkImpl :: Fn2 FilePath (JSCallback Unit) Unit
83-
foreign import rmdirImpl :: Fn3 FilePath { maxRetries :: Int, retryDelay :: Int } (JSCallback Unit) Unit
84-
foreign import rmImpl :: Fn3 FilePath { force :: Boolean, maxRetries :: Int, recursive :: Boolean, retryDelay :: Int } (JSCallback Unit) Unit
85-
foreign import mkdirImpl :: Fn3 FilePath { recursive :: Boolean, mode :: String } (JSCallback Unit) Unit
86-
foreign import readdirImpl :: Fn2 FilePath (JSCallback (Array FilePath)) Unit
87-
foreign import utimesImpl :: Fn4 FilePath Int Int (JSCallback Unit) Unit
88-
foreign import readFileImpl :: forall a opts. Fn3 FilePath { | opts } (JSCallback a) Unit
89-
foreign import writeFileImpl :: forall a opts. Fn4 FilePath a { | opts } (JSCallback Unit) Unit
90-
foreign import appendFileImpl :: forall a opts. Fn4 FilePath a { | opts } (JSCallback Unit) Unit
91-
foreign import openImpl :: Fn4 FilePath String (Nullable FileMode) (JSCallback FileDescriptor) Unit
92-
foreign import readImpl :: Fn6 FileDescriptor Buffer BufferOffset BufferLength (Nullable FilePosition) (JSCallback ByteCount) Unit
93-
foreign import writeImpl :: Fn6 FileDescriptor Buffer BufferOffset BufferLength (Nullable FilePosition) (JSCallback ByteCount) Unit
94-
foreign import closeImpl :: Fn2 FileDescriptor (JSCallback Unit) Unit
66+
foreign import renameImpl :: EffectFn3 FilePath FilePath (JSCallback Unit) Unit
67+
foreign import truncateImpl :: EffectFn3 FilePath Int (JSCallback Unit) Unit
68+
foreign import chownImpl :: EffectFn4 FilePath Int Int (JSCallback Unit) Unit
69+
foreign import chmodImpl :: EffectFn3 FilePath String (JSCallback Unit) Unit
70+
foreign import statImpl :: EffectFn2 FilePath (JSCallback StatsObj) Unit
71+
foreign import lstatImpl :: EffectFn2 FilePath (JSCallback StatsObj) Unit
72+
foreign import linkImpl :: EffectFn3 FilePath FilePath (JSCallback Unit) Unit
73+
foreign import symlinkImpl :: EffectFn4 FilePath FilePath String (JSCallback Unit) Unit
74+
foreign import readlinkImpl :: EffectFn2 FilePath (JSCallback FilePath) Unit
75+
foreign import realpathImpl :: forall cache. EffectFn3 FilePath { | cache } (JSCallback FilePath) Unit
76+
foreign import unlinkImpl :: EffectFn2 FilePath (JSCallback Unit) Unit
77+
foreign import rmdirImpl :: EffectFn3 FilePath { maxRetries :: Int, retryDelay :: Int } (JSCallback Unit) Unit
78+
foreign import rmImpl :: EffectFn3 FilePath { force :: Boolean, maxRetries :: Int, recursive :: Boolean, retryDelay :: Int } (JSCallback Unit) Unit
79+
foreign import mkdirImpl :: EffectFn3 FilePath { recursive :: Boolean, mode :: String } (JSCallback Unit) Unit
80+
foreign import readdirImpl :: EffectFn2 FilePath (JSCallback (Array FilePath)) Unit
81+
foreign import utimesImpl :: EffectFn4 FilePath Int Int (JSCallback Unit) Unit
82+
foreign import readFileImpl :: forall a opts. EffectFn3 FilePath { | opts } (JSCallback a) Unit
83+
foreign import writeFileImpl :: forall a opts. EffectFn4 FilePath a { | opts } (JSCallback Unit) Unit
84+
foreign import appendFileImpl :: forall a opts. EffectFn4 FilePath a { | opts } (JSCallback Unit) Unit
85+
foreign import openImpl :: EffectFn4 FilePath String (Nullable FileMode) (JSCallback FileDescriptor) Unit
86+
foreign import readImpl :: EffectFn6 FileDescriptor Buffer BufferOffset BufferLength (Nullable FilePosition) (JSCallback ByteCount) Unit
87+
foreign import writeImpl :: EffectFn6 FileDescriptor Buffer BufferOffset BufferLength (Nullable FilePosition) (JSCallback ByteCount) Unit
88+
foreign import closeImpl :: EffectFn2 FileDescriptor (JSCallback Unit) Unit
9589

9690

9791
-- | Renames a file.
9892
rename :: FilePath
9993
-> FilePath
10094
-> Callback Unit
10195
-> Effect Unit
102-
rename oldFile newFile cb = mkEffect $ \_ -> runFn3
96+
rename oldFile newFile cb = runEffectFn3
10397
renameImpl oldFile newFile (handleCallback cb)
10498

10599
-- | Truncates a file to the specified length.
@@ -108,7 +102,7 @@ truncate :: FilePath
108102
-> Callback Unit
109103
-> Effect Unit
110104

111-
truncate file len cb = mkEffect $ \_ -> runFn3
105+
truncate file len cb = runEffectFn3
112106
truncateImpl file len (handleCallback cb)
113107

114108
-- | Changes the ownership of a file.
@@ -118,7 +112,7 @@ chown :: FilePath
118112
-> Callback Unit
119113
-> Effect Unit
120114

121-
chown file uid gid cb = mkEffect $ \_ -> runFn4
115+
chown file uid gid cb = runEffectFn4
122116
chownImpl file uid gid (handleCallback cb)
123117

124118
-- | Changes the permissions of a file.
@@ -127,15 +121,15 @@ chmod :: FilePath
127121
-> Callback Unit
128122
-> Effect Unit
129123

130-
chmod file perms cb = mkEffect $ \_ -> runFn3
124+
chmod file perms cb = runEffectFn3
131125
chmodImpl file (permsToString perms) (handleCallback cb)
132126

133127
-- | Gets file statistics.
134128
stat :: FilePath
135129
-> Callback Stats
136130
-> Effect Unit
137131

138-
stat file cb = mkEffect $ \_ -> runFn2
132+
stat file cb = runEffectFn2
139133
statImpl file (handleCallback $ cb <<< map Stats)
140134

141135
-- | Gets file or symlink statistics. `lstat` is identical to `stat`, except
@@ -144,7 +138,7 @@ stat file cb = mkEffect $ \_ -> runFn2
144138
lstat :: FilePath
145139
-> Callback Stats
146140
-> Effect Unit
147-
lstat file cb = mkEffect $ \_ -> runFn2
141+
lstat file cb = runEffectFn2
148142
lstatImpl file (handleCallback $ cb <<< map Stats)
149143

150144
-- | Creates a link to an existing file.
@@ -153,7 +147,7 @@ link :: FilePath
153147
-> Callback Unit
154148
-> Effect Unit
155149

156-
link src dst cb = mkEffect $ \_ -> runFn3
150+
link src dst cb = runEffectFn3
157151
linkImpl src dst (handleCallback cb)
158152

159153
-- | Creates a symlink.
@@ -163,23 +157,23 @@ symlink :: FilePath
163157
-> Callback Unit
164158
-> Effect Unit
165159

166-
symlink src dest ty cb = mkEffect $ \_ -> runFn4
160+
symlink src dest ty cb = runEffectFn4
167161
symlinkImpl src dest (symlinkTypeToNode ty) (handleCallback cb)
168162

169163
-- | Reads the value of a symlink.
170164
readlink :: FilePath
171165
-> Callback FilePath
172166
-> Effect Unit
173167

174-
readlink path cb = mkEffect $ \_ -> runFn2
168+
readlink path cb = runEffectFn2
175169
readlinkImpl path (handleCallback cb)
176170

177171
-- | Find the canonicalized absolute location for a path.
178172
realpath :: FilePath
179173
-> Callback FilePath
180174
-> Effect Unit
181175

182-
realpath path cb = mkEffect $ \_ -> runFn3
176+
realpath path cb = runEffectFn3
183177
realpathImpl path {} (handleCallback cb)
184178

185179
-- | Find the canonicalized absolute location for a path using a cache object
@@ -189,15 +183,15 @@ realpath' :: forall cache. FilePath
189183
-> Callback FilePath
190184
-> Effect Unit
191185

192-
realpath' path cache cb = mkEffect $ \_ -> runFn3
186+
realpath' path cache cb = runEffectFn3
193187
realpathImpl path cache (handleCallback cb)
194188

195189
-- | Deletes a file.
196190
unlink :: FilePath
197191
-> Callback Unit
198192
-> Effect Unit
199193

200-
unlink file cb = mkEffect $ \_ -> runFn2
194+
unlink file cb = runEffectFn2
201195
unlinkImpl file (handleCallback cb)
202196

203197
-- | Deletes a directory.
@@ -211,7 +205,7 @@ rmdir' :: FilePath
211205
-> { maxRetries :: Int, retryDelay :: Int }
212206
-> Callback Unit
213207
-> Effect Unit
214-
rmdir' path opts cb = mkEffect $ \_ -> runFn3
208+
rmdir' path opts cb = runEffectFn3
215209
rmdirImpl path opts (handleCallback cb)
216210

217211
-- | Deletes a file or directory.
@@ -225,7 +219,7 @@ rm' :: FilePath
225219
-> { force :: Boolean, maxRetries :: Int, recursive :: Boolean, retryDelay :: Int }
226220
-> Callback Unit
227221
-> Effect Unit
228-
rm' path opts cb = mkEffect $ \_ -> runFn3
222+
rm' path opts cb = runEffectFn3
229223
rmImpl path opts (handleCallback cb)
230224

231225

@@ -241,15 +235,15 @@ mkdir'
241235
-> { recursive :: Boolean, mode :: Perms }
242236
-> Callback Unit
243237
-> Effect Unit
244-
mkdir' file { recursive, mode: perms } cb = mkEffect $ \_ -> runFn3
238+
mkdir' file { recursive, mode: perms } cb = runEffectFn3
245239
mkdirImpl file { recursive, mode: permsToString perms } (handleCallback cb)
246240

247241
-- | Reads the contents of a directory.
248242
readdir :: FilePath
249243
-> Callback (Array FilePath)
250244
-> Effect Unit
251245

252-
readdir file cb = mkEffect $ \_ -> runFn2
246+
readdir file cb = runEffectFn2
253247
readdirImpl file (handleCallback cb)
254248

255249
-- | Sets the accessed and modified times for the specified file.
@@ -259,7 +253,7 @@ utimes :: FilePath
259253
-> Callback Unit
260254
-> Effect Unit
261255

262-
utimes file atime mtime cb = mkEffect $ \_ -> runFn4
256+
utimes file atime mtime cb = runEffectFn4
263257
utimesImpl file
264258
(fromDate atime)
265259
(fromDate mtime)
@@ -274,7 +268,7 @@ readFile :: FilePath
274268
-> Callback Buffer
275269
-> Effect Unit
276270

277-
readFile file cb = mkEffect $ \_ -> runFn3
271+
readFile file cb = runEffectFn3
278272
readFileImpl file {} (handleCallback cb)
279273

280274
-- | Reads the entire contents of a text file with the specified encoding.
@@ -283,7 +277,7 @@ readTextFile :: Encoding
283277
-> Callback String
284278
-> Effect Unit
285279

286-
readTextFile encoding file cb = mkEffect $ \_ -> runFn3
280+
readTextFile encoding file cb = runEffectFn3
287281
readFileImpl file { encoding: show encoding } (handleCallback cb)
288282

289283
-- | Writes a buffer to a file.
@@ -292,7 +286,7 @@ writeFile :: FilePath
292286
-> Callback Unit
293287
-> Effect Unit
294288

295-
writeFile file buff cb = mkEffect $ \_ -> runFn4
289+
writeFile file buff cb = runEffectFn4
296290
writeFileImpl file buff {} (handleCallback cb)
297291

298292
-- | Writes text to a file using the specified encoding.
@@ -302,7 +296,7 @@ writeTextFile :: Encoding
302296
-> Callback Unit
303297
-> Effect Unit
304298

305-
writeTextFile encoding file buff cb = mkEffect $ \_ -> runFn4
299+
writeTextFile encoding file buff cb = runEffectFn4
306300
writeFileImpl file buff { encoding: show encoding } (handleCallback cb)
307301

308302
-- | Appends the contents of a buffer to a file.
@@ -311,7 +305,7 @@ appendFile :: FilePath
311305
-> Callback Unit
312306
-> Effect Unit
313307

314-
appendFile file buff cb = mkEffect $ \_ -> runFn4
308+
appendFile file buff cb = runEffectFn4
315309
appendFileImpl file buff {} (handleCallback cb)
316310

317311
-- | Appends text to a file using the specified encoding.
@@ -321,7 +315,7 @@ appendTextFile :: Encoding
321315
-> Callback Unit
322316
-> Effect Unit
323317

324-
appendTextFile encoding file buff cb = mkEffect $ \_ -> runFn4
318+
appendTextFile encoding file buff cb = runEffectFn4
325319
appendFileImpl file buff { encoding: show encoding } (handleCallback cb)
326320

327321

@@ -332,7 +326,7 @@ fdOpen :: FilePath
332326
-> Maybe FileMode
333327
-> Callback FileDescriptor
334328
-> Effect Unit
335-
fdOpen file flags mode cb = mkEffect $ \_ -> runFn4 openImpl file (fileFlagsToNode flags) (toNullable mode) (handleCallback cb)
329+
fdOpen file flags mode cb = runEffectFn4 openImpl file (fileFlagsToNode flags) (toNullable mode) (handleCallback cb)
336330

337331
-- | Read from a file asynchronously. See the [Node Documentation](https://nodejs.org/api/fs.html#fs_fs_read_fd_buffer_offset_length_position_callback)
338332
-- | for details.
@@ -343,7 +337,7 @@ fdRead :: FileDescriptor
343337
-> Maybe FilePosition
344338
-> Callback ByteCount
345339
-> Effect Unit
346-
fdRead fd buff off len pos cb = mkEffect $ \_ -> runFn6 readImpl fd buff off len (toNullable pos) (handleCallback cb)
340+
fdRead fd buff off len pos cb = runEffectFn6 readImpl fd buff off len (toNullable pos) (handleCallback cb)
347341

348342
-- | Convenience function to fill the whole buffer from the current
349343
-- | file position.
@@ -364,7 +358,7 @@ fdWrite :: FileDescriptor
364358
-> Maybe FilePosition
365359
-> Callback ByteCount
366360
-> Effect Unit
367-
fdWrite fd buff off len pos cb = mkEffect $ \_ -> runFn6 writeImpl fd buff off len (toNullable pos) (handleCallback cb)
361+
fdWrite fd buff off len pos cb = runEffectFn6 writeImpl fd buff off len (toNullable pos) (handleCallback cb)
368362

369363
-- | Convenience function to append the whole buffer to the current
370364
-- | file position.
@@ -381,4 +375,4 @@ fdAppend fd buff cb = do
381375
fdClose :: FileDescriptor
382376
-> Callback Unit
383377
-> Effect Unit
384-
fdClose fd cb = mkEffect $ \_ -> runFn2 closeImpl fd (handleCallback cb)
378+
fdClose fd cb = runEffectFn2 closeImpl fd (handleCallback cb)

src/Node/FS/Internal.purs

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)