我对 Meteor 还很陌生,所以我可能在这里错过了一个关键的见解。
无论如何,我想向用户表明同时网站上有多少其他用户。我已经有一个AuditItems
存储 kv 对的集合,who
我可以将其用于这种类型的计算。我的查询是使用参数运行的,所以我不能只观察结果,我必须定期重新运行查询。when
what
Collections
new Date()
然后我通过调用changed
一个Deps.Dependency
.
这是代码:
// publishing counts of the users that are logged on
// at the moment
Meteor.publish("user-activity", function () {
var self = this;
self.added("user-activity", "all-recent-activity", {'operations': getRecentActivityCountsCache});
self.ready();
Deps.autorun(function() {
self.changed("user-activity", "all-recent-activity", {'operations': getRecentActivityCounts()});
});
});
var getRecentActiveCountsDependency = new Deps.Dependency;
var getRecentActivityCountsCache = 0;
var getRecentActivityCounts = function() {
// register dependency with the caller
getRecentActiveCountsDependency.depend();
var now = new Date();
var aWhileAgo = new Date(now.valueOf() - (5 * 60 * 1000)); // 5 minutes
auditItems = AuditItems.find({'when': { '$gt': aWhileAgo }}).fetch();
console.log('raw data: ' + JSON.stringify(auditItems));
getRecentActivityCountsCache = _.chain(auditItems)
.groupBy('who')
.keys()
.size()
.value();
console.log('new count: ' + getRecentActivityCountsCache);
return getRecentActivityCountsCache;
};
Meteor.setTimeout(function() {
getRecentActiveCountsDependency.changed();
}, 60 * 1000); // 60 seconds
计时器第一次触发时,我在控制台上收到此错误:
Exception from Deps recompute: Error: Can't wait without a fiber
at Function.wait (/home/vagrant/.meteor/tools/3cba50c44a/lib/node_modules/fibers/future.js:83:9)
at Object.Future.wait (/home/vagrant/.meteor/tools/3cba50c44a/lib/node_modules/fibers/future.js:325:10)
at _.extend._nextObject (packages/mongo-livedata/mongo_driver.js:540)
at _.extend.forEach (packages/mongo-livedata/mongo_driver.js:570)
at _.extend.map (packages/mongo-livedata/mongo_driver.js:582)
at _.extend.fetch (packages/mongo-livedata/mongo_driver.js:606)
at _.each.Cursor.(anonymous function) [as fetch] (packages/mongo-livedata/mongo_driver.js:444)
at getRecentActivityCounts (app/server/server.js:26:70)
at null._func (app/server/server.js:12:79)
at _.extend._compute (packages/deps/deps.js:129)