Skip to content

Instantly share code, notes, and snippets.

@monken
Created June 26, 2011 08:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save monken/1047411 to your computer and use it in GitHub Desktop.
Save monken/1047411 to your computer and use it in GitHub Desktop.
Transition to Catalyst from Mo-AE-Plack

This document describes how to move metacpan-web from the currently asynchronous, home grown framework to Catalyst.

MetaCPAN::Web::View

The view is a Template::Alloy subclass, that defines a number of virtual methods and sets up the configuration for the Template::Alloy object. Since there is Catalyst::View::TT::Alloy already, the move towards it should be straightforward. Though, you would have to rename it to something like MetaCPAN::Web::View::HTML

MetaCPAN::Web::Model

The model class is a tiny wrapper around AnyEvent::HTTP and provides convenience methods for accessing the individual model classes. $controller->model('Author') will return an instance of MetaCPAN::Web::Model::Author. This is pretty similar to what Catalyst does.

Instead of using AnyEvent's standard CondVar, I implemented a custom one (MyCondVar) which comes with a few goodies. It allows to chain callbacks, while AE::cv can only hold one callback. It also allows to express dependencies amongst multiple CVs. In the model and controller classes, you will see code like ($cv1 & $cv2)->(sub { ... }). This means that the callback will only be called once both $cv1 and $cv2 received data.

I believe that the model classes can be left untouched. They always return a MyCondVar object which can then be handled in the controller.

MetaCPAN::Web::Controller

This is the base class for all controllers. It has an attribute view that references a MetaCPAN::Web::View instance. And a model method that allows to access the individual model classes.

The async magic happens in the call method. It calls the index method of the controller, which returns a AE::cv. This method can be ripped out entirely because Catalyst::Controller is pretty much doing the same thing. What you need to change in the controller subclasses is pretty easy. Currently we return the CV. What we need to do instead is, wait for the CV to actually return data, set $c->stash and let Catalyst do the rendering.

# MetaCPAN::Web::Controller::Author
return $cv;
# becomes
$c->stash($cv->recv);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment