Skip to content

Migrate to eventNameH-style API; update read functions #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,35 @@ Notable changes to this project are documented in this file. The format is based

Breaking changes:
- Update `node-buffer` to `v9.0.0` (#48 by @JordanMartinez)
- Reimplement event handlers using `eventNameH`-style API (#49 by @JordanMartinez)

Previously, one would write something like the following, and be unable to remove
the resulting listener.
```purs
Stream.onData stream \buffer -> do
...
```

Now, one writes such a thing via `on` (or a similar function) from `node-event-emitter`:
```purs
-- if the listener should be removed later, use `on`.
removeListener <- stream # on dataH \buffer -> do
...
-- if it doesn't need to be removed, use `on_`.
stream # on_ dataH \buffer -> do
...
```

New features:
- Added event handlers for `Writeable` streams (#49 by @JordanMartinez)

Bugfixes:

Other improvements:
- Bumped CI's node version to `lts/*` (#48 by @JordanMartinez)
- Updated CI `actions/checkout` and `actions/setup-nodee` to `v3` (#48 by @JordanMartinez)
- Format code via purs-tidy; enforce formatting via CI (#48 by @JordanMartinez)
- Refactor tests using `passThrough` streams (#49 by @JordanMartinez)

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

Expand Down
3 changes: 2 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"purescript-exceptions": "^6.0.0",
"purescript-node-buffer": "^9.0.0",
"purescript-nullable": "^6.0.0",
"purescript-prelude": "^6.0.0"
"purescript-prelude": "^6.0.0",
"purescript-node-event-emitter": "https://github.com/purescript-node/purescript-node-event-emitter.git#^3.0.0"
}
}
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"eslint": "^7.15.0",
"pulp": "16.0.0-0",
"purescript-psa": "^0.8.2",
"rimraf": "^3.0.2",
"stream-buffers": "^3.0.2"
"rimraf": "^3.0.2"
}
}
84 changes: 16 additions & 68 deletions src/Node/Stream.js
Original file line number Diff line number Diff line change
@@ -1,67 +1,22 @@
const _undefined = undefined;
export { _undefined as undefined };

export function setEncodingImpl(s) {
return enc => () => {
s.setEncoding(enc);
};
}

export function readChunkImpl(Left) {
return Right => chunk => {
if (chunk instanceof Buffer) {
return Right(chunk);
} else if (typeof chunk === "string") {
return Left(chunk);
} else {
throw new Error(
"Node.Stream.readChunkImpl: Unrecognised " +
"chunk type; expected String or Buffer, got: " +
chunk
);
}
};
}

export function onDataEitherImpl(readChunk) {
return r => f => () => {
r.on("data", data => {
f(readChunk(data))();
});
};
}

export function onEnd(s) {
return f => () => {
s.on("end", f);
};
}

export function onFinish(s) {
return f => () => {
s.on("finish", f);
};
}

export function onReadable(s) {
return f => () => {
s.on("readable", f);
};
}

export function onError(s) {
return f => () => {
s.on("error", e => {
f(e)();
});
};
}

export function onClose(s) {
return f => () => {
s.on("close", f);
};
}
export const readChunkImpl = (useBuffer, useString, chunk) => {
if (chunk instanceof Buffer) {
return useBuffer(chunk);
} else if (typeof chunk === "string") {
return useString(chunk);
} else {
throw new Error(
"Node.Stream.readChunkImpl: Unrecognised " +
"chunk type; expected String or Buffer, got: " +
chunk
);
}
};

export function resume(s) {
return () => {
Expand Down Expand Up @@ -91,16 +46,9 @@ export function unpipeAll(r) {
return () => r.unpipe();
}

export function readImpl(readChunk) {
return Nothing => Just => r => s => () => {
const v = r.read(s);
if (v === null) {
return Nothing;
} else {
return Just(readChunk(v));
}
};
}
export const readImpl = (r) => r.read();

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

export function writeImpl(w) {
return chunk => done => () => w.write(chunk, null, done);
Expand Down
Loading