Skip to content

Instantly share code, notes, and snippets.

View kriskowal's full-sized avatar

Kris Kowal kriskowal

View GitHub Profile
{
"dependencies": [
[
"nr-devtools",
{
"catalog": "http://github.com/cadorn/narwhalrunner/raw/master/catalog.json",
"name": "devtools",
"revision": "latest"
}
],
application.end = function(){
sys.puts('application end');
application.saveState(true);
};
application.saveState = function(shutdown){
sys.puts('application saveState');
//dump data.json into application state
var mode = 0444;
var x = posix.open(application.dataPath ,process.O_RDWR|process.O_TRUNC,0444).addCallback(function(fd){
(function (exports) {
// because the anonymous function is being called without a scope being set,
// "this" will refer to the global scope. In a browser, that's the window, but
// will be "undefined" in strict mode. In other JS platforms, it may be some
// other thing.
// my code here.
// don't make accidental globals.
// hang a small number of things on the "exports" object.
// EXAMPLE 1
// PSGI equivalent
var app = function(env) {
return function(respond) {
// do some event stuff
setTimeout(function() {
respond({ status : code, headers : headers, body : body });
}, 1000);
}
var Q = require("promise"); // http://gist.github.com/312929
function xhrcall() {
var deferred = Q.Deferred();
return promise(function(P){
var xhr = new XMLHttpRequest();
xhr.open("GET","/");
xhr.onreadystatechange = function(){
if (xhr.readyState == 4) {
if (xhr.responseText.match(/success/))
var HTTP = require("narwhal/q-http-client");
var Q = require("narwhal/promise-util");
// recovery
var httpReadRetryLoop = function (url, timeout, times) {
return Q.when(HTTP.read(url), function (content) {
return content;
}, function (error) {
if (times == 0)
return Q.reject(error);
require.def({
'increment': {
'requires': ['math'],
'factory': function() {
var exports = arguments[0].exports,
require = arguments[0].require,
module = arguments[0].module;
var add = require('math').add;
exports.increment = function(val) {
return add(val, 1);
#/usr/bin/env sh
check_and_exit () {
if [ ! "$?" = "0" ]; then
echo "$1"
exit 1
fi
}
# Node.JS
var Q = require("q");
var Queue = require("q/queue").Queue;
function throttle(max, wrapped) {
var queue = Queue();
var pending = 0;
function loop() {
return Q.when(queue.get(), function (next) {
defs = {};
modules = {};
function define(name, fn) {
defs[name] = fn;
}
function require(name) {
console.log("Loading " + name);
if (modules.hasOwnProperty(name)) return modules[name];
if (defs.hasOwnProperty(name)) {
var fn = defs[name];