paldepind / flyd

The minimalistic but powerful, modular, functional reactive programming library in JavaScript.
MIT License
1.56k stars 85 forks source link

Errors and Endings #103

Closed jordalgo closed 8 years ago

jordalgo commented 8 years ago

@paldepind Was curious what you thought in terms of adding more fine-grained listening/subscribing into the streams, similar to a node streams approach e.g.

flyd.on('data', function(stream1) {
  console.log(stream1);
}, stream1);

flyd.on('error', function(stream1) {
  console.log(stream1);
}, stream1);

flyd.on('end', function(stream1) {
  console.log(stream1);
}, stream1);

Streams could have an additional error stream to add to their end stream. This will definitely make them a bit more complicated but it might be a nice addition as there isn't really a way to notify stream subscribers that an error has occurred (or even that an end has occurred) with continuous checking the status of the stream.

c-dante commented 8 years ago

See PR #104 as an implementation for Errors in flyd streams.

Also, you can already subscribe to end -- it is a flyd stream

var s = flyd.stream(0)
flyd.on(() => console.log('STREAM HAS ENDED!'), s.end);

s.end(true);

In the pull request, instead of adding another stream, it takes the stance of an unopinionated Either value with a nice API for access within the stream.

jordalgo commented 8 years ago

@c-dante That is awesome! I was just mentioning how I wanted to use Either to propagate errors in most.js. Also, yes, I forgot you can subscribe to the end streams (being that they are just streams). I'll comment directly on that PR cause I agree that it's a good way to go for this lib.