-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add setEncoding() to replace 'data_as_buffer'. expose the public prop…
…erties. add 'connect' method that takes a callback. fix zk_promise. fix tests
- Loading branch information
Dave Dopson
committed
Dec 13, 2011
1 parent
1165e36
commit a947a95
Showing
9 changed files
with
223 additions
and
101 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
sys = require("sys"); | ||
util = require("util"); | ||
var fs = require('./fs-promise'); | ||
|
||
// open a file and read it | ||
fs.open("fs-promise.js", process.O_RDONLY).then(function(fd){ | ||
return fs.read(fd, 4096); | ||
}).then(function(args){ | ||
sys.puts(args[0]); | ||
util.puts(args[0]); | ||
}); | ||
|
||
// does the same thing | ||
fs.readFile("fs-promise.js").addCallback(sys.puts); | ||
fs.readFile("fs-promise.js").addCallback(util.puts); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,61 @@ | ||
var assert = require ('assert'); | ||
var sys = require ('sys'); | ||
var promise = require("./promise"); | ||
var ZK = require ("./zookeeper").ZooKeeper; | ||
exports.ZK = ZK; | ||
var ZooKeeper = require ("./zookeeper"); | ||
var util = require('util'); | ||
|
||
ZK.prototype.context = {}; | ||
exports = module.exports = ZooKeeperPromise; | ||
exports.ZK = ZooKeeperPromise; // backwards compatibility | ||
|
||
ZK.prototype.on_connected = function () { | ||
var deferred = promise.defer(); | ||
this.on (ZK.on_connected, function (zkk) { | ||
deferred.resolve (zkk); | ||
}); | ||
return deferred.promise; | ||
function ZooKeeperPromise() { | ||
var self = this; | ||
|
||
ZooKeeper.apply(this); | ||
|
||
return self; | ||
} | ||
util.inherits(ZooKeeperPromise, ZooKeeper); | ||
|
||
ZooKeeperPromise.prototype.on_connected = function on_connected() { | ||
var self = this; | ||
var deferred = promise.defer(); | ||
self.on ('connect', function () { | ||
deferred.resolve (self); | ||
}); | ||
return deferred.promise; | ||
}; | ||
|
||
|
||
convertZKAsyncFunction = function(asyncFunction){ | ||
return function(){ | ||
var deferred = promise.defer(); | ||
arguments.length ++; | ||
arguments[arguments.length-1]= | ||
function(rc, error, result){ | ||
if(rc) { | ||
deferred.emitError(rc); | ||
} else { | ||
if(arguments.length > 3){ | ||
// if there are multiple success values, we return an array | ||
Array.prototype.shift.call(arguments, 1); | ||
Array.prototype.shift.call(arguments, 1); | ||
deferred.emitSuccess(arguments); | ||
} | ||
else{ | ||
deferred.emitSuccess(result); | ||
} | ||
} | ||
}; | ||
asyncFunction.apply (this, arguments); | ||
return deferred.promise; | ||
|
||
function convertAsync(fn){ | ||
return function() { | ||
var self = this; | ||
var deferred = promise.defer(); | ||
arguments.length ++; | ||
arguments[arguments.length-1] = function(rc, error, result){ | ||
if(rc) { | ||
deferred.emitError(rc); | ||
} else { | ||
if(arguments.length > 3){ | ||
// if there are multiple success values, we return an array | ||
Array.prototype.shift.call(arguments, 1); | ||
Array.prototype.shift.call(arguments, 1); | ||
deferred.emitSuccess(arguments); | ||
} else { | ||
deferred.emitSuccess(result); | ||
} | ||
} | ||
}; | ||
fn.apply (self, arguments); | ||
return deferred.promise; | ||
}; | ||
}; | ||
|
||
for (var f in ZK.prototype) { | ||
var m = f.match(/^a(w?)_(.*)$/); | ||
if (m) { | ||
var new_func = m[1]? m[1] + "_" + m[2] : m[2]; | ||
ZK.prototype[new_func] = convertZKAsyncFunction (ZK.prototype[f]); | ||
//console.log ("function %s is %j", f, sys.inspect(ZK.prototype[new_func],true,3)); | ||
} | ||
for (var f in ZooKeeperPromise.prototype) { | ||
var m = f.match(/^a(w?)_(.*)$/); | ||
if (m) { | ||
var new_func = m[1]? m[1] + "_" + m[2] : m[2]; | ||
ZooKeeperPromise.prototype[new_func] = convertAsync (ZooKeeperPromise.prototype[f]); | ||
//console.log ("function %s is %j", f, util.inspect(ZK.prototype[new_func],true,3)); | ||
} | ||
} | ||
|
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#!/usr/bin/env node | ||
|
||
var util = require('util'); | ||
var _ = require('underscore'); | ||
|
||
function MyFunctionalShape() { | ||
if(! this instanceof MyFunctionalShape) { | ||
return new MyFunctionalShape(); | ||
} | ||
var self = this; | ||
console.log('parent ctor: this=' + this); | ||
} | ||
|
||
MyFunctionalShape.prototype.baseMethod = function() { } | ||
|
||
function MyFunctionalCircle() { | ||
if(!_.isObject(this) || !(this instanceof MyFunctionalCircle)) { | ||
return new MyFunctionalCircle(arguments); | ||
} | ||
var self = this; | ||
MyFunctionalShape.apply(self); | ||
|
||
self.fn = function() { } | ||
console.log('child ctor: this=' + this); | ||
} | ||
|
||
MyFunctionalCircle.super_ = MyFunctionalShape; | ||
MyFunctionalCircle.prototype = Object.create(MyFunctionalShape.prototype, { | ||
constructor: { | ||
value: MyFunctionalCircle, | ||
enumerable: false, | ||
writable: true, | ||
configurable: true | ||
} | ||
}); | ||
|
||
MyFunctionalCircle.prototype.method = function() { | ||
console.log("a: " + util.inspect(this, true, 99)); | ||
|
||
}; | ||
|
||
MyFunctionalShape.prototype.baseMethod2 = function() {}; | ||
|
||
function lookAt(obj, name) { | ||
console.log("Looking at " + name + ":"); | ||
for(key in obj) { | ||
console.log(name + "['" + key + "'] = " + obj[key]); | ||
} | ||
} | ||
|
||
|
||
var a = new MyFunctionalCircle(); | ||
lookAt(a, 'a'); | ||
console.log("a: " + util.inspect(a, true, 99)); | ||
a.method(); |
Oops, something went wrong.