Skip to content

Instantly share code, notes, and snippets.

@back2dos
Created December 11, 2011 22:05
Show Gist options
  • Save back2dos/1463037 to your computer and use it in GitHub Desktop.
Save back2dos/1463037 to your computer and use it in GitHub Desktop.
Tink JS externals example
extern class Server {}
extern class ServerAPI implements TinkExt<Server> {
@:native listen(port:Int, ?hostname:String, ?handler:Server->Void);//when no type is defined, the target itself is returned
@:native(listen) listenToPath(path:String, ?handler:Server->Void);//both identifiers and strings are allowed to define native names
@:event request(request:ServerRequest, response:ServerResponse);
@:event(connection) socketInbound(socket:Socket);
}
//The above will now be expanded to:
extern class ServerAPI implements TinkExt<Server> {
static public inline function listen(_target:Server, port:Int, ?hostname:String, ?handler:Server->Void):Server {
untyped _target.listen(port, hostname, handler);
return _target;
}
static public inline function listenToPath(_target:Server, path:String, ?handler:Server->Void):Server {
untyped _target.listen(path, handler);
return _target;
}
static public inline function request(_target:Server, handler:ServerRequest->ServerResponse->Dynamic):Server {
untyped _target.on('request', handler);
return _target;
}
static public inline function socketInbound(_target:Server, handler:Socket):Server {
untyped _target.on('connection', handler);
return _target;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment