[Jifty-commit] jifty branch, pubsub-plugin, created. a8f1a4e95984aef7d801f7c07e0d567f27fa5809

Jifty commits jifty-commit at lists.jifty.org
Sun Feb 28 15:30:58 EST 2010


The branch, pubsub-plugin has been created
        at  a8f1a4e95984aef7d801f7c07e0d567f27fa5809 (commit)

- Log -----------------------------------------------------------------
commit a8f1a4e95984aef7d801f7c07e0d567f27fa5809
Author: Chia-liang Kao <clkao at clkao.org>
Date:   Thu Feb 11 22:22:35 2010 +0800

    Make jifty subs work again as a plugin.

diff --git a/app.psgi b/app.psgi
new file mode 100644
index 0000000..33b225c
--- /dev/null
+++ b/app.psgi
@@ -0,0 +1,3 @@
+use Jifty;
+Jifty->new;
+Jifty->handler->psgi_app;
\ No newline at end of file
diff --git a/lib/Jifty/Config.pm b/lib/Jifty/Config.pm
index 7a981d8..017a43f 100644
--- a/lib/Jifty/Config.pm
+++ b/lib/Jifty/Config.pm
@@ -596,6 +596,7 @@ sub update_config {
         );
 
         push (@{$config->{'framework'}->{'Plugins'}},
+            { PubSub        => {}, },
             { Deflater      => {}, }
         );
     }
