Skip to content

Commit edb19d9

Browse files
FFI: uncurried; update naming consistency (#50)
* FFI: uncurried; add pipe variant; rename * Update tests
1 parent fde4489 commit edb19d9

File tree

4 files changed

+136
-176
lines changed

4 files changed

+136
-176
lines changed

CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,21 @@ Breaking changes:
2424
stream # on_ dataH \buffer -> do
2525
...
2626
```
27+
- Renamed functions to better adhere to naming consistency (#50 by @JordanMartinez)
28+
29+
All functions that take an optional callback are now
30+
named using the following schema:
31+
- no callback: `functionName`
32+
- with callback: `functionName'`
33+
34+
Thus, the following were renamed:
35+
- `write` was renamed to `write'`
36+
- `writeString` was renamed to `writeString'`
37+
- `end` was renamed to `end'`
38+
- `destroyWithError` was renamed to `destroy'`
39+
40+
`write`, `writeString`, and `end` now refer to their non-callback versions.
41+
2742

2843
New features:
2944
- Added event handlers for `Writeable` streams (#49 by @JordanMartinez)
@@ -35,6 +50,7 @@ Other improvements:
3550
- Updated CI `actions/checkout` and `actions/setup-nodee` to `v3` (#48 by @JordanMartinez)
3651
- Format code via purs-tidy; enforce formatting via CI (#48 by @JordanMartinez)
3752
- Refactor tests using `passThrough` streams (#49 by @JordanMartinez)
53+
- Updated FFI to use uncurried functions (#50 by @JordanMartinez)
3854

3955
## [v7.0.0](https://github.com/purescript-node/purescript-node-streams/releases/tag/v7.0.0) - 2022-04-29
4056

src/Node/Stream.js

Lines changed: 28 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
export function setEncodingImpl(s) {
2-
return enc => () => {
3-
s.setEncoding(enc);
4-
};
5-
}
1+
export const setEncodingImpl = (s, enc) => s.setEncoding(enc);
62

73
export const readChunkImpl = (useBuffer, useString, chunk) => {
84
if (chunk instanceof Buffer) {
@@ -18,74 +14,40 @@ export const readChunkImpl = (useBuffer, useString, chunk) => {
1814
}
1915
};
2016

21-
export function resume(s) {
22-
return () => {
23-
s.resume();
24-
};
25-
}
17+
export const resumeImpl = (r) => r.resume();
2618

27-
export function pause(s) {
28-
return () => {
29-
s.pause();
30-
};
31-
}
19+
export const pauseImpl = (r) => r.pause;
3220

33-
export function isPaused(s) {
34-
return () => s.isPaused();
35-
}
21+
export const isPausedImpl = (r) => r.isPaused;
3622

37-
export function pipe(r) {
38-
return w => () => r.pipe(w);
39-
}
23+
export const pipeImpl = (r, w) => r.pipe(w);
4024

41-
export function unpipe(r) {
42-
return w => () => r.unpipe(w);
43-
}
25+
export const unpipeAllImpl = (r) => r.unpipe();
4426

45-
export function unpipeAll(r) {
46-
return () => r.unpipe();
47-
}
27+
export const unpipeImpl = (r, w) => r.unpipe(w);
4828

4929
export const readImpl = (r) => r.read();
5030

5131
export const readSizeImpl = (r, size) => r.read(size);
5232

53-
export function writeImpl(w) {
54-
return chunk => done => () => w.write(chunk, null, done);
55-
}
56-
57-
export function writeStringImpl(w) {
58-
return enc => s => done => () => w.write(s, enc, done);
59-
}
60-
61-
export function cork(w) {
62-
return () => w.cork();
63-
}
64-
65-
export function uncork(w) {
66-
return () => w.uncork();
67-
}
68-
69-
export function setDefaultEncodingImpl(w) {
70-
return enc => () => {
71-
w.setDefaultEncoding(enc);
72-
};
73-
}
74-
75-
export function endImpl(w) {
76-
return done => () => {
77-
w.end(null, null, done);
78-
};
79-
}
80-
81-
export function destroy(strm) {
82-
return () => {
83-
strm.destroy(null);
84-
};
85-
}
86-
87-
export function destroyWithError(strm) {
88-
return e => () => {
89-
strm.destroy(e);
90-
};
91-
}
33+
export const writeImpl = (w, buf) => w.write(buf);
34+
35+
export const writeCbImpl = (w, buf, cb) => w.write(buf, cb);
36+
37+
export const writeStringImpl = (w, str, enc) => w.write(str, enc);
38+
39+
export const writeStringCbImpl = (w, str, enc, cb) => w.write(str, enc, cb);
40+
41+
export const corkImpl = (w) => w.cork();
42+
43+
export const uncorkImpl = (w) => w.uncork();
44+
45+
export const setDefaultEncodingImpl = (w, enc) => w.setDefaultEncoding(enc);
46+
47+
export const endCbImpl = (w, cb) => w.end(cb);
48+
49+
export const endImpl = (w) => w.end();
50+
51+
export const destroyImpl = (w) => w.destroy();
52+
53+
export const destroyErrorImpl = (w, e) => w.destroy(e);

src/Node/Stream.purs

Lines changed: 76 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -36,27 +36,29 @@ module Node.Stream
3636
, readEither
3737
, readEither'
3838
, write
39+
, write'
3940
, writeString
41+
, writeString'
4042
, cork
4143
, uncork
4244
, setDefaultEncoding
4345
, end
46+
, end'
4447
, destroy
45-
, destroyWithError
48+
, destroy'
4649
) where
4750

4851
import Prelude
4952

5053
import Data.Either (Either(..))
5154
import Data.Maybe (Maybe(..))
5255
import Data.Nullable (Nullable, toMaybe)
53-
import Data.Nullable as N
5456
import Effect (Effect)
5557
import Effect.Exception (Error, throw)
56-
import Effect.Uncurried (EffectFn1, EffectFn2, EffectFn3, mkEffectFn1, runEffectFn1, runEffectFn2, runEffectFn3)
58+
import Effect.Uncurried (EffectFn1, EffectFn2, EffectFn3, EffectFn4, mkEffectFn1, runEffectFn1, runEffectFn2, runEffectFn3, runEffectFn4)
5759
import Node.Buffer (Buffer)
5860
import Node.Buffer as Buffer
59-
import Node.Encoding (Encoding)
61+
import Node.Encoding (Encoding, encodingToNode)
6062
import Node.EventEmitter (EventEmitter, EventHandle(..))
6163
import Node.EventEmitter.UtilTypes (EventHandle1, EventHandle0)
6264
import Unsafe.Coerce (unsafeCoerce)
@@ -235,12 +237,6 @@ readEither' r size = do
235237
(mkEffectFn1 (pure <<< Just <<< Left))
236238
c
237239

238-
foreign import setEncodingImpl
239-
:: forall w
240-
. Readable w
241-
-> String
242-
-> Effect Unit
243-
244240
-- | Set the encoding used to read chunks as strings from the stream. This
245241
-- | function may be useful when you are passing a readable stream to some other
246242
-- | JavaScript library, which already expects an encoding to be set.
@@ -252,7 +248,9 @@ setEncoding
252248
. Readable w
253249
-> Encoding
254250
-> Effect Unit
255-
setEncoding r enc = setEncodingImpl r (show enc)
251+
setEncoding r enc = runEffectFn2 setEncodingImpl r (show enc)
252+
253+
foreign import setEncodingImpl :: forall w. EffectFn2 (Readable w) String Unit
256254

257255
closeH :: forall rw. EventHandle0 (Stream rw)
258256
closeH = EventHandle "close" identity
@@ -285,79 +283,72 @@ endH :: forall w. EventHandle0 (Readable w)
285283
endH = EventHandle "end" identity
286284

287285
-- | Resume reading from the stream.
288-
foreign import resume :: forall w. Readable w -> Effect Unit
286+
resume :: forall w. Readable w -> Effect Unit
287+
resume r = runEffectFn1 resumeImpl r
288+
289+
foreign import resumeImpl :: forall w. EffectFn1 (Readable w) (Unit)
289290

290291
-- | Pause reading from the stream.
291-
foreign import pause :: forall w. Readable w -> Effect Unit
292+
pause :: forall w. Readable w -> Effect Unit
293+
pause r = runEffectFn1 pauseImpl r
294+
295+
foreign import pauseImpl :: forall w. EffectFn1 (Readable w) (Unit)
292296

293297
-- | Check whether or not a stream is paused for reading.
294-
foreign import isPaused :: forall w. Readable w -> Effect Boolean
298+
isPaused :: forall w. Readable w -> Effect Boolean
299+
isPaused r = runEffectFn1 isPausedImpl r
300+
301+
foreign import isPausedImpl :: forall w. EffectFn1 (Readable w) (Boolean)
295302

296303
-- | Read chunks from a readable stream and write them to a writable stream.
297-
foreign import pipe
298-
:: forall r w
299-
. Readable w
300-
-> Writable r
301-
-> Effect (Writable r)
304+
pipe :: forall w r. Readable w -> Writable r -> Effect Unit
305+
pipe r w = runEffectFn2 pipeImpl r w
306+
307+
foreign import pipeImpl :: forall w r. EffectFn2 (Readable w) (Writable r) (Unit)
302308

303309
-- | Detach a Writable stream previously attached using `pipe`.
304-
foreign import unpipe
305-
:: forall r w
306-
. Readable w
307-
-> Writable r
308-
-> Effect Unit
310+
unpipe :: forall w r. Readable w -> Writable r -> Effect Unit
311+
unpipe r w = runEffectFn2 unpipeImpl r w
312+
313+
foreign import unpipeImpl :: forall w r. EffectFn2 (Readable w) (Writable r) (Unit)
309314

310315
-- | Detach all Writable streams previously attached using `pipe`.
311-
foreign import unpipeAll
312-
:: forall w
313-
. Readable w
314-
-> Effect Unit
316+
unpipeAll :: forall w. Readable w -> Effect Unit
317+
unpipeAll r = runEffectFn1 unpipeAllImpl r
315318

316-
foreign import writeImpl
317-
:: forall r
318-
. Writable r
319-
-> Buffer
320-
-> EffectFn1 (N.Nullable Error) Unit
321-
-> Effect Boolean
319+
foreign import unpipeAllImpl :: forall w. EffectFn1 (Readable w) (Unit)
322320

323-
-- | Write a Buffer to a writable stream.
324-
write
325-
:: forall r
326-
. Writable r
327-
-> Buffer
328-
-> (Maybe Error -> Effect Unit)
329-
-> Effect Boolean
330-
write w b cb = writeImpl w b $ mkEffectFn1 (cb <<< N.toMaybe)
321+
write :: forall r. Writable r -> Buffer -> Effect Boolean
322+
write w b = runEffectFn2 writeImpl w b
331323

332-
foreign import writeStringImpl
333-
:: forall r
334-
. Writable r
335-
-> String
336-
-> String
337-
-> EffectFn1 (N.Nullable Error) Unit
338-
-> Effect Boolean
324+
foreign import writeImpl :: forall r a. EffectFn2 (Writable r) (Buffer) (a)
339325

340-
-- | Write a string in the specified encoding to a writable stream.
341-
writeString
342-
:: forall r
343-
. Writable r
344-
-> Encoding
345-
-> String
346-
-> (Maybe Error -> Effect Unit)
347-
-> Effect Boolean
348-
writeString w enc s cb = writeStringImpl w (show enc) s $ mkEffectFn1 (cb <<< N.toMaybe)
326+
write' :: forall r. Writable r -> Buffer -> (Maybe Error -> Effect Unit) -> Effect Boolean
327+
write' w b cb = runEffectFn3 writeCbImpl w b $ mkEffectFn1 \err -> cb (toMaybe err)
328+
329+
foreign import writeCbImpl :: forall r a. EffectFn3 (Writable r) (Buffer) (EffectFn1 (Nullable Error) Unit) (a)
330+
331+
writeString :: forall r. Writable r -> Encoding -> String -> Effect Boolean
332+
writeString w enc str = runEffectFn3 writeStringImpl w str (encodingToNode enc)
333+
334+
foreign import writeStringImpl :: forall r a. EffectFn3 (Writable r) (String) (String) (a)
335+
336+
writeString' :: forall r. Writable r -> Encoding -> String -> (Maybe Error -> Effect Unit) -> Effect Boolean
337+
writeString' w enc str cb = runEffectFn4 writeStringCbImpl w str (encodingToNode enc) $ mkEffectFn1 \err -> cb (toMaybe err)
338+
339+
foreign import writeStringCbImpl :: forall r a. EffectFn4 (Writable r) (String) (String) (EffectFn1 (Nullable Error) Unit) (a)
349340

350341
-- | Force buffering of writes.
351-
foreign import cork :: forall r. Writable r -> Effect Unit
342+
cork :: forall r. Writable r -> Effect Unit
343+
cork s = runEffectFn1 corkImpl s
344+
345+
foreign import corkImpl :: forall r. EffectFn1 (Writable r) (Unit)
352346

353347
-- | Flush buffered data.
354-
foreign import uncork :: forall r. Writable r -> Effect Unit
348+
uncork :: forall r. Writable r -> Effect Unit
349+
uncork w = runEffectFn1 uncorkImpl w
355350

356-
foreign import setDefaultEncodingImpl
357-
:: forall r
358-
. Writable r
359-
-> String
360-
-> Effect Unit
351+
foreign import uncorkImpl :: forall r. EffectFn1 (Writable r) (Unit)
361352

362353
-- | Set the default encoding used to write strings to the stream. This function
363354
-- | is useful when you are passing a writable stream to some other JavaScript
@@ -369,35 +360,28 @@ setDefaultEncoding
369360
. Writable r
370361
-> Encoding
371362
-> Effect Unit
372-
setDefaultEncoding r enc = setDefaultEncodingImpl r (show enc)
363+
setDefaultEncoding r enc = runEffectFn2 setDefaultEncodingImpl r (show enc)
373364

374-
foreign import endImpl
375-
:: forall r
376-
. Writable r
377-
-> EffectFn1 (N.Nullable Error) Unit
378-
-> Effect Unit
365+
foreign import setDefaultEncodingImpl :: forall r. EffectFn2 (Writable r) String Unit
379366

380367
-- | End writing data to the stream.
381-
end
382-
:: forall r
383-
. Writable r
384-
-> (Maybe Error -> Effect Unit)
385-
-> Effect Unit
386-
end w cb = endImpl w $ mkEffectFn1 (cb <<< N.toMaybe)
368+
end :: forall r. Writable r -> Effect Unit
369+
end w = runEffectFn1 endImpl w
387370

388-
-- | Destroy the stream. It will release any internal resources.
389-
--
390-
-- Added in node 8.0.
391-
foreign import destroy
392-
:: forall r
393-
. Stream r
394-
-> Effect Unit
371+
foreign import endImpl :: forall r. EffectFn1 (Writable r) (Unit)
372+
373+
end' :: forall r. Writable r -> (Maybe Error -> Effect Unit) -> Effect Unit
374+
end' w cb = runEffectFn2 endCbImpl w $ mkEffectFn1 \err -> cb (toMaybe err)
375+
376+
foreign import endCbImpl :: forall r. EffectFn2 (Writable r) (EffectFn1 (Nullable Error) Unit) (Unit)
377+
378+
destroy :: forall r. Stream r -> Effect Unit
379+
destroy w = runEffectFn1 destroyImpl w
380+
381+
foreign import destroyImpl :: forall r. EffectFn1 (Stream r) (Unit)
382+
383+
destroy' :: forall r. Stream r -> Error -> Effect Unit
384+
destroy' w e = runEffectFn2 destroyErrorImpl w e
385+
386+
foreign import destroyErrorImpl :: forall r. EffectFn2 (Stream r) (Error) Unit
395387

396-
-- | Destroy the stream and emit 'error'.
397-
--
398-
-- Added in node 8.0.
399-
foreign import destroyWithError
400-
:: forall r
401-
. Stream r
402-
-> Error
403-
-> Effect Unit

0 commit comments

Comments
 (0)