Comments (3)
Ah so the tricky thing with your code is that:
newChannel
isn’t accessible within the channel executor- even if
newChannel
was accessible,isNewChannelClosed
would become stale when the twofor await
loops run.
If you want to query the closed state of the channel within the executor you have to use the stop
argument as a promise, along with a variable:
const newChannel = new Channel<number>(async (push, stop) => {
let isNewChannelClosed = false;
stop.then(() => (isNewChannelClosed = true));
(async () => {
for await (const something1 of someChannel1) {
if (isNewChannelClosed) {
// do x
}
push(something1)
}
})();
(async () => {
for await (const something2 of someChannel2) {
if (isNewChannelClosed) {
// do y
}
push(something2)
}
})();
});
I use this technique a lot in the code for channel combinators. See for example the implementation of Channel.merge
:
https://github.com/channeljs/channel/blob/master/packages/channel/src/channel.ts#L486-L524
from repeater.
It’s an explicit design goal of channels to not deviate from async generators in behavior or API. Async generators are not synchronously queryable using a property or method without consuming the next result via the next
method and checking the done
property. The question I have is, what advantage do you have in having a closed property over 1. storing the previous iterator result and checking its done
property, or 2. calling next
again and checking the done
property. Alternatively, you could also use for await
with a closed
iterator and there will be no effect (it’s the same as doing for (const value of [])
.
I am 99% sure that this is a wontfix issue unless we can come up with an incredibly compelling use case that could not be handled using Channel.prototype.next
or for await
. Abiding by the async iterator API and making channels indistinguishable from async generators is immensely valuable insofar as we can then take functions which return channels and treat them like they were async generator functions.
from repeater.
-
I would say
channel.isClosed()
is more convenient than storing the last result ofnext
and checking if it returned done. -
I am not sure but I think that calling
channel.next()
at the same time as runningfor await
loop on the same specific channel might cause that some data does not make it tofor await
loop? Please correct me if I am wrong.
What I needed it for was something like this. It had more logic but overall was similar.
const someChannel1 = new Channel<any>(() => {
// ...
})
const someChannel2 = new Channel<any>(() => {
// ...
})
const newChannel = new Channel<number>(async (push, stop) => {
const isNewChannelClosed = false; // newChannel.isClosed();
(async () => {
for await (const something1 of someChannel1) {
if (isNewChannelClosed) {
// do x
}
push(something1)
}
})();
(async () => {
for await (const something2 of someChannel2) {
if (isNewChannelClosed) {
// do y
}
push(something2)
}
})()
await stop()
});
Not the best/cleanest code I've written >.<
from repeater.
Related Issues (20)
- Create a logo HOT 1
- Redesign Repeater.prototype.return and the stop promise.
- Question: Convert Observable to Repeater and back HOT 5
- Missing documentation for packages HOT 2
- Not that great for implementing transducers? HOT 31
- Should the Repeater automatically stop when its executor fulfills? HOT 5
- Doesn't quite match AsyncIterable interface? HOT 3
- push*(repeater) type API HOT 3
- Some of the more fragile tests are failing in newer versions of node HOT 1
- Question about the abilities of async iteration HOT 2
- Repeater.race typings are incorrect HOT 1
- Aggressive memory leak in Repeater.race HOT 2
- 4.0 changes HOT 4
- TypeError: Invalid attempt to iterate non-iterable instance. HOT 5
- WeakMap error at Repeater.return HOT 8
- RepeatOrNot HOT 1
- Question about merging two Repeaters HOT 2
- Help with map transducer? HOT 13
- Stop promise not entirely compatible with Promise.race
- Event loop can die if stop() not called
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from repeater.