Skip to content

Commit

Permalink
Add WriteableStream support
Browse files Browse the repository at this point in the history
  • Loading branch information
YourFin committed Jul 9, 2022
1 parent d73ca8f commit ae0a869
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Notable changes to this project are documented in this file. The format is based
Breaking changes:

New features:
- Add WriteableStream support (#9)

Bugfixes:

Expand Down
24 changes: 24 additions & 0 deletions src/Web/Streams/Sink.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export function _make(options) {
var newOptions = {};
if (options.start) {
newOptions.start = function(controller) {
return options.start(controller)();
};
}
if (options.write) {
newOptions.write = function(chunk, controller) {
return options.write(controller)(chunk)();
};
}
if (options.close) {
newOptions.close = function(controller) {
return options.close(controller)();
};
}
if (options.abort) {
newOptions.abort = function(reason) {
return options.abort(reason)();
};
}
return newOptions;
}
26 changes: 26 additions & 0 deletions src/Web/Streams/Sink.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module Web.Streams.Sink
( Sink
, Optional
, make
) where

import Effect (Effect)
import Effect.Exception (Error)
import Prelude (Unit)
import Prim.Row as Row
import Web.Promise (Promise)
import Web.Streams.WritableStreamController (WritableStreamController)

type Optional chunk =
( start :: WritableStreamController chunk -> Effect (Promise Unit)
, write :: WritableStreamController chunk -> chunk -> Effect (Promise Unit)
, close :: WritableStreamController chunk -> Effect (Promise Unit)
, abort :: Error -> Effect (Promise Unit)
)

foreign import data Sink :: Type -> Type

make :: forall r rx chunk. Row.Union r rx (Optional chunk) => { | r } -> Sink chunk
make = _make

foreign import _make :: forall r chunk. { | r } -> Sink chunk
27 changes: 27 additions & 0 deletions src/Web/Streams/WritableStream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export function _new(source, strategy) {
return new WritableStream(source, strategy);
}

export function close(stream) {
return function() {
return stream.close();
};
}

export function _abort(stream, reason) {
return function() {
return stream.abort(reason);
};
}

export function locked(stream) {
return function() {
return stream.locked;
};
}

export function getWriter(stream) {
return function() {
return stream.getWriter();
};
}
37 changes: 37 additions & 0 deletions src/Web/Streams/WritableStream.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module Web.Streams.WritableStream
( WritableStream
, new
, abort
, close
, getWriter
, locked
) where

import Data.Maybe (Maybe)
import Data.Nullable (Nullable, toNullable)
import Effect (Effect)
import Effect.Exception (Error)
import Effect.Uncurried (EffectFn2, runEffectFn2)
import Prelude (Unit)
import Web.Promise (Promise)
import Web.Streams.QueuingStrategy (QueuingStrategy)
import Web.Streams.Sink (Sink)
import Web.Streams.Writer (Writer)

foreign import data WritableStream :: Type -> Type

foreign import _new :: forall chunk. EffectFn2 (Sink chunk) (Nullable (QueuingStrategy chunk)) (WritableStream chunk)

new :: forall chunk. Sink chunk -> Maybe (QueuingStrategy chunk) -> Effect (WritableStream chunk)
new source strategy = runEffectFn2 _new source (toNullable strategy)

foreign import _abort :: forall chunk. EffectFn2 (WritableStream chunk) Error (Promise Unit)

abort :: forall chunk. WritableStream chunk -> Error -> Effect (Promise Unit)
abort = runEffectFn2 _abort

foreign import close :: forall chunk. WritableStream chunk -> Effect (Promise Unit)

foreign import getWriter :: forall chunk. WritableStream chunk -> Effect (Writer chunk)

foreign import locked :: forall chunk. WritableStream chunk -> Effect Boolean
7 changes: 7 additions & 0 deletions src/Web/Streams/WritableStreamController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function error(error) {
return function(controller) {
return function() {
return controller.error(error);
};
};
}
9 changes: 9 additions & 0 deletions src/Web/Streams/WritableStreamController.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Web.Streams.WritableStreamController where

import Effect (Effect)
import Effect.Exception (Error)
import Prelude (Unit)

foreign import data WritableStreamController :: Type -> Type

foreign import error :: forall chunk. WritableStreamController chunk -> Error -> Effect Unit
17 changes: 17 additions & 0 deletions src/Web/Streams/Writer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export function _write(writer, chunk) {
return function() {
return writer.write(chunk);
};
}

export function ready(writer) {
return function() {
return writer.ready;
};
}

export function close(writer) {
return function() {
return writer.close();
};
}
20 changes: 20 additions & 0 deletions src/Web/Streams/Writer.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module Web.Streams.Writer
( Writer
, write
) where

import Effect (Effect)
import Effect.Uncurried (EffectFn2, runEffectFn2)
import Prelude (Unit)
import Web.Promise (Promise)

foreign import data Writer :: Type -> Type

foreign import _write :: forall chunk. EffectFn2 (Writer chunk) chunk (Promise Unit)

write :: forall chunk. Writer chunk -> chunk -> Effect (Promise Unit)
write = runEffectFn2 _write

foreign import ready :: forall chunk. Writer chunk -> Effect (Promise Unit)

foreign import close :: forall chunk. Writer chunk -> Effect (Promise Unit)

0 comments on commit ae0a869

Please sign in to comment.