Skip to content

Commit fde4489

Browse files
Migrate to eventNameH-style API; update read functions (#49)
* Add dep on event-emitter * Add coercion to event emitter * Replace most onX fns with eventH * Update exports to modern conventions * Add missing handlers for Writable/Readable * Update read functions / define dataH* handlers * Update tests * Drop unneeded test dep * Reimplement readEither This was dropped in a previous commit * Add changelog entry * Fix docs on readEither
1 parent 44f3fb9 commit fde4489

File tree

7 files changed

+278
-278
lines changed

7 files changed

+278
-278
lines changed

CHANGELOG.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,35 @@ Notable changes to this project are documented in this file. The format is based
66

77
Breaking changes:
88
- Update `node-buffer` to `v9.0.0` (#48 by @JordanMartinez)
9+
- Reimplement event handlers using `eventNameH`-style API (#49 by @JordanMartinez)
10+
11+
Previously, one would write something like the following, and be unable to remove
12+
the resulting listener.
13+
```purs
14+
Stream.onData stream \buffer -> do
15+
...
16+
```
17+
18+
Now, one writes such a thing via `on` (or a similar function) from `node-event-emitter`:
19+
```purs
20+
-- if the listener should be removed later, use `on`.
21+
removeListener <- stream # on dataH \buffer -> do
22+
...
23+
-- if it doesn't need to be removed, use `on_`.
24+
stream # on_ dataH \buffer -> do
25+
...
26+
```
927

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

1231
Bugfixes:
1332

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

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

bower.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"purescript-exceptions": "^6.0.0",
2323
"purescript-node-buffer": "^9.0.0",
2424
"purescript-nullable": "^6.0.0",
25-
"purescript-prelude": "^6.0.0"
25+
"purescript-prelude": "^6.0.0",
26+
"purescript-node-event-emitter": "https://github.com/purescript-node/purescript-node-event-emitter.git#^3.0.0"
2627
}
2728
}

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
"eslint": "^7.15.0",
1212
"pulp": "16.0.0-0",
1313
"purescript-psa": "^0.8.2",
14-
"rimraf": "^3.0.2",
15-
"stream-buffers": "^3.0.2"
14+
"rimraf": "^3.0.2"
1615
}
1716
}

src/Node/Stream.js

Lines changed: 16 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,22 @@
1-
const _undefined = undefined;
2-
export { _undefined as undefined };
3-
41
export function setEncodingImpl(s) {
52
return enc => () => {
63
s.setEncoding(enc);
74
};
85
}
96

10-
export function readChunkImpl(Left) {
11-
return Right => chunk => {
12-
if (chunk instanceof Buffer) {
13-
return Right(chunk);
14-
} else if (typeof chunk === "string") {
15-
return Left(chunk);
16-
} else {
17-
throw new Error(
18-
"Node.Stream.readChunkImpl: Unrecognised " +
19-
"chunk type; expected String or Buffer, got: " +
20-
chunk
21-
);
22-
}
23-
};
24-
}
25-
26-
export function onDataEitherImpl(readChunk) {
27-
return r => f => () => {
28-
r.on("data", data => {
29-
f(readChunk(data))();
30-
});
31-
};
32-
}
33-
34-
export function onEnd(s) {
35-
return f => () => {
36-
s.on("end", f);
37-
};
38-
}
39-
40-
export function onFinish(s) {
41-
return f => () => {
42-
s.on("finish", f);
43-
};
44-
}
45-
46-
export function onReadable(s) {
47-
return f => () => {
48-
s.on("readable", f);
49-
};
50-
}
51-
52-
export function onError(s) {
53-
return f => () => {
54-
s.on("error", e => {
55-
f(e)();
56-
});
57-
};
58-
}
59-
60-
export function onClose(s) {
61-
return f => () => {
62-
s.on("close", f);
63-
};
64-
}
7+
export const readChunkImpl = (useBuffer, useString, chunk) => {
8+
if (chunk instanceof Buffer) {
9+
return useBuffer(chunk);
10+
} else if (typeof chunk === "string") {
11+
return useString(chunk);
12+
} else {
13+
throw new Error(
14+
"Node.Stream.readChunkImpl: Unrecognised " +
15+
"chunk type; expected String or Buffer, got: " +
16+
chunk
17+
);
18+
}
19+
};
6520

6621
export function resume(s) {
6722
return () => {
@@ -91,16 +46,9 @@ export function unpipeAll(r) {
9146
return () => r.unpipe();
9247
}
9348

94-
export function readImpl(readChunk) {
95-
return Nothing => Just => r => s => () => {
96-
const v = r.read(s);
97-
if (v === null) {
98-
return Nothing;
99-
} else {
100-
return Just(readChunk(v));
101-
}
102-
};
103-
}
49+
export const readImpl = (r) => r.read();
50+
51+
export const readSizeImpl = (r, size) => r.read(size);
10452

10553
export function writeImpl(w) {
10654
return chunk => done => () => w.write(chunk, null, done);

0 commit comments

Comments
 (0)