Skip to content

Instantly share code, notes, and snippets.

//pseudo-code (the actual implementation uses Prototype-like Classical OO syntax, which I will
//probably rip out for a port to node, seeing as how most noders seem to not be too crazy about that style)
//(my framework's name is jojo)
var fsm = new jojo.fsm({
states: {
//by convention, the fsm will be automatically put into the "initial" state on construction
//(defining the transition events for this state here is optional)
initial: {
//within each state, the "stateStartup" event will always be fired (again, defining the handler is optional)
if(require.main == module){
if (system.args[2] === "-refresh") {
system.print("deleting " + exports.baseFilePath);
require("child_process").exec("rm -r " + exports.baseFilePath, function(err, stdout, stderr) {
if (err !== null) {
system.print("error deleting directory: " + err);
} else {
exports.useLocal().runAsMain(system.args[3]);
}
});
var sys = require('sys');
//use protoype methods for massive speed improvement (and less memory consumption)
function greetBehaviors(message) {
this.message = message;
}
greetBehaviors.prototype.greet = function() {
sys.puts(this.message);
};
greetBehaviors.prototype.slowGreet = function() {
require("../common");
var events = require('events');
var t = 0;
var e = new events.EventEmitter();
e.on("test", function(amount) {
if (isNaN(amount)) amount = 1;
t += amount;
});
[51/69] cxx: src/node.cc -> build/default/src/node_4.o
/usr/bin/g++ -DHAVE_OPENSSL=1 -DEV_MULTIPLICITY=0 -pthread -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -DHAVE_FDATASYNC=1 -DPLATFORM=linux -DNDEBUG -g -O3 -Idefault/src -I../src -Idefault/deps/libeio -I../deps/libeio -Idefault/deps/http_parser -I../deps/http_parser -Idefault/deps/v8/include -I../deps/v8/include -Idefault/deps/libev -I../deps/libev -Idefault/deps/c-ares -I../deps/c-ares -Idefault/deps/c-ares/linux-x86_64 -I../deps/c-ares/linux-x86_64 -Ideps/v8/include ../src/node.cc -c -o default/src/node_4.o
In file included from ../src/node.cc:39:
default/src/node_natives.h:53: error: expected initializer before â-â token
Waf: Leaving directory `/home/ryan/node/build'
Build failed: -> task failed (err #1):
{task: cxx node.cc -> node_4.o}
make: *** [all] Error 1
var Semaphore = require("semaphore"),
sys = require("sys");
var sem1 = new Semaphore(function(i) {
sys.puts("ultimate callback... iteration that executed last = " + i);
});
//create a bunch of async ops, and we only want the sys.puts() call to happen after all have finished
for (var i = 0, l = 100; i < l; i++) {
//before each async op, explicitly tell sem1 to increment internal counter
@ryedin
ryedin / onReady pattern
Created October 20, 2010 16:53
pseudo-code
var ready = false,
readyCallbacks = [];
function onReady(callback) {
if (ready) return callback();
readyCallbacks.push(callback);
}
function signalReady() {
ready = true;
var CharacterElement = function(locChar) {
this.x = null;
this.y = null;
this.width = null;
this.dy = null;
this.value = (locChar !== null) ? locChar : "";
this.se_newline = "";
this.characterAttribute = null;
this.bbox = null;
};
@ryedin
ryedin / bubble.js pseudocode
Created November 9, 2010 00:27
quick method to wire bubbled (relayed) events
//pseudocode!!
process.EventEmitter.prototype.bubble = function(target, eventName) {
this.on(eventName, function() {
arguments.unshift(eventName);
target.emit.call(target, arguments);
});
};
var emitter1 = new EventEmitter();
var emitter2 = new EventEmitter();
var _emit = emitter1.emit;
emitter1.emit = function() {
//piggyback
emitter2.emit.call(emitter2, arguments);
//original impl.
_emit.call(this, arguments);
};