Skip to content
Browse files

fixed duplicate subscription init requests on the same object

  • Loading branch information...
1 parent 0414959 commit 3ef115d534a1c9108d1acc3fc44784a19abc49c0 @fritzy fritzy committed Jul 11, 2012
Showing with 17 additions and 4 deletions.
  1. +17 −4 thoonk.js
View
21 thoonk.js
@@ -108,10 +108,11 @@ Thoonk.prototype.constructor = Thoonk;
}).call(Thoonk.prototype);
-var Subscription = function(thoonkobject, instance, event_handler) {
+var Subscription = function(thoonkobject, instance, event_handler, callback) {
EventEmitter.call(this);
this.thoonk = thoonkobject.thoonk;
this.instance = instance;
+ this.callback = callback;
this.objtype = thoonkobject.objtype;
this.thoonkobject = thoonkobject;
this.sub = this.objtype + '::' + this.instance;
@@ -144,6 +145,9 @@ Subscription.prototype.constructor = Subscription;
if(!this.thoonk.subscriptions.hasOwnProperty(this.sub)) {
this.thoonk.once('subscribed.' + this._build_event(this.subscribables[this.subscribables.length - 1]), function() {
this.emit('subscribe_ready');
+ if(this.callback) {
+ process.nextTick(this.callback);
+ }
}.bind(this));
this.thoonk.subscriptions[this.sub] = this.subscribables;
for(var subscribable in this.subscribables) {
@@ -153,6 +157,9 @@ Subscription.prototype.constructor = Subscription;
}
} else {
this.emit('subscribe_ready');
+ if(this.callback) {
+ process.nextTick(this.callback);
+ }
}
if(!this.subinitted) {
for(var subscribable in this.subscribables) {
@@ -196,13 +203,19 @@ ThoonkBaseObject.constructor = ThoonkBaseObject;
};
this.init_subscribe = function(callback) {
- this.subscription = new Subscription(this, this.name, this.handle_event.bind(this));
- this.subscription.once("subscribe_ready", function() {
+ if(!this.subscription) {
+ this.subscription = new Subscription(this, this.name, this.handle_event.bind(this), function() {
+ this.emit("subscribe_ready");
+ if(callback) {
+ callback(false, this.subscription);
+ }
+ }.bind(this));
+ } else {
this.emit("subscribe_ready");
if(callback) {
callback(false, this.subscription);
}
- }.bind(this));
+ }
};
this.runscript = function(scriptname, args, callback) {

0 comments on commit 3ef115d

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