Re: [RFC] Glib::Idle->add() and Glib::Timeout->add() with destroy notification parameter

On Feb 21, 2007, at 11:08 AM, Emmanuele Bassi wrote:

so I'd like to request the addition of a notify callback to
Glib::Idle->add() and Glib::Timeout->add(), set to undef by default, to be called as the destroy notification function. I can provide a patch, unless someone either shows me how to do it differently with the current Glib version, or demonstrates that my idea is pure crack and I should be
shot down. :-)

*cracks knuckles*  Easily done with available tools....

#!/usr/bin/perl -w

use strict;
use Glib;

my $loop = Glib::MainLoop->new;

Glib::Idle->add (sub {
                my $data = shift;
                print "IDLE $data->{count}\n";
                return --$data->{count};
        # Note: this works because the only reference to this object
# is the one stored in the idle event source we're creating.
        #       If you create it in two steps, make sure your other
        #       reference goes away!
        GSourceDataWithDestroyNotifier->new (
                count => 10,
                callback => sub {
                        print "destroyed!\n";


print "exiting\n";

package GSourceDataWithDestroyNotifier;

sub new {
        my $class = shift;
        return bless { @_ }, $class;

        my $self = shift;
        $self->{callback}->($self) if exists $self->{callback} and
                                      'CODE' eq ref $self->{callback};


However, i do admit that it can seem like quite a lot of overkill and overhead to have to create a new class for each GSource for which you want a destruction notification.

Your example application of the destruction notification is a good example of why simply cleaning up in the last call to the idle or timeout is not sufficient; if the source is removed by Glib::Source::remove(), that last call may not happen, and you would have to trigger the cleanup by hand.

I think it should apply to Glib::IO::add_watch(), too.

Oh, and we're missing the bindings for the primitives to create your own GSource implementations in perl...

So, make a patch, and let's have a look.  :-)

I hate to break it to you, but magic data pixies don't exist.
  -- Simon Cozens

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]