Jifty::Event - Event objects for publish/subscribe communication


Jifty documentation Contained in the Jifty distribution.

Index


Code Index:

NAME

Top

Jifty::Event - Event objects for publish/subscribe communication

DESCRIPTION

Top

An event object from the Jifty::PubSub stream.

METHODS

Top

new($payload)

Constructor. Takes any kind of payload and blesses a scalar reference to it into an Event object.

publish()

Inserts the event into the pubsub stream. If Jifty is configured into synchronous republishing, then this method runs a republish on itself with all current subscriptions implicitly. If not, it's simply inserted into its main channel for asynchronous republishing later.

filter(@query)

Takes multiple class-specific queries, which are evaluated in order by calling match.

republish(@query)

Run filter with the queries; if they all succeed, the event is republished into that query-specific channel.

encode_queries(@query)

Encode queries into some sort of canonical MD5 encoding.

match($query)

Takes a class-specific query and returns whether it matches.

You almost always want to override this; the default implementation simply always return true;

render_arguments()

A list of additional things to push into the %ARGS of the region that is about to render this event; see Jifty::Subs::Render for more information.

data()

This event's payload as a scalar value.


Jifty documentation Contained in the Jifty distribution.
use warnings;
use strict;

package Jifty::Event;

use Jifty::YAML;
use Digest::MD5 qw(md5_hex);
use vars qw/%PUBLISHER/;

sub new {
    my $class   = shift;
    my $payload = shift;
    bless \$payload, $class;
}

sub publish {
    my $self  = shift;
    my $class = ref($self) || $self;

    return undef unless (Jifty->config->framework('PubSub')->{'Enable'});

    # Always publish to the main stream (needed for async & debugging)
    # if ($ASYNC || $DEBUGGING) {
    #    ($PUBLISHER{$class} ||= Jifty->bus->new_publisher($class))->msg($$self);
    #    return;
    # }

    # Synchronized auto-republishing
    # TODO - Prioritize current-user subscriptions first?
    my $subscriptions = Jifty->bus->modify("$class-subscriptions") || {};
    while (my ($channel, $queries) = each %$subscriptions) {
        if ($self->filter(@$queries)) {
            ($PUBLISHER{$channel} ||= Jifty->bus->new_publisher($channel))->msg($$self);
        }
    }
}

sub filter {
    my $self = shift;
    $self->match($_) or return 0 for @_;
    return 1;
}

sub republish {
    my $self = shift;
    $self->filter(@_) or return;

    my $channel = $self->encode_queries(@_);
    ($PUBLISHER{$channel} ||= Jifty->bus->new_publisher($channel))->msg($$self);
}


sub encode_queries {
    my $self    = shift;
    my $class   = ref($self) || $self;
    return $class unless @_;

    return $class . '-' . md5_hex(join('', sort map { Jifty::YAML::Dump($_) } @_));
}


sub match {
    1;
}

sub render_arguments {
    ();
}

sub data {
    ${$_[0]}
}


1;