Skip to content

Commit

Permalink
Fixed bug in reEstablishSubscriptions method.
Browse files Browse the repository at this point in the history
  • Loading branch information
Paolo Scanferla committed Oct 4, 2014
1 parent e03c85d commit 671f7c3
Show file tree
Hide file tree
Showing 11 changed files with 328 additions and 98 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"gulpfile.js"
],
"dependencies": {
"ddp.js": "~0.5.0",
"ddp.js": "~0.6.0",
"q": "1.0.1"
}
}
38 changes: 29 additions & 9 deletions dist/asteroid.browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1046,10 +1046,10 @@ Asteroid.Set = Set;
// Subscription class //
////////////////////////

var Subscription = function (name, params, hash, asteroid) {
var Subscription = function (name, params, fingerprint, asteroid) {
this._name = name;
this._params = params;
this._hash = hash;
this._fingerprint = fingerprint;
this._asteroid = asteroid;
// Subscription promises
this._ready = Q.defer();
Expand All @@ -1064,7 +1064,7 @@ Subscription.constructor = Subscription;

Subscription.prototype.stop = function () {
this._asteroid.ddp.unsub(this.id);
delete this._asteroid._subscriptionsCache[this._hash];
delete this._asteroid._subscriptionsCache[this._fingerprint];
};

Subscription.prototype._onReady = function () {
Expand All @@ -1073,13 +1073,15 @@ Subscription.prototype._onReady = function () {

Subscription.prototype._onStop = function () {
delete this._asteroid.subscriptions[this.id];
delete this._asteroid._subscriptionsCache[this._fingerprint];
};

Subscription.prototype._onError = function (err) {
if (this.ready.isPending()) {
this._ready.reject(err);
}
delete this._asteroid.subscriptions[this.id];
delete this._asteroid._subscriptionsCache[this._fingerprint];
};


Expand All @@ -1094,23 +1096,41 @@ Asteroid.prototype.subscribe = function (name /* , param1, param2, ... */) {
// Collect arguments into array
var args = Array.prototype.slice.call(arguments);
// Hash the arguments to get a key for _subscriptionsCache
var hash = JSON.stringify(args);
var fingerprint = JSON.stringify(args);
// Only subscribe if there is no cached subscription
if (!this._subscriptionsCache[hash]) {
if (!this._subscriptionsCache[fingerprint]) {
// Get the parameters of the subscription
var params = args.slice(1);
var sub = new Subscription(name, params, hash, this);
this._subscriptionsCache[hash] = sub;
// Subscribe
var sub = new Subscription(
name,
params,
fingerprint,
this
);
this._subscriptionsCache[sub._fingerprint] = sub;
this.subscriptions[sub.id] = sub;
}
return this._subscriptionsCache[hash];
return this._subscriptionsCache[fingerprint];
};

Asteroid.prototype._reEstablishSubscriptions = function () {
var subs = this.subscriptions;
var oldSub;
var newSub;
for (var id in subs) {
if (subs.hasOwnProperty(id)) {
subs[id] = new Subscription(subs[id]._name, subs[id]._params, this);
oldSub = subs[id];
newSub = new Subscription(
oldSub._name,
oldSub._params,
oldSub._fingerprint,
this
);
delete this.subscriptions[oldSub.id];
delete this._subscriptionsCache[oldSub._fingerprint];
this.subscriptions[newSub.id] = newSub;
this._subscriptionsCache[newSub._fingerprint] = newSub;
}
}
};
Expand Down
38 changes: 29 additions & 9 deletions dist/asteroid.chrome.js
Original file line number Diff line number Diff line change
Expand Up @@ -1046,10 +1046,10 @@ Asteroid.Set = Set;
// Subscription class //
////////////////////////

var Subscription = function (name, params, hash, asteroid) {
var Subscription = function (name, params, fingerprint, asteroid) {
this._name = name;
this._params = params;
this._hash = hash;
this._fingerprint = fingerprint;
this._asteroid = asteroid;
// Subscription promises
this._ready = Q.defer();
Expand All @@ -1064,7 +1064,7 @@ Subscription.constructor = Subscription;

Subscription.prototype.stop = function () {
this._asteroid.ddp.unsub(this.id);
delete this._asteroid._subscriptionsCache[this._hash];
delete this._asteroid._subscriptionsCache[this._fingerprint];
};

Subscription.prototype._onReady = function () {
Expand All @@ -1073,13 +1073,15 @@ Subscription.prototype._onReady = function () {

Subscription.prototype._onStop = function () {
delete this._asteroid.subscriptions[this.id];
delete this._asteroid._subscriptionsCache[this._fingerprint];
};

Subscription.prototype._onError = function (err) {
if (this.ready.isPending()) {
this._ready.reject(err);
}
delete this._asteroid.subscriptions[this.id];
delete this._asteroid._subscriptionsCache[this._fingerprint];
};


Expand All @@ -1094,23 +1096,41 @@ Asteroid.prototype.subscribe = function (name /* , param1, param2, ... */) {
// Collect arguments into array
var args = Array.prototype.slice.call(arguments);
// Hash the arguments to get a key for _subscriptionsCache
var hash = JSON.stringify(args);
var fingerprint = JSON.stringify(args);
// Only subscribe if there is no cached subscription
if (!this._subscriptionsCache[hash]) {
if (!this._subscriptionsCache[fingerprint]) {
// Get the parameters of the subscription
var params = args.slice(1);
var sub = new Subscription(name, params, hash, this);
this._subscriptionsCache[hash] = sub;
// Subscribe
var sub = new Subscription(
name,
params,
fingerprint,
this
);
this._subscriptionsCache[sub._fingerprint] = sub;
this.subscriptions[sub.id] = sub;
}
return this._subscriptionsCache[hash];
return this._subscriptionsCache[fingerprint];
};

Asteroid.prototype._reEstablishSubscriptions = function () {
var subs = this.subscriptions;
var oldSub;
var newSub;
for (var id in subs) {
if (subs.hasOwnProperty(id)) {
subs[id] = new Subscription(subs[id]._name, subs[id]._params, this);
oldSub = subs[id];
newSub = new Subscription(
oldSub._name,
oldSub._params,
oldSub._fingerprint,
this
);
delete this.subscriptions[oldSub.id];
delete this._subscriptionsCache[oldSub._fingerprint];
this.subscriptions[newSub.id] = newSub;
this._subscriptionsCache[newSub._fingerprint] = newSub;
}
}
};
Expand Down
38 changes: 29 additions & 9 deletions dist/asteroid.cordova.js
Original file line number Diff line number Diff line change
Expand Up @@ -1046,10 +1046,10 @@ Asteroid.Set = Set;
// Subscription class //
////////////////////////

var Subscription = function (name, params, hash, asteroid) {
var Subscription = function (name, params, fingerprint, asteroid) {
this._name = name;
this._params = params;
this._hash = hash;
this._fingerprint = fingerprint;
this._asteroid = asteroid;
// Subscription promises
this._ready = Q.defer();
Expand All @@ -1064,7 +1064,7 @@ Subscription.constructor = Subscription;

Subscription.prototype.stop = function () {
this._asteroid.ddp.unsub(this.id);
delete this._asteroid._subscriptionsCache[this._hash];
delete this._asteroid._subscriptionsCache[this._fingerprint];
};

Subscription.prototype._onReady = function () {
Expand All @@ -1073,13 +1073,15 @@ Subscription.prototype._onReady = function () {

Subscription.prototype._onStop = function () {
delete this._asteroid.subscriptions[this.id];
delete this._asteroid._subscriptionsCache[this._fingerprint];
};

Subscription.prototype._onError = function (err) {
if (this.ready.isPending()) {
this._ready.reject(err);
}
delete this._asteroid.subscriptions[this.id];
delete this._asteroid._subscriptionsCache[this._fingerprint];
};


Expand All @@ -1094,23 +1096,41 @@ Asteroid.prototype.subscribe = function (name /* , param1, param2, ... */) {
// Collect arguments into array
var args = Array.prototype.slice.call(arguments);
// Hash the arguments to get a key for _subscriptionsCache
var hash = JSON.stringify(args);
var fingerprint = JSON.stringify(args);
// Only subscribe if there is no cached subscription
if (!this._subscriptionsCache[hash]) {
if (!this._subscriptionsCache[fingerprint]) {
// Get the parameters of the subscription
var params = args.slice(1);
var sub = new Subscription(name, params, hash, this);
this._subscriptionsCache[hash] = sub;
// Subscribe
var sub = new Subscription(
name,
params,
fingerprint,
this
);
this._subscriptionsCache[sub._fingerprint] = sub;
this.subscriptions[sub.id] = sub;
}
return this._subscriptionsCache[hash];
return this._subscriptionsCache[fingerprint];
};

Asteroid.prototype._reEstablishSubscriptions = function () {
var subs = this.subscriptions;
var oldSub;
var newSub;
for (var id in subs) {
if (subs.hasOwnProperty(id)) {
subs[id] = new Subscription(subs[id]._name, subs[id]._params, this);
oldSub = subs[id];
newSub = new Subscription(
oldSub._name,
oldSub._params,
oldSub._fingerprint,
this
);
delete this.subscriptions[oldSub.id];
delete this._subscriptionsCache[oldSub._fingerprint];
this.subscriptions[newSub.id] = newSub;
this._subscriptionsCache[newSub._fingerprint] = newSub;
}
}
};
Expand Down
38 changes: 29 additions & 9 deletions dist/asteroid.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -1040,10 +1040,10 @@ Asteroid.Set = Set;
// Subscription class //
////////////////////////

var Subscription = function (name, params, hash, asteroid) {
var Subscription = function (name, params, fingerprint, asteroid) {
this._name = name;
this._params = params;
this._hash = hash;
this._fingerprint = fingerprint;
this._asteroid = asteroid;
// Subscription promises
this._ready = Q.defer();
Expand All @@ -1058,7 +1058,7 @@ Subscription.constructor = Subscription;

Subscription.prototype.stop = function () {
this._asteroid.ddp.unsub(this.id);
delete this._asteroid._subscriptionsCache[this._hash];
delete this._asteroid._subscriptionsCache[this._fingerprint];
};

Subscription.prototype._onReady = function () {
Expand All @@ -1067,13 +1067,15 @@ Subscription.prototype._onReady = function () {

Subscription.prototype._onStop = function () {
delete this._asteroid.subscriptions[this.id];
delete this._asteroid._subscriptionsCache[this._fingerprint];
};

Subscription.prototype._onError = function (err) {
if (this.ready.isPending()) {
this._ready.reject(err);
}
delete this._asteroid.subscriptions[this.id];
delete this._asteroid._subscriptionsCache[this._fingerprint];
};


Expand All @@ -1088,23 +1090,41 @@ Asteroid.prototype.subscribe = function (name /* , param1, param2, ... */) {
// Collect arguments into array
var args = Array.prototype.slice.call(arguments);
// Hash the arguments to get a key for _subscriptionsCache
var hash = JSON.stringify(args);
var fingerprint = JSON.stringify(args);
// Only subscribe if there is no cached subscription
if (!this._subscriptionsCache[hash]) {
if (!this._subscriptionsCache[fingerprint]) {
// Get the parameters of the subscription
var params = args.slice(1);
var sub = new Subscription(name, params, hash, this);
this._subscriptionsCache[hash] = sub;
// Subscribe
var sub = new Subscription(
name,
params,
fingerprint,
this
);
this._subscriptionsCache[sub._fingerprint] = sub;
this.subscriptions[sub.id] = sub;
}
return this._subscriptionsCache[hash];
return this._subscriptionsCache[fingerprint];
};

Asteroid.prototype._reEstablishSubscriptions = function () {
var subs = this.subscriptions;
var oldSub;
var newSub;
for (var id in subs) {
if (subs.hasOwnProperty(id)) {
subs[id] = new Subscription(subs[id]._name, subs[id]._params, this);
oldSub = subs[id];
newSub = new Subscription(
oldSub._name,
oldSub._params,
oldSub._fingerprint,
this
);
delete this.subscriptions[oldSub.id];
delete this._subscriptionsCache[oldSub._fingerprint];
this.subscriptions[newSub.id] = newSub;
this._subscriptionsCache[newSub._fingerprint] = newSub;
}
}
};
Expand Down
Loading

0 comments on commit 671f7c3

Please sign in to comment.