GithubHelp home page GithubHelp logo

Comments (6)

zenparsing avatar zenparsing commented on September 13, 2024

%SubscriptionObserverPrototype%.return(v) returns { value: undefined, done: true } if the un-normalized sink has no return method. But { value: v, done: true } seems more correct, based on how generators behave?

How so? If I have a generator that doesn't have a return statement (and no weird finally's), then invoking "return" will always give me { value: undefined, done: true } regardless of the arguments. (Perhaps that's not a good analogy, though.)

Both generators and %SubscriptionObserverPrototype% allow multiple calls to return() or throw(), which after the first do nothing but the default action---i.e. they don't affect the state in any way. Did I get this right?

That's right.

For both generators and %SubscriptionObserverPrototype%, calling throw() will then cause next() to always return { value: undefined, done: true }. This is surprising to me---I would have expected it to re-throw the error?

I'm not sure what the rationale was for that behavior on the generator side, but I don't think it causes any problems for Observable. Do you?

I am also curious how the cancellation function maps to generators. It seems like a pretty ad-hoc addition to me and I can't really make the mapping back.

The cancellation function (the "unsubscriber") acts similarly to a finally clause within a generator function. The idea is that the generator or observable needs a way to release any held resources, regardless of how the sequence is terminated (naturally or prematurely). For generators we have the finally clause. For observables we have the unsubscriber function.

See #14 for an open question regarding the unsubscriber function. Any input is appreciated!

from proposal-observable.

domenic avatar domenic commented on September 13, 2024

How so? If I have a generator that doesn't have a return statement (and no weird finally's), then invoking "return" will always give me { value: undefined, done: true } regardless of the arguments.

I was thinking https://people.mozilla.org/~jorendorff/es6-draft.html#sec-generatorresumeabrupt step 4.a.i.

I'm not sure what the rationale was for that behavior on the generator side, but I don't think it causes any problems for Observable. Do you?

Hmm, well for iterators it means you can't read the failure value out of the iterator. Not sure how it plays out for observers.

The cancellation function (the "unsubscriber") acts similarly to a finally clause within a generator function.

Makes some sense, but not entirely.

Under the paradigm that new Observable(fn) creates a function-class that roughly encapsulates calling fn(sink), it sounds like the actual behavior roughly-encapsulated by [Symbol.observer]() ends up being something like

let returnValue;
try {
  returnValue = fn(sink);
} async finally {
  returnValue();
}

?

Maybe a better model is new Observable(tryFunction, finallyFunction) which encapsulates the behavior

try {
  tryFunction(sink);
} async finally {
  finallyFunction();
}

Of course that raises the question: why don't we have a catchFunction? And now we're basically back to cancelable promises (which also have a .finally method with similar semantics).

from proposal-observable.

zenparsing avatar zenparsing commented on September 13, 2024

I was thinking https://people.mozilla.org/~jorendorff/es6-draft.html#sec-generatorresumeabrupt step 4.a.i.

Ah, you're right. I'll make that change.

from proposal-observable.

zenparsing avatar zenparsing commented on September 13, 2024

Maybe a better model is new Observable(tryFunction, finallyFunction)

Often, the subscriber function will create declare local variables that the cleanup function needs to access (e.g. the "listen" example on the README). For that reason it's convenient to define the cleanup function within the scope of the subscriber function. And since you don't want the cleanup function to run until after the subscriber function completes, returning it is both ergonomic and makes sense.

from proposal-observable.

domenic avatar domenic commented on September 13, 2024

Sure, but that's not really how scoping works for try/finally.

from proposal-observable.

zenparsing avatar zenparsing commented on September 13, 2024

Closing for cleanup

from proposal-observable.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.