about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2022-11-28 05:32:04 +0000
committerEric Wong <e@80x24.org>2022-11-28 23:38:57 +0000
commit4452b5ebd8f202d72260325768512230c93ef6f2 (patch)
treea2574f084f0042ebadb4f0aa5c009bac57bfbc9e
parent02d0dfd3f22f8f7c5d1e189dbe29034b9da9f510 (diff)
downloadpublic-inbox-4452b5ebd8f202d72260325768512230c93ef6f2.tar.gz
on_destroy: support ->cancel callback
We probably use this idiom elsewhere, but having this method
around to make future use cases more readable is probably prudent.
-rw-r--r--lib/PublicInbox/OnDestroy.pm5
-rw-r--r--t/on_destroy.t8
2 files changed, 10 insertions, 3 deletions
diff --git a/lib/PublicInbox/OnDestroy.pm b/lib/PublicInbox/OnDestroy.pm
index 615bc450..d9a6cd24 100644
--- a/lib/PublicInbox/OnDestroy.pm
+++ b/lib/PublicInbox/OnDestroy.pm
@@ -1,13 +1,16 @@
-# Copyright (C) 2020-2021 all contributors <meta@public-inbox.org>
+# Copyright (C) all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 
 package PublicInbox::OnDestroy;
+use v5.12;
 
 sub new {
         shift; # ($class, $cb, @args)
         bless [ @_ ], __PACKAGE__;
 }
 
+sub cancel { @{$_[0]} = () }
+
 sub DESTROY {
         my ($cb, @args) = @{$_[0]};
         if (!ref($cb) && $cb) {
diff --git a/t/on_destroy.t b/t/on_destroy.t
index 0de67d0b..e7945100 100644
--- a/t/on_destroy.t
+++ b/t/on_destroy.t
@@ -1,6 +1,5 @@
 #!perl -w
-use strict;
-use v5.10.1;
+use v5.12;
 use Test::More;
 require_ok 'PublicInbox::OnDestroy';
 my @x;
@@ -25,6 +24,11 @@ $od = PublicInbox::OnDestroy->new($$, sub { $tmp = $$ });
 undef $od;
 is($tmp, $$, '$tmp set to $$ by callback');
 
+$od = PublicInbox::OnDestroy->new($$, sub { $tmp = 'foo' });
+$od->cancel;
+$od = undef;
+isnt($tmp, 'foo', '->cancel');
+
 if (my $nr = $ENV{TEST_LEAK_NR}) {
         for (0..$nr) {
                 $od = PublicInbox::OnDestroy->new(sub { @x = @_ }, qw(x y));