Meteor, 错误: 没有光纤就无法等待在服务器上使用 Meteor.setTimeout()



我对Meteor相当陌生,所以我可能在这里错过了一个关键的见解。

无论如何,我想向用户指出同时有多少其他用户在网站上。我已经有一个AuditItems集合,它存储了whowhenwhat的kv对,我可以将其用于这种类型的计算。我的Collections查询是使用 new Date() 参数运行的,因此我不能只观察结果,我必须定期重新运行查询。

然后,我通过在Deps.Dependency上调用changed使其被动。

这是代码:

  // 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)

所有Deps方法只能在客户端上使用。你得到的错误是关于Deps的构建方式(因为客户端不使用光纤)。

如果要在服务器上具有反应性,则需要使用 observeobserveChanges 。使用添加和删除的句柄,您可以查询该点的日期(当文档更改时)。

您还可以使用Meteor.setInterval定期从那里删除旧用户。

你可以做的一件事是使用像流星-prescence这样的软件包,它为你做这一切。它的作用是保存一个实时集合,其中包含有关在线每个人的信息,然后当他们离线/超时后,它会使用定期Meteor.setInterval方法将它们从集合中删除。

最新更新