-
Notifications
You must be signed in to change notification settings - Fork 90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proposal: Add a "do" method, like "forEach" #50
Comments
I would prefer |
Also, FWIW, I think it would be better to have |
Are you concerned about similarity to event emitter? JH
|
@Blesh I'm strongly opposed to implicit conversion from Observable to promise. : ) The challenge that I'm trying to address is the desire to provide an API which is ergonomically competitive with EventEmitter for users that don't really care about Rx, and don't particularly want to know any more about Observable than they have to. With EventEmitter, you have: object.on("type", event => {
// Do stuff
}); Which is about as ergonomic as you can get. Given that observable's are lazy, I really want a word that implies strong "action" and will differentiate it from the combinators. To me, "forEach" and "observe" sound passive. Also, in JS the "do" keyword is associated with immediate execution as opposed to registering side-effects. |
@jhusain That's a good point. Node's EventEmitter will throw if the second argument is not a function, so it's totally possible to create a backward-compatible overload. https://github.com/joyent/node/blob/master/lib/events.js#L143
|
To be fair, I'm strongly opposed to promises in general. I don't think of it as implicit conversion, I think of it as await being more useful. It should just subscribe to the observable and await completion, returning the last value. |
Regardless, I feel like the completion value on observable should probably go away. I was on board with it, but I can't seem to find the utility of it. |
Isn't the utility of the completion value that we can continue to drive generators with Observers? |
But sending a value into a generator via return doesn't do anything but call the finally block if one exists, right? |
A generator might want to be communicated a final computation value. This is for the co-routine case in which two algorithms are exchanging values. JH
|
But if you have a generator: function* myGeneratorObserver() {
try {
while(true) {
var value = yield;
console.log('next: ' + value);
}
} catch (err) {
console.log('error: ' + err);
} finally {
console.log('done');
}
} and you call return on it: var generatorObserver = myGeneratorObserver();
// primed
generatorObserver.next();
// and return
generatorObserver.return('where will this go?'); The return value isn't used for anything. What good is pushing something at it? |
@zenparsing do you get the same timing as you do today? E.g. the canceling mechanism doesn't seem to integrate well with events firing synchronously. |
@Blesh I agree that a value supplied to "complete" doesn't seem particularly useful. On the other hand, I can't think of a good reason to not forward the argument from the normalized observer's "complete" method to the observer's "complete". @annevk With this spec, observables send their "next" values synchronously, so per-event things like As an exercise, I rewrote https://github.com/zenparsing/es-observable/blob/master/dom-event-dispatch.md |
@zenparsing cool, seems compelling. Glad we haven't specified/implemented https://gist.github.com/annevk/5238964 yet. |
@zenparsing I guess what I'm saying is I can't think of a good reason to pass a value to complete at all... For a while I thought it was a good idea, not now I'm not so sure.
The second issue is a bigger deal, IMO. An Observable of 1 composes through any other observable operation. An Observable of none with a completion value is just an oddity. There would have to be totally different operations added for those, and maybe some sort of weird context switching operators to switch a nexted value or values over to be a completion value? The more I think about it, the more I'm not sure it makes sense. |
Switched back to "forEach" |
I'm happy with the decision to use an observer object instead of callbacks for "subscribe". However, this can make things a little cumbersome to type:
RxJS overloads "subscribe" such that it allows a list of callbacks, in addition to an observer. I think this overload is a little questionable for the ES spec though. In ES, all "callables" are objects, so it's entirely possible for a callable object to also implement "next", "error", and "complete". I would like to avoid any heuristic overloading.
Previously, we had a "forEach" method which accepted a single callback and returned a Promise for the completion of the stream. I'd like to bring that back, with some minor adjustments.
Proposal: add a "do" method which accepts a callback, subscribes to the observable and returns a promise for the completion value of the stream.
The "do" method would subscribe synchronously, and would provide no built in cancelation mechanism. Instead, a combinator could be used for early completion. For example:
The text was updated successfully, but these errors were encountered: