about summary refs log tree commit homepage
path: root/lib/PublicInbox
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2016-06-21 02:58:58 +0000
committerEric Wong <e@80x24.org>2016-06-21 03:07:04 +0000
commitcf1fb806c5a4cb03833bd4276c0fb2906ebb9cc7 (patch)
treeed8f92843c31fc61fb110a220ec52cb4e3c8b58a /lib/PublicInbox
parent76d8f68dc273e54809ad69cfe49e141003f790ef (diff)
downloadpublic-inbox-cf1fb806c5a4cb03833bd4276c0fb2906ebb9cc7.tar.gz
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.
Diffstat (limited to 'lib/PublicInbox')
-rw-r--r--lib/PublicInbox/SearchView.pm8
-rw-r--r--lib/PublicInbox/View.pm35
2 files changed, 15 insertions, 28 deletions
diff --git a/lib/PublicInbox/SearchView.pm b/lib/PublicInbox/SearchView.pm
index ba258270..ae875bf7 100644
--- a/lib/PublicInbox/SearchView.pm
+++ b/lib/PublicInbox/SearchView.pm
@@ -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" .
diff --git a/lib/PublicInbox/View.pm b/lib/PublicInbox/View.pm
index 8075e4ae..b6fa2a3d 100644
--- a/lib/PublicInbox/View.pm
+++ b/lib/PublicInbox/View.pm
@@ -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;
         }