forked from mondora/asteroid
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored subscriptions. Updated tests.
- Loading branch information
Paolo Scanferla
committed
May 13, 2014
1 parent
cded01e
commit f8808c4
Showing
17 changed files
with
1,159 additions
and
905 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
//////////////////////// | ||
// Subscription class // | ||
//////////////////////// | ||
|
||
var Subscription = function (name, params, asteroid) { | ||
this._name = name; | ||
this._params = params; | ||
this._asteroid = asteroid; | ||
// Subscription promises | ||
this._ready = Q.defer(); | ||
this.ready = this._ready.promise; | ||
// Subscribe via DDP | ||
var or = this._onReady.bind(this); | ||
var os = this._onStop.bind(this); | ||
var oe = this._onError.bind(this); | ||
this.id = asteroid.ddp.sub(name, params, or, os, oe); | ||
}; | ||
Subscription.constructor = Subscription; | ||
|
||
Subscription.prototype.stop = function () { | ||
this._asteroid.ddp.unsub(this.id); | ||
}; | ||
|
||
Subscription.prototype._onReady = function () { | ||
this._ready.resolve(); | ||
}; | ||
|
||
Subscription.prototype._onStop = function () { | ||
delete this._asteroid.subscriptions[this.id]; | ||
}; | ||
|
||
Subscription.prototype._onError = function (err) { | ||
if (this.ready.isPending()) { | ||
this._ready.reject(err); | ||
} | ||
delete this._asteroid.subscriptions[this.id]; | ||
}; | ||
|
||
|
||
|
||
////////////////////// | ||
// Subscribe method // | ||
////////////////////// | ||
|
||
Asteroid.prototype.subscribe = function (name /* , param1, param2, ... */) { | ||
// Assert arguments type | ||
must.beString(name); | ||
// Collect arguments into array | ||
var params = Array.prototype.slice.call(arguments, 1); | ||
var sub = new Subscription(name, params, this); | ||
this.subscriptions[sub.id] = sub; | ||
return sub; | ||
}; | ||
|
||
Asteroid.prototype._reEstablishSubscriptions = function () { | ||
var subs = this.subscriptions; | ||
for (var id in subs) { | ||
subs[id] = new Subscription(subs[id]._name, subs[id]._params, this); | ||
} | ||
}; |
Oops, something went wrong.