在长时间运行的方法执行时向客户端发送消息

时间:2016-03-10 18:44:41

标签: javascript meteor

说我有这样的流星方法(就像一个快速,可行的例子)

import {EventEmitter} from 'events';
Meteor.methods({
  'wait': async function () {
    let i = 0;
    let timer;
    let interval;
    const emitter = new EventEmitter();

    return await new Promise((resolve, reject) => {
      timer = Meteor.setTimeout(resolve, 60 * 1000);

      // whenever this interval runs, inform the client running this.
      interval = Meteor.setInterval(() => {
        i++;
        i%2 === 0 ? emitter.emit('tick') : emitter.emit('tock');
      }, 500);
    }).finally(() => {
      Meteor.clearInterval(interval);
      Meteor.clearTimeout(timeout);
    });
  }
});

所以在我的客户端上,我希望在方法执行时订阅此 。例如,像:

const asyncOperation = new Meteor.callEmitter('wait', function () {
  console.log('Oh look, your operation is complete.')
}).on('tick', () => {
  console.log('received tick'));
}).on('tock', () => {
  console.log('received tock')
});

这可以在Meteor中做到吗?

0 个答案:

没有答案