Actions in Synergy
I've been working on a JavaScript server-side framework, Synergy, based on Mozilla's Rhino JavaScript engine.
A little about actions in Synergy...
There are no controllers in Synergy: just actions. A Synergy action is a function that takes a request object as it's single parameter and returns a response object. Functional programming.
For example, the simplest action would be the following.
function(request) {
return {
body: 'hello, world'
};
}
The response status and content type can be specified but for this example the defaults of 200 and text/html, respectively, are sufficient.
We need to register this action with the Synergy framework. The lowest level way to register an action is to use Synergy's pushAction function
pushAction(
function(request) {
return request.path == '/speak';
},
function(request) {
return {
body: 'hello, world'
};
}
);
The pushAction function takes two parameters: the "conditional" function and the action itself.
When a request arrives to the server, the Synergy framework loops through all the registered conditional functions, calling each. The first conditional function to return true, causes the associated action to be called as the handler for that request.
The above is verbose. We can build and use higher-level functions based on pushAction. In a Rails-like style we could write
mapConnect('sayHello', {path:'/speak'});
function sayHello(request) {
return {
body: 'hello, world'
};
}
The mapConnect function creates the conditional and action functions required by pushAction and then calls pushAction for you.
If mapConnect does not fit a particular need then the developer is encouraged to dip down through the levels of abstraction to the function that suits the problem. Higher-level functions can be built up as needed from that level. Bottom-up programming.
Comments
Have something to write? Comment on this article.
I'm really looking forward to this: a working SSJS framework is something I really miss.
Sounds very interesting. I look forward to hearing more.
Have something to write? Comment on this article.
feed
It's exciting to see an example of "real" Synergy code. The expressiveness of JavaScript could allow for some real elegance.