[Jifty-commit] r5674 - in jifty/trunk: lib/Jifty lib/Jifty/Subs share/web/static/js share/web/templates/=

Jifty commits jifty-commit at lists.jifty.org
Thu Aug 7 15:41:59 EDT 2008


Author: alexmv
Date: Thu Aug  7 15:41:48 2008
New Revision: 5674

Modified:
   jifty/trunk/   (props changed)
   jifty/trunk/lib/Jifty/Event.pm
   jifty/trunk/lib/Jifty/Subs.pm
   jifty/trunk/lib/Jifty/Subs/Render.pm
   jifty/trunk/share/web/static/js/jifty_subs.js
   jifty/trunk/share/web/templates/=/subs

Log:
 r35489 at kohr-ah:  chmrr | 2008-08-07 15:41:32 -0400
  * Storable isn't always consistent, based on if ints were used as
 strings at any point.  YAML it instead.
  * Coalesce region updates in PubSub
  * Not-quite-working effect support for pubsub updates


Modified: jifty/trunk/lib/Jifty/Event.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Event.pm	(original)
+++ jifty/trunk/lib/Jifty/Event.pm	Thu Aug  7 15:41:48 2008
@@ -3,7 +3,7 @@
 
 package Jifty::Event;
 
-use Storable 'nfreeze';
+use Jifty::YAML;
 use Digest::MD5 qw(md5_hex);
 use vars qw/%PUBLISHER/;
 
@@ -100,8 +100,7 @@
     my $class   = ref($self) || $self;
     return $class unless @_;
 
-    local $Storable::canonical = 1;
-    return $class . '-' . md5_hex(join('', sort map { Storable::nfreeze($_) } @_));
+    return $class . '-' . md5_hex(join('', sort map { Jifty::YAML::Dump($_) } @_));
 }
 
 

Modified: jifty/trunk/lib/Jifty/Subs.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Subs.pm	(original)
+++ jifty/trunk/lib/Jifty/Subs.pm	Thu Aug  7 15:41:48 2008
@@ -57,6 +57,16 @@
 
 The path of the fragment used to render items matching this subscription
 
+=item effect
+
+The effect to use when showing the region, if any.
+
+=item coalesce
+
+If multiple events would cause the update of the given region with the
+same C<render_with> path, merge them and only render the region once,
+with the latest update.
+
 =back
 
 =cut
@@ -69,6 +79,8 @@
         return undef;
     }
 
+    $args->{coalesce} = 1 if not exists $args->{coalesce} and $args->{mode} eq "Replace";
+
     my $id          = ($args->{window_id} || Jifty->web->session->id);
     my $event_class = Jifty->app_class("Event", $args->{class});
 
@@ -95,7 +107,7 @@
         "$id-render" => sub {
             $_->{$channel}{$region} = {
                 map { $_ => $args->{$_} }
-                    qw/render_with region arguments mode/
+                    qw/render_with region arguments mode effect coalesce/
             };
         }
     );

Modified: jifty/trunk/lib/Jifty/Subs/Render.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Subs/Render.pm	(original)
+++ jifty/trunk/lib/Jifty/Subs/Render.pm	Thu Aug  7 15:41:48 2008
@@ -40,47 +40,67 @@
     # it's equivalent to ->modify("$id-render", sub { $_ }).
     my $render = Jifty->bus->modify("$id-render");
 
