Skip to content

Commit 15409e1

Browse files
Update write/writeString/end to take Maybe Error arg
1 parent 1158f22 commit 15409e1

File tree

2 files changed

+29
-14
lines changed

2 files changed

+29
-14
lines changed

src/Node/Stream.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export function readImpl(readChunk) {
102102
};
103103
}
104104

105-
export function write(w) {
105+
export function writeImpl(w) {
106106
return chunk => done => () => w.write(chunk, null, done);
107107
}
108108

@@ -124,11 +124,9 @@ export function setDefaultEncodingImpl(w) {
124124
};
125125
}
126126

127-
export function end(w) {
127+
export function endImpl(w) {
128128
return done => () => {
129-
w.end(null, null, () => {
130-
done();
131-
});
129+
w.end(null, null, done);
132130
};
133131
}
134132

src/Node/Stream.purs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@ module Node.Stream
3737
import Prelude
3838

3939
import Effect (Effect)
40-
import Effect.Exception (throw, Error())
40+
import Effect.Exception (throw, Error)
4141
import Data.Either (Either(..))
4242
import Data.Maybe (Maybe(..), fromMaybe)
43-
import Node.Buffer (Buffer())
43+
import Node.Buffer (Buffer)
44+
import Data.Nullable as N
45+
import Effect.Uncurried (EffectFn1, mkEffectFn1)
4446
import Node.Buffer as Buffer
4547
import Node.Encoding (Encoding)
4648

@@ -249,20 +251,28 @@ foreign import unpipeAll
249251
. Readable w
250252
-> Effect Unit
251253

254+
foreign import writeImpl
255+
:: forall r
256+
. Writable r
257+
-> Buffer
258+
-> EffectFn1 (N.Nullable Error) Unit
259+
-> Effect Boolean
260+
252261
-- | Write a Buffer to a writable stream.
253-
foreign import write
262+
write
254263
:: forall r
255264
. Writable r
256265
-> Buffer
257-
-> (Error -> Effect Unit)
266+
-> (Maybe Error -> Effect Unit)
258267
-> Effect Boolean
268+
write w b cb = writeImpl w b $ mkEffectFn1 (cb <<< N.toMaybe)
259269

260270
foreign import writeStringImpl
261271
:: forall r
262272
. Writable r
263273
-> String
264274
-> String
265-
-> (Error -> Effect Unit)
275+
-> EffectFn1 (N.Nullable Error) Unit
266276
-> Effect Boolean
267277

268278
-- | Write a string in the specified encoding to a writable stream.
@@ -271,9 +281,9 @@ writeString
271281
. Writable r
272282
-> Encoding
273283
-> String
274-
-> (Error -> Effect Unit)
284+
-> (Maybe Error -> Effect Unit)
275285
-> Effect Boolean
276-
writeString w enc = writeStringImpl w (show enc)
286+
writeString w enc s cb = writeStringImpl w (show enc) s $ mkEffectFn1 (cb <<< N.toMaybe)
277287

278288
-- | Force buffering of writes.
279289
foreign import cork :: forall r. Writable r -> Effect Unit
@@ -299,12 +309,19 @@ setDefaultEncoding
299309
-> Effect Unit
300310
setDefaultEncoding r enc = setDefaultEncodingImpl r (show enc)
301311

302-
-- | End writing data to the stream.
303-
foreign import end
312+
foreign import endImpl
304313
:: forall r
305314
. Writable r
315+
-> EffectFn1 (N.Nullable Error) Unit
306316
-> Effect Unit
317+
318+
-- | End writing data to the stream.
319+
end
320+
:: forall r
321+
. Writable r
322+
-> (Maybe Error -> Effect Unit)
307323
-> Effect Unit
324+
end w cb = endImpl w $ mkEffectFn1 (cb <<< N.toMaybe)
308325

309326
-- | Destroy the stream. It will release any internal resources.
310327
--

0 commit comments

Comments
 (0)