Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I simulate a slow Meteor publication?

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

Loading...

Here's the publication:

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();
});

And the router:

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');

Full source code: https://gitlab.com/meonkeys/meteor-simulate-slow-publication

like image 391
Adam Monsen Avatar asked Nov 08 '14 15:11

Adam Monsen


1 Answers

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();
});
like image 86
David Weldon Avatar answered Oct 27 '22 08:10

David Weldon