from
and of
: Return read-only channels.
This commit is contained in:
parent
8088d7e096
commit
9c18fc7055
3 changed files with 54 additions and 22 deletions
34
doc/API.md
34
doc/API.md
|
@ -9,10 +9,11 @@
|
||||||
- [value()](#value)
|
- [value()](#value)
|
||||||
- [Array-like Properties](#array-like-properties)
|
- [Array-like Properties](#array-like-properties)
|
||||||
- [Channel](#channel)
|
- [Channel](#channel)
|
||||||
- [Channel([bufferLength = 0]) -> Channel](#channelbufferlength--0---channel)
|
- [Channel([bufferLength]) -> Channel](#channelbufferlength---channel)
|
||||||
- [Channel.isChannel(value) -> Boolean](#channelischannelvalue---boolean)
|
- [Channel.isChannel(value) -> Boolean](#channelischannelvalue---boolean)
|
||||||
- [Channel.of(...values) -> Channel](#channelofvalues---channel)
|
- [Channel.of(...values) -> read-only Channel](#channelofvalues---read-only-channel)
|
||||||
- [Channel.from(callback | iterable | stream.Readable[, mapfn [, thisArg]]) -> Channel](#channelfromcallback--iterable--streamreadable-mapfn--thisarg---channel)
|
- [Channel.from(callback | iterable | stream.Readable[, mapfn [, thisArg]]) -> read-only Channel](#channelfromcallback--iterable--streamreadable-mapfn--thisarg---read-only-channel)
|
||||||
|
- [Examples](#examples-1)
|
||||||
- [Channel Object](#channel-object)
|
- [Channel Object](#channel-object)
|
||||||
- [every(callbackfn[, thisArg]) -> async Boolean](#everycallbackfn-thisarg---async-boolean)
|
- [every(callbackfn[, thisArg]) -> async Boolean](#everycallbackfn-thisarg---async-boolean)
|
||||||
- [filter(callbackfn[, thisArg]) -> Channel](#filtercallbackfn-thisarg---channel)
|
- [filter(callbackfn[, thisArg]) -> Channel](#filtercallbackfn-thisarg---channel)
|
||||||
|
@ -160,11 +161,11 @@ to push up to `bufferLength` values before blocking.
|
||||||
|
|
||||||
Return `true` if `value` is a channel, `false` otherwise.
|
Return `true` if `value` is a channel, `false` otherwise.
|
||||||
|
|
||||||
### Channel.of(...values) -> Channel
|
### Channel.of(...values) -> read-only Channel
|
||||||
|
|
||||||
Push `values` into a new channel and then close it.
|
Push `values` into a new channel and then close it.
|
||||||
|
|
||||||
### Channel.from(callback | iterable | stream.Readable[, mapfn [, thisArg]]) -> Channel
|
### Channel.from(callback | iterable | stream.Readable[, mapfn [, thisArg]]) -> read-only Channel
|
||||||
|
|
||||||
Create a new `Channel` from a callback function, an iterable, or a [Node.js
|
Create a new `Channel` from a callback function, an iterable, or a [Node.js
|
||||||
readable stream](https://nodejs.org/api/stream.html#stream_readable_streams).
|
readable stream](https://nodejs.org/api/stream.html#stream_readable_streams).
|
||||||
|
@ -176,6 +177,29 @@ pushing into the channel. Close the channel when the function returns
|
||||||
If the optional `mapfn` argument is provided, call it (using the also optional
|
If the optional `mapfn` argument is provided, call it (using the also optional
|
||||||
`thisArg`) on each value before pushing it into the channel.
|
`thisArg`) on each value before pushing it into the channel.
|
||||||
|
|
||||||
|
#### Examples
|
||||||
|
|
||||||
|
```JavaScript
|
||||||
|
const randomValues = Channel.from(Math.random);
|
||||||
|
|
||||||
|
const fromArray = Channel.from([0, 1, 2]);
|
||||||
|
|
||||||
|
const generator = function*() {
|
||||||
|
let counter = 0;
|
||||||
|
|
||||||
|
for (;;) {
|
||||||
|
yield counter;
|
||||||
|
counter++;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const fromGenerator = Channel.from(generator());
|
||||||
|
|
||||||
|
const fromStream = Channel.from(
|
||||||
|
fs.createReadStream(`Communicating Sequential Processes.pdf`)
|
||||||
|
);
|
||||||
|
```
|
||||||
|
|
||||||
## Channel Object
|
## Channel Object
|
||||||
|
|
||||||
### every(callbackfn[, thisArg]) -> async Boolean
|
### every(callbackfn[, thisArg]) -> async Boolean
|
||||||
|
|
|
@ -349,7 +349,7 @@ Channel.from = (values, mapfn, thisArg) => {
|
||||||
}
|
}
|
||||||
})();
|
})();
|
||||||
|
|
||||||
return mapfn ? channel.map(mapfn, thisArg) : channel;
|
return (mapfn ? channel.map(mapfn, thisArg) : channel).readOnly();
|
||||||
};
|
};
|
||||||
|
|
||||||
Channel.of = (...values) => Channel.from(values);
|
Channel.of = (...values) => Channel.from(values);
|
||||||
|
|
|
@ -45,17 +45,27 @@ describe(`Channel`, function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe(`from`, function() {
|
describe(`from`, function() {
|
||||||
it(`callback`, async function() {
|
describe(`types`, function() {
|
||||||
let counter = 0;
|
it(`callback`, async function() {
|
||||||
const callback = () => (counter < 3 ? counter++ : undefined);
|
let counter = 0;
|
||||||
assert.deepEqual(await Channel.from(callback).values(), [0, 1, 2]);
|
const callback = () => (counter < 3 ? counter++ : undefined);
|
||||||
|
assert.deepEqual(await Channel.from(callback).values(), [0, 1, 2]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it(`iterable`, async function() {
|
||||||
|
assert.deepEqual(await Channel.from([0, 1, 2]).values(), [0, 1, 2]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it(`Node.js's stream.Readable`, async function() {
|
||||||
|
const readable = stream.PassThrough({ objectMode: true });
|
||||||
|
readable.write(0);
|
||||||
|
readable.write(1);
|
||||||
|
readable.end(2);
|
||||||
|
assert.deepEqual(await Channel.from(readable).values(), [0, 1, 2]);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it(`iterable`, async function() {
|
it(`with mapfn`, async function() {
|
||||||
assert.deepEqual(await Channel.from([0, 1, 2]).values(), [0, 1, 2]);
|
|
||||||
});
|
|
||||||
|
|
||||||
it(`mapfn`, async function() {
|
|
||||||
assert.deepEqual(
|
assert.deepEqual(
|
||||||
await Channel.from([`a`, `b`, `c`], value =>
|
await Channel.from([`a`, `b`, `c`], value =>
|
||||||
value.toUpperCase()
|
value.toUpperCase()
|
||||||
|
@ -64,12 +74,8 @@ describe(`Channel`, function() {
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it(`Node.js's stream.readOnly`, async function() {
|
it(`returns a readOnly channel`, function() {
|
||||||
const readOnly = stream.PassThrough({ objectMode: true });
|
assert(!(`push` in Channel.from([0, 1, 2])));
|
||||||
readOnly.write(0);
|
|
||||||
readOnly.write(1);
|
|
||||||
readOnly.end(2);
|
|
||||||
assert.deepEqual(await Channel.from(readOnly).values(), [0, 1, 2]);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -81,7 +87,9 @@ describe(`Channel`, function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it(`of`, async function() {
|
it(`of`, async function() {
|
||||||
assert.deepEqual(await Channel.of(0, 1, 2).values(), [0, 1, 2]);
|
const channel = Channel.of(0, 1, 2);
|
||||||
|
assert(!(`push` in channel));
|
||||||
|
assert.deepEqual(await channel.values(), [0, 1, 2]);
|
||||||
});
|
});
|
||||||
|
|
||||||
describe(`select`, function() {
|
describe(`select`, function() {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue