about summary refs log tree commit homepage
path: root/lib/PublicInbox/GetlineBody.pm
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2016-05-27 05:59:16 +0000
committerEric Wong <e@80x24.org>2016-05-27 06:02:33 +0000
commit79e11eea11dd561301a05c42ba824a7d352bd398 (patch)
treeda0db45671648d6803c9efc8e4755f5853dae03f /lib/PublicInbox/GetlineBody.pm
parent656bc41ecb1a77208adaeee6c68faed3e8328695 (diff)
downloadpublic-inbox-79e11eea11dd561301a05c42ba824a7d352bd398.tar.gz
We need to avoid circular references in the generic PSGI layer,
do it by abusing DESTROY.
Diffstat (limited to 'lib/PublicInbox/GetlineBody.pm')
-rw-r--r--lib/PublicInbox/GetlineBody.pm31
1 files changed, 31 insertions, 0 deletions
diff --git a/lib/PublicInbox/GetlineBody.pm b/lib/PublicInbox/GetlineBody.pm
new file mode 100644
index 00000000..4f8765bb
--- /dev/null
+++ b/lib/PublicInbox/GetlineBody.pm
@@ -0,0 +1,31 @@
+# Copyright (C) 2016 all contributors <meta@public-inbox.org>
+# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
+
+# Wrap a pipe or file for PSGI streaming response bodies and calls the
+# end callback when the object goes out-of-scope.
+# This depends on rpipe being _blocking_ on getline.
+package PublicInbox::GetlineBody;
+use strict;
+use warnings;
+
+sub new {
+        my ($class, $rpipe, $end, $buf) = @_;
+        bless { rpipe => $rpipe, end => $end, buf => $buf }, $class;
+}
+
+sub DESTROY { $_[0]->close }
+
+sub getline {
+        my ($self) = @_;
+        my $buf = delete $self->{buf};
+        defined $buf ? $buf : $self->{rpipe}->getline;
+}
+
+sub close {
+        my ($self) = @_;
+        delete $self->{rpipe};
+        my $end = delete $self->{end} or return;
+        $end->();
+}
+
+1;