Skip to content
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

Cannot read property 'then$1' of undefined when cancelling periodic stream subscription #26777

Closed
dustinlessard-wf opened this issue Jun 27, 2016 · 2 comments
Assignees
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-async

Comments

@dustinlessard-wf
Copy link

dustinlessard-wf commented Jun 27, 2016

When attempting to cancel a subscription to a periodic stream, doing something like this:

import 'dart:async';

Stream<dynamic> _myPeriodicStream;
StreamSubscription _myStreamSubscription;

void main() {
  _myPeriodicStream = new Stream<dynamic>.periodic(new Duration(milliseconds:100));
  _myStreamSubscription = _myPeriodicStream.listen((_){});
  _myStreamSubscription.cancel().then((_){
    print('cancelled subscription');
  });
}

An error is thrown:

Uncaught TypeError: Cannot read property 'then$1' of undefined

A quick trip to the function comments shows:

* Returns a future that is completed once the stream has finished
   * its cleanup. May also return `null` if no cleanup was necessary.
   *
   * Typically, futures are returned when the stream needs to release resources.
   * For example, a stream might need to close an open file (as an asynchronous
   * operation). If the listener wants to delete the file after having
   * canceled the subscription, it must wait for the cleanup future to complete.

Why return a null in the case of no cleanup being necessary? It's a bit unexpected to have to check if we're getting a null back instead of a Future. I expected that in the case of no cleanup, we would simply get a Future that immediately resolves.

@floitschG floitschG added area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-async labels Jun 27, 2016
@floitschG
Copy link
Contributor

Initially, close was a void function.
We later realized that this was a mistake and changed the API. However, for backwards-compatibility reasons we still had to allow the old void-functions. Since these void functions implicitly return null, we allow null as return value for close.

If you use async/await syntax, you don't need to care, since await null works fine.

Leaving this issue open, since we should probably update the documentation to recommend returning a Future and update our own Subscriptions.

@floitschG floitschG self-assigned this Aug 1, 2016
floitschG added a commit that referenced this issue Aug 8, 2016
Deprecate returning `null`.

Also, fixes cases where transformations on a stream didn't forward the cancel future

Fixes #26777.

BUG= http://dartbug.com/26777.
R=lrn@google.com

Committed: 395e7aa

Review URL: https://codereview.chromium.org/2202533003 .

Reverted: 99e5328
@floitschG floitschG reopened this Aug 9, 2016
@floitschG
Copy link
Contributor

Commit didn't stick. Will have to investigate why some tests are failing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-async
Projects
None yet
Development

No branches or pull requests

2 participants