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

Promises/A+ support #1141

Merged
merged 14 commits into from
Oct 25, 2017
Merged

Promises/A+ support #1141

merged 14 commits into from
Oct 25, 2017

Conversation

kraih
Copy link
Member

@kraih kraih commented Oct 24, 2017

Resolves #1139.

Copy link
Member

@jberger jberger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since I have little experience in the promise pattern I don't feel like I can review the new code very effectively. I am excited about the new capabilities especially if it can bring in new users.

That said, my concern about the change to catch still worries me greatly.

sub begin {
my ($self, $offset, $len) = @_;
$self->{pending}++;
my $id = $self->{counter}++;
return sub { $self->_step($id, $offset // 1, $len, @_) };
}

sub catch { shift->then(undef, shift) }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This breaks documented usage and can issue false negatives in common practice. I feel that to change this without a major version number increment or a deprecation is dangerous to our users. This could be mitigated by wrapping the passed in callback. Whether that be in a deprecation cycle or just a long-term fix is immaterial to me.

@kraih
Copy link
Member Author

kraih commented Oct 25, 2017

I disagree with @jberger and think documenting the possibility of breakage very prominently is good enough in this case.

Copy link
Member

@jhthorsen jhthorsen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall I'm positive to the change, but I think a major version and a note in the Changes file could be an idea. Another way to resolve this would be to make a new module and then deprecate both Mojo::IOLoop->delay and Mojo::IOLoop::Delay. Not too fond of that idea either, since a long(er) deprecation process should be in place for such a major change. I think this is tricky, but it could be resolved by saying that catch() is not well documented, tested and not widely in use on cpan, so let's just do the change.

@@ -455,7 +467,7 @@ L<Mojo::IOLoop::Delay/"steps">.
say 'Never actually reached.';
}
)->catch(sub {
my ($delay, $err) = @_;
my $err = shift;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not very fond of this change, but it does not seem to be very many modules on CPAN that use catch(). (my cpangrep skills might be off of course)

Copy link
Member

@jberger jberger Oct 25, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is true, I did a similar search and by far the most users were me you and @Akron . Though I can imagine that delay/catch is used more in end-user code than in library code, so it might not matter that much.

sub wait {
my $self = shift;
return if $self->ioloop->is_running;
$self->once(error => \&_die);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is _die() in use..?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

}

sub _die { $_[0]->has_subscribers('error') ? $_[0]->ioloop->stop : die $_[1] }

sub _settle {
my ($self, $status) = (shift, shift);
return $self if $self->{result};
Copy link
Member

@jhthorsen jhthorsen Oct 25, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I read the js spec for promises and I think if already resolved, the reject() or resolve() methods should return undef and not $self.

http://www.ecma-international.org/ecma-262/7.0/index.html#sec-promise-resolve-functions

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those two methods are not spec compliant by design. We can do there whatever has the most value for us.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't want this constructor just to be compatible.

Mojo::IOLoop::Delay->new(sub {
  my ($resolve, $reject) = @_;
  ...
   $resolve->('some value');
});

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you can find more use cases for boolean return values we can use those instead.

@@ -81,7 +81,8 @@ sub _delay {
my $c = shift;
my $tx = $c->render_later->tx;
my $delay = Mojo::IOLoop->delay(@_);
$delay->catch(sub { $c->helpers->reply->exception(pop) and undef $tx })->wait;
$delay->on(error => sub { $c->helpers->reply->exception(pop) and undef $tx });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need to change this line? Doesn't it work the same way, since you use pop and not $_[1] ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the time i didn't want to change behavior, promise callbacks are not the same as event callbacks. But it might be worth changing if $c->delay is also used purely for promises.

@kraih kraih merged commit bbe18c3 into master Oct 25, 2017
@kraih kraih deleted the promises branch October 31, 2017 12:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants