mojo icon indicating copy to clipboard operation
mojo copied to clipboard

__SUB__ references wrong callback, we must use outer callback

Open Sadrak opened this issue 4 years ago • 15 comments

Summary

__SUB__ references the wrong sub

Motivation

copy & paste from example was not working

Sadrak avatar May 12 '21 21:05 Sadrak

Pretty sure that introduces a circular reference, so 👎 from me.

jhthorsen avatar May 13 '21 12:05 jhthorsen

Pretty sure that introduces a circular reference, so from me.

No, this is a common workflow for working with callbacks calling itself. No circular reference.

Sadrak avatar May 13 '21 12:05 Sadrak

No circular reference.

OK, i misunderstood your plea.

There is one circular reference. Variable $fetch itself used in callback, produce one circular reference, but not for every call.

Sadrak avatar May 13 '21 13:05 Sadrak

Always squash your commits.

kraih avatar May 13 '21 13:05 kraih

Always squash your commits.

I am not familiar with github, i thought this will happen on accepting MR.

Sadrak avatar May 13 '21 13:05 Sadrak

ping?

I guess i changed everything required, except the squash, how can i do this in a running MR?

Sadrak avatar Jun 25 '21 06:06 Sadrak

ping?

I guess i changed everything required, except the squash, how can i do this in a running MR?

Interactive rebase and force push.

marcusramberg avatar Aug 13 '21 19:08 marcusramberg

ping? I guess i changed everything required, except the squash, how can i do this in a running MR?

Interactive rebase and force push.

nice & easy, thanks for the hint!

done

Sadrak avatar Aug 14 '21 09:08 Sadrak

@jhthorsen did i get an approval now? :pray:

Sadrak avatar Aug 14 '21 09:08 Sadrak

ping?

Sadrak avatar Sep 30 '21 11:09 Sadrak

ping?

Sadrak avatar Feb 11 '22 08:02 Sadrak

@Sadrak, I've toyed with some alternative approaches that avoid storing the __SUB__ for nested scopes altogether.

This example uses a single function for both the "worker" and the callback.

my $fetch = sub ($ua, $tx = undef) {
  if ($tx) {    # $tx exists if sub is responding to a callback
    say $tx->req->url;
    __SUB__->($ua);
    $promise->resolve if --$count == 0;
  }

  return unless my $url = shift @urls;

  $ua->get($url => __SUB__);

  $count++;
};

$fetch->($ua) for 1 .. 8;
$promise->wait;

The next example does away with the explicitly managed promise, and instead piggybacks on the get_p functionality.

sub ($tx = undef) {
  say $tx->req->url if $tx;    # $tx exists if responding as a callback

  return unless my $url = shift @urls;
  $ua->get_p($url)->then(__SUB__);
  }
  ->() for 1 .. 8;

Mojo::IOLoop->start unless Mojo::IOLoop->is_running;

I wonder whether one of these would sit better with the reviewers.

kathackeray avatar Mar 10 '22 14:03 kathackeray

I was thinking about the new approaches for some time and they are cool, but this is a documentation and this rise the complexity in my eyes a lot. The first one with some additional comments could be ok, the second one is dope.

Sadrak avatar Mar 11 '22 07:03 Sadrak

Yea, I do agree they're a little more difficult to follow, especially without comments.

The sticking point for this issue passing review seems to be the assignment:

  • @jhthorsen vetoed my fetch; $fetch = sub { on account of a circular reference
  • @kraih seemed to veto my $fetch = __SUB__; on account of it being unnecessary (preferring the approach @jhthorsen vetoed)

In testing I've been unable to isolate tangible problems with either approach. But as the new approaches I've put forward avoid assignment of the sub altogether, I figured we might persuade the reviewers to let one of those pass (heavily commented), if not one of your originals.

I agree with you that it is important to have a correct example in the docs for this. Especially as it has been tricky to settle on the correct approach even amongst the maintainers.

kathackeray avatar Mar 11 '22 08:03 kathackeray

It's unfortunate this has not progressed. And now the CI is broken. Would have liked to have it in the next release.

kraih avatar Sep 10 '22 18:09 kraih