本文介绍了如何模拟慢速 Meteor 发布?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试模拟一个出版物,它做了大量工作并需要很长时间才能返回游标.

I'm trying to simulate a publication doing a bunch of work and taking a long time to return a cursor.

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

My publish method has a forced sleep (using a future), but the app always displays only

这是出版物:

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

推荐答案

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

The easiest way to do this is to use the undocumented Meteor._sleepForMs function like so:

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

这篇关于如何模拟慢速 Meteor 发布?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

查看更多