Skip to content

Commit ae0a869

Browse files
committed
Add WriteableStream support
1 parent d73ca8f commit ae0a869

9 files changed

+168
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Notable changes to this project are documented in this file. The format is based
77
Breaking changes:
88

99
New features:
10+
- Add WriteableStream support (#9)
1011

1112
Bugfixes:
1213

src/Web/Streams/Sink.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
export function _make(options) {
2+
var newOptions = {};
3+
if (options.start) {
4+
newOptions.start = function(controller) {
5+
return options.start(controller)();
6+
};
7+
}
8+
if (options.write) {
9+
newOptions.write = function(chunk, controller) {
10+
return options.write(controller)(chunk)();
11+
};
12+
}
13+
if (options.close) {
14+
newOptions.close = function(controller) {
15+
return options.close(controller)();
16+
};
17+
}
18+
if (options.abort) {
19+
newOptions.abort = function(reason) {
20+
return options.abort(reason)();
21+
};
22+
}
23+
return newOptions;
24+
}

src/Web/Streams/Sink.purs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
module Web.Streams.Sink
2+
( Sink
3+
, Optional
4+
, make
5+
) where
6+
7+
import Effect (Effect)
8+
import Effect.Exception (Error)
9+
import Prelude (Unit)
10+
import Prim.Row as Row
11+
import Web.Promise (Promise)
12+
import Web.Streams.WritableStreamController (WritableStreamController)
13+
14+
type Optional chunk =
15+
( start :: WritableStreamController chunk -> Effect (Promise Unit)
16+
, write :: WritableStreamController chunk -> chunk -> Effect (Promise Unit)
17+
, close :: WritableStreamController chunk -> Effect (Promise Unit)
18+
, abort :: Error -> Effect (Promise Unit)
19+
)
20+
21+
foreign import data Sink :: Type -> Type
22+
23+
make :: forall r rx chunk. Row.Union r rx (Optional chunk) => { | r } -> Sink chunk
24+
make = _make
25+
26+
foreign import _make :: forall r chunk. { | r } -> Sink chunk

src/Web/Streams/WritableStream.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
export function _new(source, strategy) {
2+
return new WritableStream(source, strategy);
3+
}
4+
5+
export function close(stream) {
6+
return function() {
7+
return stream.close();
8+
};
9+
}
10+
11+
export function _abort(stream, reason) {
12+
return function() {
13+
return stream.abort(reason);
14+
};
15+
}
16+
17+
export function locked(stream) {
18+
return function() {
19+
return stream.locked;
20+
};
21+
}
22+
23+
export function getWriter(stream) {
24+
return function() {
25+
return stream.getWriter();
26+
};
27+
}

src/Web/Streams/WritableStream.purs

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
module Web.Streams.WritableStream
2+
( WritableStream
3+
, new
4+
, abort
5+
, close
6+
, getWriter
7+
, locked
8+
) where
9+
10+
import Data.Maybe (Maybe)
11+
import Data.Nullable (Nullable, toNullable)
12+
import Effect (Effect)
13+
import Effect.Exception (Error)
14+
import Effect.Uncurried (EffectFn2, runEffectFn2)
15+
import Prelude (Unit)
16+
import Web.Promise (Promise)
17+
import Web.Streams.QueuingStrategy (QueuingStrategy)
18+
import Web.Streams.Sink (Sink)
19+
import Web.Streams.Writer (Writer)
20+
21+
foreign import data WritableStream :: Type -> Type
22+
23+
foreign import _new :: forall chunk. EffectFn2 (Sink chunk) (Nullable (QueuingStrategy chunk)) (WritableStream chunk)
24+
25+
new :: forall chunk. Sink chunk -> Maybe (QueuingStrategy chunk) -> Effect (WritableStream chunk)
26+
new source strategy = runEffectFn2 _new source (toNullable strategy)
27+
28+
foreign import _abort :: forall chunk. EffectFn2 (WritableStream chunk) Error (Promise Unit)
29+
30+
abort :: forall chunk. WritableStream chunk -> Error -> Effect (Promise Unit)
31+
abort = runEffectFn2 _abort
32+
33+
foreign import close :: forall chunk. WritableStream chunk -> Effect (Promise Unit)
34+
35+
foreign import getWriter :: forall chunk. WritableStream chunk -> Effect (Writer chunk)
36+
37+
foreign import locked :: forall chunk. WritableStream chunk -> Effect Boolean
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export function error(error) {
2+
return function(controller) {
3+
return function() {
4+
return controller.error(error);
5+
};
6+
};
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module Web.Streams.WritableStreamController where
2+
3+
import Effect (Effect)
4+
import Effect.Exception (Error)
5+
import Prelude (Unit)
6+
7+
foreign import data WritableStreamController :: Type -> Type
8+
9+
foreign import error :: forall chunk. WritableStreamController chunk -> Error -> Effect Unit

src/Web/Streams/Writer.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export function _write(writer, chunk) {
2+
return function() {
3+
return writer.write(chunk);
4+
};
5+
}
6+
7+
export function ready(writer) {
8+
return function() {
9+
return writer.ready;
10+
};
11+
}
12+
13+
export function close(writer) {
14+
return function() {
15+
return writer.close();
16+
};
17+
}

src/Web/Streams/Writer.purs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module Web.Streams.Writer
2+
( Writer
3+
, write
4+
) where
5+
6+
import Effect (Effect)
7+
import Effect.Uncurried (EffectFn2, runEffectFn2)
8+
import Prelude (Unit)
9+
import Web.Promise (Promise)
10+
11+
foreign import data Writer :: Type -> Type
12+
13+
foreign import _write :: forall chunk. EffectFn2 (Writer chunk) chunk (Promise Unit)
14+
15+
write :: forall chunk. Writer chunk -> chunk -> Effect (Promise Unit)
16+
write = runEffectFn2 _write
17+
18+
foreign import ready :: forall chunk. Writer chunk -> Effect (Promise Unit)
19+
20+
foreign import close :: forall chunk. Writer chunk -> Effect (Promise Unit)

0 commit comments

Comments
 (0)