Created
May 12, 2010 19:22
-
-
Save sspencer/399025 to your computer and use it in GitHub Desktop.
NodeJS BrowserPlus Service Sketch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A rough sketch of how a NodeJS service might look | |
// Take 2 - keep services flat. | |
service = require('browserplus').service; | |
log = require('browserplus').log; | |
service.name = "HelloWorld"; | |
service.version = "1.0.1"; | |
service.doc = "A hello world service for BrowserPlus"; | |
// A hook that is called just after the service is loaded | |
service.atStartup = function() { | |
log('info', "we're starting up!"); | |
}; | |
// A hook that is called just before the service is unloaded | |
service.atShutdown = function() { | |
log('info', "we're shutting down!"); | |
}; | |
// initialize is a reserved word and is the constructor, invoked | |
// when a page allocates an instance of this service | |
service.initialize = function(args) { | |
log('info', "new session started for " + args.uri); | |
}; | |
function myPrivateFunc() { | |
// functions not exported are not visible | |
// outside of this module | |
} | |
// exported below so it can be called through browserplus | |
function hello(trans, arg) { | |
// args is how you can access arguments. callback arguments | |
// are proper objects that support an "invoke" method | |
args.cb.invoke("Hi there " + args.who); | |
trans.complete("hello " + args.who); | |
}; | |
// first param is function to be exported | |
// second param is function doc | |
// third param+ is argument doc | |
service.export( | |
hello, | |
"A function that says hello", | |
"<name: string> the person's name", | |
"[greeting: string] the optional greeting (defaults to Hello)", | |
"[cb: callback] the function to invoke with the greeting" | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment