Skip to content

Instantly share code, notes, and snippets.

View jed's full-sized avatar

Jed Schmidt jed

View GitHub Profile
@jed
jed / promises-roundup.js
Created January 30, 2010 19:59
brainstorming promise syntax
// a few ideas for unifying async "promises"
// there are three types of callbacks:
function callback( data ){ sys.puts( "called on both success and error" ) }
function success( data ){ sys.puts( "called on success only" ) }
function error( data ){ sys.puts( "called on error only" ) }
// for any async function, offer a single optional callback.
posix.cat( "config.json", callback );
@jed
jed / fab3.js
Created February 13, 2010 23:51
// thoughts for v3 of the (fab) API.
//
// each handler gets two function arguments:
// (1) a stream back to the last app, and
// (2) a stream forward to the next app, or a 404 if there is none.
//
// (i haven't figured out elegant names, so let's just call them respond for and request)
//
// all information about the request is streamed, so that you need to
//
@jed
jed / app.js
Created February 16, 2010 13:20
module.exports = require( "fab" )
()
( "/page1", "page1" )
( "/page2", "page2" )
()
/*
about (fab) v3
==============
the first version of (fab) was mostly a toy project, to see how function
chaining could help create a concise javascript DSL for building web
apps.
the second version of (fab) smoothed out some edges and focused on the
require("http").createServer( function( req, res ) {
res.write( "this will silently hang because sendHeader was not called" )
res.close();
}).listen( 8000 )
var
assert = require( "assert" ),
test = require( "fab/tests" );
function myBinaryApp( app ) {
return function() {
require( "sys" ).puts( "myBinaryApp called..." )
return app.call( this );
}
}
// this is an example of what the first pass at native (fab) templates will look like:
with ( require( "fab" ) ) fab
( listen, 0xFAB )
( /\// )
( /1/ )
( "This is a template with no variables: a unary app" )
// the upcoming (fab) rewrite is also sympathetic to folks that don't care for parentheses:
with ( require( "fab" ) ) fab
( listen, 0xFAB
, /\//
, /1/
, "This is a template with no variables: a unary app"

changes coming in (fab) v0.5.0

here's a quick outline of some of the stuff i'm working on in the next release of (fab). once i have the internals working again, i'll push a branch for folks to play with.

100% async

right now, (fab) is mostly async, but relies on sync in one case: apps need to return a listener if they require more information to finish. this is a problem because apps may not know if they need more information until they hear from their upstream apps.

for example, a template app needs to return a function based on a string, but that string may come from an async file or http request, so the app can't reliably respond immediately.

// async: manually sets the length of a function by wrapping it in another with the desired length
fab.arity = function( num ) {
var fn = function(_){ fn.apply( this, arguments ) }
, ends = [ "this(", ")" ]
, args = [];
while ( --num ) args[ num - 1 ] = "_" + num;
this( Function( "fn", ends.join( fn ).replace( "_", args ) ) );