The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Mojo::Reactor::Glib - Glib::MainLoop backend for Mojo

VERSION

Version 0.002

I hope I need not to emphasise that this is in VERY EARLY STAGES OF DEVELOPMENT.

SYNOPSIS

Mojo::Reactor::Glib is a backend for Mojo::Reactor, build on top of the Glib main loop, allowing you to use various Mojo(licious) modules within a Glib or Gtk program.

    BEGIN {
        $ENV{MOJO_REACTOR} = 'Mojo::Reactor::Glib';
    }

    use Gtk2 -init;
    use Mojo::UserAgent;

    my $ua = Mojo::UserAgent->new();

    Glib::Timeout->add(1000, sub {
        $ua->get('http://example.com/' => sub {
            my ($ua, $tx) = @_;
            say $tx->res->body;
        });
        return Glib::SOURCE_CONTINUE;
    });

    Gtk2->main();

METHODS

Mojo::Reactor::Glib->new()

Just the constructor. You probably won't ever call it yourself.

$r->again($id)

Runs the timer known by $id again

$r->io($handle, $cb)

Assigns a callback function $cb to the IO handle $handle. This is required before you can use watch.

Returns the reactor $r so you can chain the calls.

$r->is_running()

Returns true if the loop is running, otherwise returns false

$r->one_tick()

Does a single Glib::MainLoop iteration. Returns true if events were dispatched during this iteration (whether or not they had been Mojo::Reactor::Glib events), false if nothing happened.

$r->recurring($after_sec, $cb)

Starts a recurring timer that beats every $after_sec seconds (N.B. Glib allows for millisecond granularity), which will result in $cb being fired.

Returns the Glib::Timeout ID that you can use to stop the timer.

See also timer

$r->remove($id)

Removes the timer identified by $id, returning true if this was successful, and a false-ish value otherwise.

$r->reset()

Stops all timers and watches.

$r->start()

Starts the loop if it isn't already running.

$r->stop()

Stops the loop.

$r->timer($after_sec, $cb)

Starts a one-shot timer that beats after $after_sec seconds (N.B. Glib allows for millisecond granularity), which will result in $cb being fired.

Returns the Glib::Timeout ID that you can use to stop or again the timer.

See also recurring.

$r->watch($handle, $read, $write)

Adds an IO watch for $read or $write (booleans) on $handle. If both $read and $write are false, it removes the watch.

Requires io to be run on $handle first, as that associates the callback function with the handle.

See also io.

AUTHOR

Ralesk <ralesk at cpan.org>

BUGS

Please report issues at https://bitbucket.org/ralesk/mojo-reactor-glib/issues

KNOWN ISSUES

  • Breaks on Win32, Mojo::UserAgent can't do non-blocking calls at least.

  • Can't latch onto an existing Gtk event loop (no API for that), not sure if we actually should be able to or if we're good here in a sub-loop.

LICENCE AND COPYRIGHT

Copyright (C) 2014 Henrik Pauli

This program is free software; you can redistribute it and/or modify it under the terms of the Artistic License (2.0). You may obtain a copy of the full licence at:

http://www.perlfoundation.org/artistic_license_2_0