Skip to content
Browse files

switch to wrap/unwrap model

  • Loading branch information...
1 parent 4368988 commit b04a26e42f95b05c4e5b0283cc199407e84ac227 @substack committed Jun 20, 2012
Showing with 14 additions and 13 deletions.
  1. +12 −11 index.js
  2. +2 −2 test/wrap.js
View
23 index.js
@@ -4,8 +4,8 @@ var objectKeys = require('./lib/keys');
var forEach = require('./lib/foreach');
var isEnumerable = require('./lib/is_enum');
-module.exports = function (cons, wrap) {
- return new Proto(cons, wrap);
+module.exports = function (cons, opts) {
+ return new Proto(cons, opts);
};
(function () { // browsers bleh
@@ -14,13 +14,15 @@ module.exports = function (cons, wrap) {
}
})();
-function Proto (cons, wrap) {
+function Proto (cons, opts) {
var self = this;
EventEmitter.call(self);
+ if (!opts) opts = {};
self.remote = {};
self.callbacks = { local : [], remote : [] };
- self.wrap = wrap;
+ self.wrap = opts.wrap;
+ self.unwrap = opts.unwrap;
self.scrubber = scrubber(self.callbacks.local);
@@ -62,14 +64,13 @@ Proto.prototype.handle = function (req) {
var cb = function () {
self.request(id, [].slice.apply(arguments));
};
- if (self.wrap) {
- var ref = self.wrap(cb, id);
- self.callbacks.remote[id] = ref;
- return cb;
- }
- else self.callbacks.remote[id] = cb;
+ self.callbacks.remote[id] = self.wrap ? self.wrap(cb, id) : cb;
+ return cb;
}
- return self.callbacks.remote[id];
+ return self.unwrap
+ ? self.unwrap(self.callbacks.remote[id])
+ : self.callbacks.remote[id]
+ ;
});
if (req.method === 'methods') {
View
4 test/wrap.js
@@ -35,9 +35,9 @@ test('proto hashes', function (t) {
setTimeout(g.bind({}, [ 'q', 'r' ]), 50);
},
y : 555
- }, swrapper);
+ }, { wrap : swrapper });
- var c = proto({}, cwrapper);
+ var c = proto({}, { wrap : cwrapper });
var sreqs = [];
s.on('request', function (req) {

0 comments on commit b04a26e

Please sign in to comment.
Something went wrong with that request. Please try again.