from: Accept a callback function.
This commit is contained in:
parent
8c26f23ead
commit
6b577ea6cd
3 changed files with 38 additions and 14 deletions
34
lib/index.js
34
lib/index.js
|
@ -308,30 +308,44 @@ const Channel = function(length = 0) {
|
|||
|
||||
Channel.from = values => {
|
||||
const channel = Channel();
|
||||
|
||||
(async () => {
|
||||
// iterable
|
||||
try {
|
||||
// iterable
|
||||
for (let value of values) {
|
||||
await channel.push(value);
|
||||
}
|
||||
|
||||
await channel.close();
|
||||
} catch (exception) {
|
||||
// Assume it's a Node.js stream.readable.
|
||||
// callback function
|
||||
try {
|
||||
for (;;) {
|
||||
const value = values();
|
||||
|
||||
values.on("readable", async () => {
|
||||
while (true) {
|
||||
const data = values.read();
|
||||
|
||||
if (data === null) {
|
||||
if (value === undefined) {
|
||||
await channel.close();
|
||||
break;
|
||||
} else {
|
||||
await channel.push(data);
|
||||
await channel.push(value);
|
||||
}
|
||||
}
|
||||
});
|
||||
} catch (exception) {
|
||||
// Node.js stream.readable
|
||||
values.on("readable", async () => {
|
||||
while (true) {
|
||||
const data = values.read();
|
||||
|
||||
values.once("end", channel.close);
|
||||
if (data === null) {
|
||||
break;
|
||||
} else {
|
||||
await channel.push(data);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
values.once("end", channel.close);
|
||||
}
|
||||
}
|
||||
})();
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue