Skip to content

Commit 5f0a9f9

Browse files
committed
Add WriteableStream support
1 parent d73ca8f commit 5f0a9f9

9 files changed

+234
-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(chunk)(controller)();
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 :: chunk -> WritableStreamController 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

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 _abortErr(stream, reason) {
12+
return function() {
13+
return stream.abort(reason);
14+
};
15+
}
16+
17+
export function abort(stream) {
18+
return function() {
19+
return stream.abort();
20+
};
21+
}
22+
23+
export function locked(stream) {
24+
return function() {
25+
return stream.locked;
26+
};
27+
}
28+
29+
export function getWriter(stream) {
30+
return function() {
31+
return stream.getWriter();
32+
};
33+
}

src/Web/Streams/WritableStream.purs

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

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
export function _write(writer, chunk) {
2+
return function() {
3+
return writer.write(chunk);
4+
};
5+
}
6+
7+
export function close(writer) {
8+
return function() {
9+
return writer.close();
10+
};
11+
}
12+
13+
export function closed(writer) {
14+
return function() {
15+
return writer.closed;
16+
};
17+
}
18+
19+
export function _desiredSize(writer) {
20+
return function() {
21+
return writer.desiredSize;
22+
};
23+
}
24+
25+
export function ready(writer) {
26+
return function() {
27+
return writer.ready;
28+
};
29+
}
30+
31+
export function releaseLock(writer) {
32+
return function() {
33+
return writer.releaseLock();
34+
};
35+
}
36+
37+
export function abort(writer) {
38+
return function() {
39+
return writer.abort();
40+
};
41+
}
42+
43+
export function _abortErr(writer, err) {
44+
return function() {
45+
return writer.abort(err);
46+
};
47+
}

src/Web/Streams/Writer.purs

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
module Web.Streams.Writer
2+
( Writer
3+
, abort
4+
, abortErr
5+
, close
6+
, closed
7+
, desiredSize
8+
, ready
9+
, write
10+
) where
11+
12+
import Prelude
13+
14+
import Data.Maybe (Maybe)
15+
import Data.Nullable (Nullable)
16+
import Data.Nullable as Nullable
17+
import Effect (Effect)
18+
import Effect.Exception (Error)
19+
import Effect.Uncurried (EffectFn2, runEffectFn2)
20+
import Web.Promise (Promise)
21+
22+
foreign import data Writer :: Type -> Type
23+
24+
foreign import _write :: forall chunk. EffectFn2 (Writer chunk) chunk (Promise Unit)
25+
26+
write :: forall chunk. Writer chunk -> chunk -> Effect (Promise Unit)
27+
write = runEffectFn2 _write
28+
29+
foreign import ready :: forall chunk. Writer chunk -> Effect (Promise Unit)
30+
31+
foreign import close :: forall chunk. Writer chunk -> Effect (Promise Unit)
32+
33+
foreign import closed :: forall chunk. Writer chunk -> Effect (Promise Unit)
34+
35+
foreign import _desiredSize :: forall chunk. Writer chunk -> Effect (Nullable Int)
36+
37+
desiredSize :: forall chunk. Writer chunk -> Effect (Maybe Int)
38+
desiredSize = map (Nullable.toMaybe) <<< _desiredSize
39+
40+
foreign import releaseLock :: forall chunk. Writer chunk -> Effect Unit
41+
42+
foreign import abort :: forall chunk. Writer chunk -> Promise (Effect Unit)
43+
44+
foreign import _abortErr :: forall chunk. EffectFn2 (Writer chunk) Error (Promise Error)
45+
46+
abortErr :: forall chunk. Writer chunk -> Error -> Effect (Promise Error)
47+
abortErr = runEffectFn2 _abortErr

0 commit comments

Comments
 (0)