+    my %coalesce;
     while ( my ( $channel, $msgs ) = each(%$got) ) {
+        # Channel name is always App::Event::Class-MD5QUERIES
+        my $event_class = $channel;
+        $event_class =~ s/-.*//;
+
+        unless ( UNIVERSAL::can( $event_class => 'new' ) ) {
+            Jifty->log->error("Receiving unknown event $event_class from the Bus");
+            $event_class = Jifty->app_class("Event");
+        }
+
         foreach my $rv (@$msgs) {
 
             # XXX - We don't yet use $timestamp here.
             my ( $timestamp, $msg ) = @$rv;
 
-            # Channel name is always App::Event::Class-MD5QUERIES
-            my $event_class = $channel;
-            $event_class =~ s/-.*//;
-
-            unless ( UNIVERSAL::can( $event_class => 'new' ) ) {
-                Jifty->log->error("Receiving unknown event $event_class from the Bus");
-                $event_class = Jifty->app_class("Event");
-            }
-
-            Jifty->log->debug("Rendering $channel event $msg");
-
             for my $render_info (values %{$render->{$channel}}) {
-                my $region      = Jifty::Web::PageRegion->new(
-                    name => $render_info->{region},
-                    path => $render_info->{render_with},
-                );
-                delete Jifty->web->{'regions'}{ $region->qualified_name };
-
-                # Finally render the region.  In addition to the user-supplied arguments
-                # in $render_info, we always pass the target $region and the event object
-                # into its %ARGS.
-                my $region_content = '';
-                my $event_object   = $event_class->new($msg);
-                $region->render_as_subrequest( \$region_content,
-                    {   %{ $render_info->{arguments} || {} },
-                        event => $event_object,
-                        $event_object->render_arguments,
-                    }
-                );
-                $callback->( $render_info->{mode}, $region->qualified_name, $region_content);
-                $sent++;
+                if ($render_info->{coalesce} and $render_info->{mode} eq "Replace") {
+                    my $hash = Digest::MD5::md5_hex( YAML::Dump([$render_info->{region}, $render_info->{render_with}] ) );
+                    Jifty->log->debug("Coalesced duplicate region @{[$render_info->{region}]} with @{[$render_info->{render_with}]} from $channel event $msg") if exists $coalesce{$hash};
+                    $coalesce{$hash} = [ $timestamp, $event_class, $msg, $render_info ] unless $coalesce{$hash} and $coalesce{$hash}[0] > $timestamp;
+                } else {
+                    Jifty->log->debug("Rendering $channel event $msg in @{[$render_info->{region}]} with @{[$render_info->{render_with}]}");
+                    render_single( $event_class, $msg, $render_info, $callback );
+                    $sent++;
+                }
             }
         }
     }
+
+    for (values %coalesce) {
+        my (undef, $event_class, $msg, $render_info) = @{$_};
+        Jifty->log->debug("Rendering @{[$render_info->{region}]} with @{[$render_info->{render_with}]} for $event_class");
+        render_single( $event_class, $msg, $render_info, $callback );
+        $sent++;
+    }
+
     return ($sent);
 }
 
+sub render_single {
+    my ($class, $msg, $render_info, $callback) = @_;
+    my $region = Jifty::Web::PageRegion->new(
+        name => $render_info->{region},
+        path => $render_info->{render_with},
+    );
+
+    # Finally render the region.  In addition to the user-supplied arguments
+    # in $render_info, we always pass the target $region and the event object
+    # into its %ARGS.
+    my $region_content = '';
+    my $event_object   = $class->new($msg);
+    $region->enter;
+    $region->render_as_subrequest( \$region_content,
+        {   %{ $render_info->{arguments} || {} },
+            event => $event_object,
+            $event_object->render_arguments,
+        }
+    );
+    $callback->( $render_info->{mode}, $region->qualified_name, $region_content, $render_info->{effect});
+    $region->exit;
+}
+
 1;

Modified: jifty/trunk/share/web/static/js/jifty_subs.js
==============================================================================
--- jifty/trunk/share/web/static/js/jifty_subs.js	(original)
+++ jifty/trunk/share/web/static/js/jifty_subs.js	Thu Aug  7 15:41:48 2008
@@ -16,8 +16,9 @@
 
     var onPushHandler = function(t) {
     	var mode = t.getAttribute('mode');
+    	var effect =  t.getAttribute('effect');
     	var rid =  t.firstChild.getAttribute('id');
-    	var f = { region: rid, path: '', mode: mode };
+    	var f = { region: rid, path: '', mode: mode, effect: effect };
 
         // If SinglePlugin is enabled, region name will be prefixed
         // "__page-" by the time that region was rendered. Therefore

Modified: jifty/trunk/share/web/templates/=/subs
==============================================================================
--- jifty/trunk/share/web/templates/=/subs	(original)
+++ jifty/trunk/share/web/templates/=/subs	Thu Aug  7 15:41:48 2008
@@ -41,8 +41,8 @@
     Jifty::Subs::Render->render(
         Jifty->web->session->id,
         sub {
-            my ( $mode, $name, $content ) = @_;
-            $writer->startTag( "pushfrag", mode => $mode );
+            my ( $mode, $name, $content, $effect ) = @_;
+            $writer->startTag( "pushfrag", mode => $mode, ($effect? (effect => $effect) : ()) );
             $writer->startTag( "fragment", id   => $name );
             $writer->dataElement( "content", $content );
             $writer->endTag();


More information about the Jifty-commit mailing list