Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

assign to a .prototype in Proto

  • Loading branch information...
commit 00e304e082f2ac5f0aff7c6cc2ec9311335543e2 1 parent b916665
@substack authored
Showing with 94 additions and 80 deletions.
  1. +92 −79 index.js
  2. +2 −1  package.json
View
171 index.js
@@ -4,20 +4,30 @@ var Store = require('./lib/store');
var objectKeys = require('./lib/keys');
var forEach = require('./lib/foreach');
-module.exports = function (wrapper) {
- var self = new EventEmitter;
-
+module.exports = function (cons) {
+ return new Proto(cons);
+};
+
+/*
+(function () { // browsers bleh
+ for (var key in EventEmitter.prototype) {
+ Proto.prototype[key] = EventEmitter.prototype[key];
+ }
+})();
+*/
+
+Proto.prototype = new EventEmitter;
+
+function Proto (cons) {
+ var self = this;
+ EventEmitter.call(self);
self.remote = {};
- var instance = self.instance =
- typeof(wrapper) == 'function'
- ? new wrapper(self.remote, self)
- : wrapper || {}
- ;
-
self.localStore = new Store;
self.remoteStore = new Store;
+ self.scrubber = new Scrubber(self.localStore);
+
self.localStore.on('cull', function (id) {
self.emit('request', {
method : 'cull',
@@ -26,84 +36,87 @@ module.exports = function (wrapper) {
});
});
- var scrubber = new Scrubber(self.localStore);
-
- self.start = function () {
- self.request('methods', [ instance ]);
- };
+ if (typeof cons === 'function') {
+ self.instance = new cons(self.remote, self);
+ }
+ else self.instance = cons || {};
+}
+
+Proto.prototype.start = function () {
+ this.request('methods', [ this.instance ]);
+};
+
+Proto.prototype.request = function (method, args) {
+ var scrub = this.scrubber.scrub(args);
- self.request = function (method, args) {
- var scrub = scrubber.scrub(args);
-
- self.emit('request', {
- method : method,
- arguments : scrub.arguments,
- callbacks : scrub.callbacks,
- links : scrub.links
- });
- };
+ this.emit('request', {
+ method : method,
+ arguments : scrub.arguments,
+ callbacks : scrub.callbacks,
+ links : scrub.links
+ });
+};
+
+Proto.prototype.handle = function (req) {
+ var self = this;
+ var args = self.scrubber.unscrub(req, function (id) {
+ if (!self.remoteStore.has(id)) {
+ // create a new function only if one hasn't already been created
+ // for a particular id
+ self.remoteStore.add(function () {
+ self.request(id, [].slice.apply(arguments));
+ }, id);
+ }
+ return self.remoteStore.get(id);
+ });
- self.handle = function (req) {
- var args = scrubber.unscrub(req, function (id) {
- if (!self.remoteStore.has(id)) {
- // create a new function only if one hasn't already been created
- // for a particular id
- self.remoteStore.add(function () {
- self.request(id, [].slice.apply(arguments));
- }, id);
- }
- return self.remoteStore.get(id);
+ if (req.method === 'methods') {
+ self.handleMethods(args[0]);
+ }
+ else if (req.method === 'cull') {
+ forEach(args, function (id) {
+ self.remoteStore.cull(args);
});
-
- if (req.method === 'methods') {
- handleMethods(args[0]);
- }
- else if (req.method === 'cull') {
- forEach(args, function (id) {
- self.remoteStore.cull(args);
- });
- }
- else if (typeof req.method === 'string') {
- if (self.instance.propertyIsEnumerable(req.method)) {
- apply(self.instance[req.method], self.instance, args);
- }
- else {
- self.emit('fail', new Error(
- 'Request for non-enumerable method: ' + req.method
- ));
- }
+ }
+ else if (typeof req.method === 'string') {
+ if (self.instance.propertyIsEnumerable(req.method)) {
+ self.apply(self.instance[req.method], self.instance, args);
}
- else if (typeof req.method == 'number') {
- var fn = self.localStore.get(req.method);
- if (!fn) {
- self.emit('fail', new Error('no such method'));
- }
- else apply(fn, self.instance, args);
+ else {
+ self.emit('fail', new Error(
+ 'Request for non-enumerable method: ' + req.method
+ ));
}
}
-
- function handleMethods (methods) {
- if (typeof methods != 'object') {
- methods = {};
+ else if (typeof req.method == 'number') {
+ var fn = self.localStore.get(req.method);
+ if (!fn) {
+ self.emit('fail', new Error('no such method'));
}
-
- // copy since assignment discards the previous refs
- forEach(objectKeys(self.remote), function (key) {
- delete self.remote[key];
- });
-
- forEach(objectKeys(methods), function (key) {
- self.remote[key] = methods[key];
- });
-
- self.emit('remote', self.remote);
- self.emit('ready');
+ else self.apply(fn, self.instance, args);
}
-
- function apply(f, obj, args) {
- try { f.apply(obj, args) }
- catch (err) { self.emit('error', err) }
+};
+
+Proto.prototype.handleMethods = function (methods) {
+ var self = this;
+ if (typeof methods != 'object') {
+ methods = {};
}
- return self;
+ // copy since assignment discards the previous refs
+ forEach(objectKeys(self.remote), function (key) {
+ delete self.remote[key];
+ });
+
+ forEach(objectKeys(methods), function (key) {
+ self.remote[key] = methods[key];
+ });
+
+ self.emit('remote', self.remote);
+ self.emit('ready');
+};
+
+Proto.prototype.apply = function (f, obj, args) {
+ try { f.apply(obj, args) }
+ catch (err) { this.emit('error', err) }
};
View
3  package.json
@@ -14,7 +14,8 @@
"jsonify" : "~0.0.0"
},
"devDependencies" : {
- "tap" : "~0.2.5"
+ "tap" : "~0.2.5",
+ "weak" : "~0.2.1"
},
"scripts": {
"test": "tap test/*.js"
Please sign in to comment.
Something went wrong with that request. Please try again.