This allows callers to avoid creating expensive closures.
We no longer pass the `$now' value to callers, as none of
the callers used it.
---
 lib/PublicInbox/DS.pm           | 10 +++++-----
 lib/PublicInbox/FakeInotify.pm  | 10 ++++------
 lib/PublicInbox/WatchMaildir.pm | 15 ++++++---------
 3 files changed, 15 insertions(+), 20 deletions(-)

diff --git a/lib/PublicInbox/DS.pm b/lib/PublicInbox/DS.pm
index c46b20cba27..a3f2e76c16a 100644
--- a/lib/PublicInbox/DS.pm
+++ b/lib/PublicInbox/DS.pm
@@ -95,18 +95,18 @@ sub SetLoopTimeout {
     return $LoopTimeout = $_[1] + 0;
 }
 
-=head2 C<< PublicInbox::DS::add_timer( $seconds, $coderef ) >>
+=head2 C<< PublicInbox::DS::add_timer( $seconds, $coderef, $arg) >>
 
 Add a timer to occur $seconds from now. $seconds may be fractional, but timers
 are not guaranteed to fire at the exact time you ask for.
 
 =cut
-sub add_timer ($$) {
-    my ($secs, $coderef) = @_;
+sub add_timer ($$;$) {
+    my ($secs, $coderef, $arg) = @_;
 
     my $fire_time = now() + $secs;
 
-    my $timer = [$fire_time, $coderef];
+    my $timer = [$fire_time, $coderef, $arg];
 
     if (!@Timers || $fire_time >= $Timers[-1][0]) {
         push @Timers, $timer;
@@ -198,7 +198,7 @@ sub RunTimers {
     # Run expired timers
     while (@Timers && $Timers[0][0] <= $now) {
         my $to_run = shift(@Timers);
-        $to_run->[1]->($now) if $to_run->[1];
+        $to_run->[1]->($to_run->[2]);
     }
 
     # timers may enqueue into nextq:
diff --git a/lib/PublicInbox/FakeInotify.pm b/lib/PublicInbox/FakeInotify.pm
index df63173f083..debd2d39ae5 100644
--- a/lib/PublicInbox/FakeInotify.pm
+++ b/lib/PublicInbox/FakeInotify.pm
@@ -16,16 +16,14 @@ my $poll_intvl = 2; # same as Filesys::Notify::Simple
 
 sub poll_once {
        my ($self) = @_;
-       sub {
-               eval { $self->poll };
-               warn "E: FakeInotify->poll: $@\n" if $@;
-               PublicInbox::DS::add_timer($poll_intvl, poll_once($self));
-       };
+       eval { $self->poll };
+       warn "E: FakeInotify->poll: $@\n" if $@;
+       PublicInbox::DS::add_timer($poll_intvl, \&poll_once, $self);
 }
 
 sub new {
        my $self = bless { watch => {} }, __PACKAGE__;
-       PublicInbox::DS::add_timer($poll_intvl, poll_once($self));
+       PublicInbox::DS::add_timer($poll_intvl, \&poll_once, $self);
        $self;
 }
 
diff --git a/lib/PublicInbox/WatchMaildir.pm b/lib/PublicInbox/WatchMaildir.pm
index b82b51025e6..f36aa20aa33 100644
--- a/lib/PublicInbox/WatchMaildir.pm
+++ b/lib/PublicInbox/WatchMaildir.pm
@@ -549,8 +549,8 @@ sub watch_imap_fetch_all ($$) {
        }
 }
 
-sub imap_fetch_fork ($$$) {
-       my ($self, $intvl, $uris) = @_;
+sub imap_fetch_fork ($) { # DS::add_timer callback
+       my ($self, $intvl, $uris) = @{$_[0]};
        return if $self->{quit};
        watch_atfork_parent($self);
        defined(my $pid = fork) or die "fork: $!";
@@ -563,11 +563,6 @@ sub imap_fetch_fork ($$$) {
        PublicInbox::DS::dwaitpid($pid, \&imap_fetch_reap, $self);
 }
 
-sub imap_fetch_cb ($$$) {
-       my ($self, $intvl, $uris) = @_;
-       sub { imap_fetch_fork($self, $intvl, $uris) };
-}
-
 sub imap_fetch_reap { # PublicInbox::DS::dwaitpid callback
        my ($self, $pid) = @_;
        my $intvl_uris = delete $self->{poll_pids}->{$pid} or
@@ -579,7 +574,8 @@ sub imap_fetch_reap { # PublicInbox::DS::dwaitpid callback
                        map { $_->as_string."\n" } @$uris;
        }
        warn('I: will check ', $_->as_string, " in ${intvl}s\n") for @$uris;
-       PublicInbox::DS::add_timer($intvl, imap_fetch_cb($self, $intvl, $uris));
+       PublicInbox::DS::add_timer($intvl, \&imap_fetch_fork,
+                                       [$self, $intvl, $uris]);
 }
 
 sub watch_imap_init ($) {
@@ -625,7 +621,8 @@ sub watch_imap_init ($) {
 
        # poll all URIs for a given interval sequentially
        while (my ($intvl, $uris) = each %$poll) {
-               PublicInbox::DS::requeue(imap_fetch_cb($self, $intvl, $uris));
+               PublicInbox::DS::add_timer(0, \&imap_fetch_fork,
+                                               [$self, $intvl, $uris]);
        }
 }
 
--
unsubscribe: one-click, see List-Unsubscribe header
archive: https://public-inbox.org/meta/

Reply via email to