]> Sergey Matveev's repositories - public-inbox.git/commitdiff
view: common thread walking interface
authorEric Wong <e@80x24.org>
Tue, 21 Jun 2016 02:58:58 +0000 (02:58 +0000)
committerEric Wong <e@80x24.org>
Tue, 21 Jun 2016 03:07:04 +0000 (03:07 +0000)
Since we have a common pattern, for walking threads,
extract it into a function and reduce the amount of code
we haev.

This will make it easier to switch to an event-driven interface
for getline, too.

lib/PublicInbox/SearchView.pm
lib/PublicInbox/View.pm

index ba25827077eb1f3c65efdf8908539021c0a6651e..ae875bf736d2bb3be8f28ec63d19373f856243fe 100644 (file)
@@ -172,13 +172,7 @@ sub tdump {
                fh => $fh,
        };
        $ctx->{searchview} = 1;
-       my @q = map { (0, $_) } $th->rootset;
-       while (@q) {
-               my $level = shift @q;
-               my $node = shift @q or next;
-               tdump_ent($state, $level, $node);
-               unshift @q, $level+1, $node->child, $level, $node->next;
-       }
+       PublicInbox::View::walk_thread($th, $state, *tdump_ent);
        PublicInbox::View::thread_adj_level($state, 0);
 
        $fh->write(search_nav_bot($mset, $q). "\n\n" .
index 8075e4ae93259568cb9669e5e7eea37464fa4d23..b6fa2a3d81886dd480978ea8efefd0bbfea0973a 100644 (file)
@@ -152,6 +152,17 @@ sub thread_html {
        sub { emit_thread_html($_[0], $ctx, $foot, $srch) }
 }
 
+sub walk_thread {
+       my ($th, $state, $cb) = @_;
+       my @q = map { (0, $_) } $th->rootset;
+       while (@q) {
+               my $level = shift @q;
+               my $node = shift @q or next;
+               $cb->($state, $level, $node);
+               unshift @q, $level+1, $node->child, $level, $node->next;
+       }
+}
+
 # only private functions below.
 
 sub emit_thread_html {
@@ -177,13 +188,7 @@ sub emit_thread_html {
                pre_anchor_entry($seen, $_) for (@$msgs);
                __thread_entry($state, $_, 0) for (@$msgs);
        } else {
-               my @q = map { (0, $_) } thread_results($msgs)->rootset;
-               while (@q) {
-                       my $level = shift @q;
-                       my $node = shift @q or next;
-                       thread_entry($state, $level, $node);
-                       unshift @q, $level+1, $node->child, $level, $node->next;
-               }
+               walk_thread(thread_results($msgs), $state, *thread_entry);
                if (my $max = $state->{cur_level}) {
                        $state->{fh}->write(
                                ('</ul></li>' x ($max - 1)) . '</ul>');
@@ -391,13 +396,7 @@ sub thread_skel {
                upfx => "$tpfx../",
                dst => $dst,
        };
-       my @q = map { (0, $_) } thread_results(load_results($sres))->rootset;
-       while (@q) {
-               my $level = shift @q;
-               my $node = shift @q or next;
-               skel_dump($state, $level, $node);
-               unshift @q, $level+1, $node->child, $level, $node->next;
-       }
+       walk_thread(thread_results(load_results($sres)), $state, *skel_dump);
        $ctx->{next_msg} = $state->{next_msg};
        $ctx->{parent_msg} = $parent;
 }
@@ -847,13 +846,7 @@ sub emit_index_topics {
                my $sres = $state->{srch}->query('', \%opts);
                my $nr = scalar @{$sres->{msgs}} or last;
                $sres = load_results($sres);
-               my @q = map { (0, $_) } thread_results($sres)->rootset;
-               while (@q) {
-                       my $level = shift @q;
-                       my $node = shift @q or next;
-                       add_topic($state, $level, $node);
-                       unshift @q, $level+1, $node->child, $level, $node->next;
-               }
+               walk_thread(thread_results($sres), $state, *add_topic);
                $opts{offset} += $nr;
        }