Skip to content

Commit

Permalink
Changed subscription logic. Updated docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Paolo Scanferla committed May 9, 2014
1 parent ed700bc commit 53a1c56
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 13 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,9 @@ successful. Otherwise it'll be rejected with the error.
------------------------------------------------------------
###Asteroid.subscribe(name, [param1, param2, ...])

Subscribes to the specified subscription.
Subscribes to the specified subscription. If a subscription
by that name is already present (and successful), first
Asteroid unsubscribes from it.

#####Arguments

Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "asteroid",
"version": "0.2.2",
"version": "0.2.3",
"homepage": "https://github.com/mondora/asteroid",
"authors": [
"Paolo Scanferla <[email protected]>"
Expand Down
10 changes: 6 additions & 4 deletions dist/asteroid.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,11 @@ Asteroid.prototype._onChanged = function (data) {
Asteroid.prototype.subscribe = function (name /* , param1, param2, ... */) {
// Assert name must be a string
must.beString(name);
// If we're already subscribed, return the subscription
if (this.subscriptions[name]) {
return this.subscriptions[name];
// If we're already subscribed, unsubscribe before re-subscribing
var subPromise = this.subscriptions[name];
if (subPromise && subPromise.isFulfilled()) {
var subId = subPromise.inspect().value;
this.unsubscribe(subId);
}
// Init the promise that will be returned
var deferred = Q.defer();
Expand All @@ -258,7 +260,7 @@ Asteroid.prototype.subscribe = function (name /* , param1, param2, ... */) {
// This is the onReady/onNoSub callback
if (err) {
// Reject the promise if the server answered nosub
deferred.reject(err, id);
deferred.reject(err);
} else {
// Resolve the promise if the server answered ready
deferred.resolve(id);
Expand Down
2 changes: 1 addition & 1 deletion dist/asteroid.min.js

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion docs/asteroid.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ successful. Otherwise it'll be rejected with the error.
------------------------------------------------------------
###Asteroid.subscribe(name, [param1, param2, ...])

Subscribes to the specified subscription.
Subscribes to the specified subscription. If a subscription
by that name is already present (and successful), first
Asteroid unsubscribes from it.

#####Arguments

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "asteroid",
"version": "0.2.2",
"version": "0.2.3",
"description": "Aletrnative Meteor client",
"main": "dist/asteroid.js",
"scripts": {
Expand Down
10 changes: 6 additions & 4 deletions src/asteroid.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,11 @@ Asteroid.prototype._onChanged = function (data) {
Asteroid.prototype.subscribe = function (name /* , param1, param2, ... */) {
// Assert name must be a string
must.beString(name);
// If we're already subscribed, return the subscription
if (this.subscriptions[name]) {
return this.subscriptions[name];
// If we're already subscribed, unsubscribe before re-subscribing
var subPromise = this.subscriptions[name];
if (subPromise && subPromise.isFulfilled()) {
var subId = subPromise.inspect().value;
this.unsubscribe(subId);
}
// Init the promise that will be returned
var deferred = Q.defer();
Expand All @@ -150,7 +152,7 @@ Asteroid.prototype.subscribe = function (name /* , param1, param2, ... */) {
// This is the onReady/onNoSub callback
if (err) {
// Reject the promise if the server answered nosub
deferred.reject(err, id);
deferred.reject(err);
} else {
// Resolve the promise if the server answered ready
deferred.resolve(id);
Expand Down

0 comments on commit 53a1c56

Please sign in to comment.