From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.1 required=3.0 tests=ALL_TRUSTED,AWL,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id D2598203B7 for ; Mon, 28 Nov 2022 05:32:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=80x24.org; s=selector1; t=1669613566; bh=rqZhD/qiAs0FrboB8HiGGXEgZ3kdaO1yU5v1BEBPTzQ=; h=From:To:Subject:Date:In-Reply-To:References:From; b=JYeMkiV7Gvjnz/lcago15oc43ctPprJkw0tWHNs9sDUA8h9Qv8JC2fiEov4E2/8Tj F7wTNN97ArxLSoH1dLp1afWCTRSgH2s1Rtjpv/Dz2Z0rQRAsVrq8gU6vlUP/iQnHa+ u3X8sQCIUZc3ofl4CE81Kw2wK279JE5/iDdKYwnc= From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 67/95] on_destroy: support ->cancel callback Date: Mon, 28 Nov 2022 05:32:04 +0000 Message-Id: <20221128053232.291618-68-e@80x24.org> In-Reply-To: <20221128053232.291618-1-e@80x24.org> References: <20221128053232.291618-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: We probably use this idiom elsewhere, but having this method around to make future use cases more readable is probably prudent. --- lib/PublicInbox/OnDestroy.pm | 5 ++++- t/on_destroy.t | 8 ++++++-- 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 +# Copyright (C) all contributors # License: AGPL-3.0+ 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));