【问题标题】:How do I simulate a slow Meteor publication?如何模拟慢速 Meteor 出版物?
【发布时间】:2014-11-09 00:23:23
【问题描述】:

我正在尝试模拟一个出版物做大量工作并花费很长时间返回一个光标。

我的发布方法强制休眠(使用未来),但应用始终只显示

加载中...

这是出版物:

Meteor.publish('people', function() {
  Future = Npm.require('fibers/future');
  var future = new Future();

  //simulate long pause
  setTimeout(function() {
    // UPDATE: coding error here. This line needs to be
    //   future.return(People.find());
    // See the accepted answer for an alternative, too:
    //   Meteor._sleepForMs(2000);
    return People.find();
  }, 2000);

  //wait for future.return
  return future.wait();
});

还有路由器:

Router.configure({
  layoutTemplate: 'layout',
  loadingTemplate: 'loading'
});

Router.map(function() {
  return this.route('home', {
    path: '/',
    waitOn: function() {
      return [Meteor.subscribe('people')];
    },
    data: function() {
      return {
        'people': People.find()
      };
    }
  });
});

Router.onBeforeAction('loading');

完整源代码:https://gitlab.com/meonkeys/meteor-simulate-slow-publication

【问题讨论】:

    标签: javascript meteor


    【解决方案1】:

    最简单的方法是使用未记录的Meteor._sleepForMs 函数,如下所示:

    Meteor.publish('people', function() {
      Meteor._sleepForMs(2000);
      return People.find();
    });
    

    【讨论】:

    • 方便,谢谢!我也更新了我的问题以显示我犯的编码错误。
    • 提示:不要试图在客户端查看Meteor._sleepForMs,这是一个仅限服务器的方法。
    • 问题:这对我有用,谢谢。但只有一次。我猜在第一次加载后,该出版物会缓存在客户端上。有没有办法在每次刷新时模拟较慢的连接? (或者至少再测试一次?)
    • 是的,这是预期的行为。订阅文档后,您将访问它们的客户端缓存。为了再次看到延迟,您需要停止订阅并重新开始(或刷新页面等)。
    • 是的。刷新页面将终止客户端上的所有活动订阅。我建议添加一个 console.log 来查看发布者是否被调用。如果您仍然无法弄清楚,请使用相关代码发布一个新问题。
    猜你喜欢
    • 1970-01-01
    • 2011-01-02
    • 2018-02-13
    • 2014-08-28
    • 2017-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多