This chapter is intended to give you a short overview about the framework and what you might achieve with it. You should be able to judge, whether the framework is the right choice for your job or not.

Spludo is a framework for Node.JS, thus uses an event loop in the background. You need to understand what this means to benefit from this difference to other frameworks and engines.

Spludo uses the Continuable-Pattern for most framework functions. That means a function call is splitted in 2 function calls.

Instead of:

var alice = getAlice(bob);
// following code

You have to write:

getAlice(bob)(function(alice) {
    // following code
});

The important part is your following code will get executed as soon as the value for alice is there. So long: the code does not block. No matter if it takes 1ms or 10 seconds to retrieve the value. In the non-evented way, the code blocks until getValue returns - and this is what is expensive when working with I/O on file system or external systems.

Consider this in a non-Node.JS world:

function home_controller() {
    /*
     * The next command may take multiple seconds to complete.
     * The server will be blocked, until the page is downloaded.
     */
    var response_text = download_page('http://google.de');
    return response_text;
}

In Spludo you write this:

new Controller('home.html', {
    "execute": function() {
        return function(cb) {
            var responseHandler = function(response_text) {
                /* call cb as soon as we have the result from google */
                cb(response_text);
            };

            http.get('http://google.de')(reponseHandler);
        }
    }
});

The call to the system function http.get returns a function. You read right: a function. This function is called with a single argument: responseHandler. responseHandler will be called as soon as http.get has the result.

When you are fimiliar with this style of programming, you won't add a new variable for each handler (which you use only one time), but use an anonymous function instead.

In Spludo you write this:

new Controller('home.html', {
    "execute": function() {
        return function(cb) {
            http.get('http://google.de')(function(response_text) {
                /* call cb as soon as we have the result from google */
                cb(response_text);
            });
        }
    }
});

This is the reasons, why each Controllers and Views execute-Method return an function, which gets a callback as first parameter. To make super fast non-blocking applications.

Since the framework offers also a console mode and is not bound to html in any way, you can use the framework for any server or command line application.

Comments