diff --git a/lib/Jifty/Plugin/PubSub.pm b/lib/Jifty/Plugin/PubSub.pm
new file mode 100644
index 0000000..7c23a4d
--- /dev/null
+++ b/lib/Jifty/Plugin/PubSub.pm
@@ -0,0 +1,35 @@
+use strict;
+use warnings;
+
+package Jifty::Plugin::PubSub;
+use base 'Jifty::Plugin';
+use Plack::Builder;
+use Plack::Util;
+
+=head1 NAME
+
+Jifty::Plugin::Deflater - Handles Accept-Encoding and compression
+
+=head1 SYNOPSIS
+
+# In your jifty config.yml under the framework section:
+
+  Plugins:
+    - PubSub: {}
+
+# You should put defalter at the end of the plugins list
+
+=head1 DESCRIPTION
+
+This plugin provides Accept-Encoding handling.
+
+=cut
+
+sub init {
+    my $self = shift;
+    return if $self->_pre_init;
+
+    Jifty->web->add_javascript( 'jifty_subs.js' );
+}
+
+1;
diff --git a/lib/Jifty/Plugin/PubSub/Dispatcher.pm b/lib/Jifty/Plugin/PubSub/Dispatcher.pm
new file mode 100644
index 0000000..6c48c3b
--- /dev/null
+++ b/lib/Jifty/Plugin/PubSub/Dispatcher.pm
@@ -0,0 +1,83 @@
+use warnings;
+use strict;
+
+package Jifty::Plugin::PubSub::Dispatcher;
+
+=head1 NAME
+
+Jifty::Plugin::PubSub::Dispatcher - Dispatcher for PubSub plugin
+
+=head1 DESCRIPTION
+
+=cut
+
+use Jifty::Dispatcher -base;
+
+on '/=/subs' => stream {
+    my $req = Jifty->web->request;
+    my $forever = $req->parameters->{'forever'} || 0;
+    my $web = Jifty->web;
+    return sub {
+        local $Jifty::WEB = $web;
+
+        my $respond = shift;
+        my $w = $respond->([200,
+                            ['Content-Type' => 'text/html; charset=utf-8',
+                             'Pragma' => 'no-cache',
+                             'Cache-control' => 'no-cache' ] ]);
+
+        use IO::Handle::Util qw(io_prototype);
+
+        my $writer = XML::Writer->new( OUTPUT => io_prototype
+                                           print => sub { shift;
+                                                          $w->write(shift);
+                                                      } );
+        $writer->xmlDecl( "UTF-8", "yes" );
+
+        my $begin = <<'END';
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
+ "http://www.w3.org/TR/2002/REC-xhtml1-20020801/DTD/xhtml1-strict.dtd">
+<html><head><title></title></head>
+END
+
+        chomp $begin;
+
+        if ($forever) {
+            my $whitespace = " " x ( 1024 - length $begin );
+            $begin =~ s/<body>$/$whitespace<body>/s;
+        }
+
+        $w->write($begin);
+        $writer->startTag("body");
+
+        local $SIG{PIPE} = sub {
+            die "ABORT";
+        };
+
+        my $loops;
+        while (Jifty->config->framework('PubSub')->{'Enable'}) {
+            Jifty->web->out(" ") if ++$loops % 10 == 0;
+            my $sent = write_subs_once($writer);
+            last if ( $sent && !$forever );
+            sleep 1;
+        }
+        $writer->endTag();
+    };
+};
+
+sub write_subs_once {
+    my $writer = shift;
+    Jifty::Subs::Render->render(
+        Jifty->web->session->id,
+        sub {
+            my ( $mode, $name, $content, $attrs ) = @_;
+            $writer->startTag( "pushfrag", mode => $mode, %{$attrs || {}} );
+            $writer->startTag( "fragment", id   => $name );
+            $writer->dataElement( "content", $content );
+            $writer->endTag();
+            $writer->endTag();
+        }
+    );
+}
+
+1;
diff --git a/lib/Jifty/Subs/Render.pm b/lib/Jifty/Subs/Render.pm
index f20e54d..019186b 100644
--- a/lib/Jifty/Subs/Render.pm
+++ b/lib/Jifty/Subs/Render.pm
@@ -100,7 +100,7 @@ sub render_single {
 
     my $event_object   = $class->new($msg);
     # Region's arguments come from explicit arguments only
-    $region->arguments( $render_info->{arguments} );
+    $region->arguments( $render_info->{arguments} || {} );
 
     $region->enter;
     # Also provide an 'event' argument, and fill in the render
diff --git a/lib/Jifty/View/Declare/CoreTemplates.pm b/lib/Jifty/View/Declare/CoreTemplates.pm
index dec6de5..ad5b88e 100644
--- a/lib/Jifty/View/Declare/CoreTemplates.pm
+++ b/lib/Jifty/View/Declare/CoreTemplates.pm
@@ -35,58 +35,6 @@ This library contains templates that Jifty can't function without:
 
 These templates are still in masonland. we're doign something wrong with escaping in them
 
-
-template '__jifty/subs' => sub {
-    my ($forever) = get(qw(forever)) || 1;
-
-    Jifty->web->response->content_type("text/html; charset=utf-8");
-    Jifty->web->response->header('Pragma' => 'no-cache');
-    Jifty->web->response->header('Cache-control' => 'no-cache');
-
-    my $writer = XML::Writer->new;
-    $writer->xmlDecl( "UTF-8", "yes" );
-
-    my $begin = <<'END';
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
- "http://www.w3.org/TR/2002/REC-xhtml1-20020801/DTD/xhtml1-strict.dtd">
-<html><head><title></title></head>
-END
-    chomp $begin;
-
-    if ($forever) {
-        my $whitespace = " " x ( 1024 - length $begin );
-        $begin =~ s/<body>$/$whitespace/s;
-    }
-
-    Jifty->web->out($begin);
-    $writer->startTag("body");
-
-    while (1) {
-        my $sent = _write_subs_once($writer);
-        flush STDOUT;
-        last if ( $sent && !$forever );
-        sleep 1;
-    }
-    $writer->endTag();
-    return;
-
-};
-
-sub _write_subs_once {
-    my $writer = shift;
-    Jifty::Subs::Render->render(
-        Jifty->web->session->id,
-        sub {
-            my ( $mode, $name, $content ) = @_;
-            $writer->startTag( "pushfrag", mode => $mode );
-            $writer->startTag( "fragment", id   => $name );
-            $writer->dataElement( "content", $content );
-            $writer->endTag();
-            $writer->endTag();
-        }
-    );
-}
-
 template '__jifty/autocomplete.xml' => sub {
 
     # Note: the only point to this file is to set the content_type; the actual
diff --git a/lib/Jifty/View/Declare/Page.pm b/lib/Jifty/View/Declare/Page.pm
index d10fb4b..d4d596e 100644
--- a/lib/Jifty/View/Declare/Page.pm
+++ b/lib/Jifty/View/Declare/Page.pm
@@ -214,6 +214,10 @@ sub render_jifty_page_detritus {
     with( id => "jifty-wait-message", style => "display: none" ),
         div { _('Loading...') };
 
+    # XXX: pubsub is now a plugin. this should be a hook registered by
+    # plugins, perhaps just use after_include_javascript?
+
+
     # This is required for jifty server push.  If you maintain your own
     # wrapper, make sure you have this as well.
     if ( Jifty->config->framework('PubSub')->{'Enable'} && Jifty::Subs->list )
diff --git a/lib/Jifty/Web.pm b/lib/Jifty/Web.pm
index 89312bc..2be8a96 100644
--- a/lib/Jifty/Web.pm
+++ b/lib/Jifty/Web.pm
@@ -43,7 +43,6 @@ __PACKAGE__->javascript_libs([qw(
     behaviour.js
     jifty.js
     jifty_utils.js
-    jifty_subs.js
     jifty_smoothscroll.js
     calendar.js
     datetime.js
diff --git a/share/web/static/js/jifty_subs.js b/share/plugins/Jifty/Plugin/PubSub/web/static/js/jifty_subs.js
similarity index 100%
rename from share/web/static/js/jifty_subs.js
rename to share/plugins/Jifty/Plugin/PubSub/web/static/js/jifty_subs.js
diff --git a/share/web/templates/=/subs b/share/web/templates/=/subs
deleted file mode 100644
index 2860b2d..0000000
--- a/share/web/templates/=/subs
+++ /dev/null
@@ -1,61 +0,0 @@
-<%args>
-$forever => 1
-</%args>
-<%init>
-
-Jifty->web->response->content_type("text/html; charset=utf-8");
-Jifty->web->response->header('Pragma' => 'no-cache');
-Jifty->web->response->header('Cache-control' => 'no-cache');
-Jifty->handler->send_http_header;
-
-my $writer = XML::Writer->new;
-$writer->xmlDecl( "UTF-8", "yes" );
-
-my $begin = <<'END';
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
- "http://www.w3.org/TR/2002/REC-xhtml1-20020801/DTD/xhtml1-strict.dtd">
-<html><head><title></title></head>
-END
-chomp $begin;
-
-if ($forever) {
-    my $whitespace = " " x ( 1024 - length $begin );
-    $begin =~ s/<body>$/$whitespace<body>/s;
-}
-
-$m->print($begin);
-$m->flush_buffer;
-Jifty->handler->buffer->flush_output;
-$writer->startTag("body");
-
-local $SIG{PIPE} = sub {
-    die "ABORT";
-};
-
-my $loops;
-while (Jifty->config->framework('PubSub')->{'Enable'}) {
-    Jifty->web->out(" ") if ++$loops % 10 == 0;
-    my $sent = write_subs_once($writer);
-    Jifty->handler->buffer->flush_output;
-    flush STDOUT;
-    last if ( $sent && !$forever );
-    sleep 1;
-}
-$writer->endTag();
-return;
-
-sub write_subs_once {
-    my $writer = shift;
-    Jifty::Subs::Render->render(
-        Jifty->web->session->id,
-        sub {
-            my ( $mode, $name, $content, $attrs ) = @_;
-            $writer->startTag( "pushfrag", mode => $mode, %{$attrs || {}} );
-            $writer->startTag( "fragment", id   => $name );
-            $writer->dataElement( "content", $content );
-            $writer->endTag();
-            $writer->endTag();
-        }
-    );
-}
-</%init>

-----------------------------------------------------------------------


More information about the Jifty-commit mailing list