Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No expire, no cache limit. #21

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ But, you should have the capability to control the cache. Subscriptions Manager

~~~js
var subs = new SubsManager({
// maximum number of cache subscriptions
// maximum number of cache subscriptions, use 0 for unlimited
cacheLimit: 10,
// any subscription will be expire after 5 minute, if it's not subscribed again
// any subscription will be expire after 5 minute, if it's not subscribed again, use 0 to never expire
expireIn: 5
});
~~~
Expand Down Expand Up @@ -187,4 +187,4 @@ Tracker.autorun(function() {
// all the subscriptions are ready to use.
}
});
~~~
~~~
18 changes: 11 additions & 7 deletions lib/sub_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ SubsManager = function (options) {
var self = this;
self.options = options || {};
// maxiumum number of subscriptions are cached
self.options.cacheLimit = self.options.cacheLimit || 10;
// maximum time, subscription stay in the cache
self.options.expireIn = self.options.expireIn || 5;
self.options.cacheLimit || (self.options.cacheLimit === 0) || (self.options.cacheLimit = 10);
// maximum time, subscription stay in the cache, use 0 for no expiration
self.options.expireIn || (self.options.expireIn === 0) || (self.options.expireIn = 5);

self._cacheMap = {};
self._cacheList = [];
Expand Down Expand Up @@ -69,7 +69,7 @@ SubsManager.prototype._addSub = function(args) {

// to notify the global ready()
self._notifyChanged();

// no need to interfere with the current computation
self._reRunSubs();
}
Expand Down Expand Up @@ -136,8 +136,12 @@ SubsManager.prototype._applyExpirations = function() {
SubsManager.prototype._registerComputation = function() {
var self = this;
var computation = Deps.autorun(function() {
self._applyExpirations();
self._applyCacheLimit();
if(self.options.expireIn !== 0){
self._applyExpirations();
}
if(self.options.cacheLimit !== 0){
self._applyCacheLimit();
}

var ready = true;
_.each(self._cacheList, function(sub) {
Expand Down Expand Up @@ -226,4 +230,4 @@ SubsManager.prototype.ready = function() {
return false;
}
return this._ready;
};
};
14 changes: 14 additions & 0 deletions tests/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,17 @@ Tinytest.addAsync('options expireIn - not expired', function(test, done) {
done();
});
});

Tinytest.add('options expireIn - no expiration: expireIn set to 0', function(test) {
// expireIn 2 minutes
var sm = new SubsManager({expireIn: 0});

test.equal(sm.options.expireIn, 0);
});

Tinytest.add('options expireIn - no limit: cacheLimit set to 0', function(test) {
// expireIn 2 minutes
var sm = new SubsManager({cacheLimit: 0});

test.equal(sm.options.cacheLimit, 0);
});