简体   繁体   English

将Meteor.setTimeout返回值存储在Collection中

[英]Store Meteor.setTimeout return value in Collection

I want to store the handle giving by Meteor.setTimeout() into my own Timers Collection. 我想将Meteor.setTimeout()提供的handle存储到我自己的Timers集合中。 The problem is i get a Maximum call stack size exceeded error doing this... 问题是我在执行此操作时出现Maximum call stack size exceeded错误...

Client side I get an int ID but server side I get a object like this that my update function doesn't want to store... 客户端我得到一个int ID但是服务器端我得到了这样的对象,我的update函数不想存储...

{ _idleTimeout: 10000,
  _idlePrev: 
  { _idleNext: [Circular],
    _idlePrev: [Circular],
    msecs: 10000,
    ontimeout: [Function: listOnTimeout] },
 _idleNext: 
  { _idleNext: [Circular],
    _idlePrev: [Circular],
    msecs: 10000,
    ontimeout: [Function: listOnTimeout] },
 _idleStart: 1421848999217,
 _monotonicStartTime: 4341630336,
 _onTimeout: [Function],
 _repeat: false }

Look at my code (only server side), here is timer creation: 查看我的代码(仅在服务器端),这是计时器的创建:

triStartTimer: function (timerId, timerDuration)
{   
    var meteorTimer = Meteor.setTimeout(function()  {timerCallbackTimeout(timerId);}, timerDuration);
    console.log(meteorTimer);
    var start = new Date().getTime();

    var data = {
        duration : timerDuration,
        timerID : meteorTimer,
        start : start,
        state : timerStateEnum.RUNNING
    };

    var ret = updateTimerInfos(timerId, data);
    return ret;
},

The update function that get the Maximum call stack size exceeded : 获得Maximum call stack size exceeded的更新函数Maximum call stack size exceeded

var updateTimerInfos = function (id, data)
{
  var ret = Timers.update(id,
  {
    $set :
    {
        duration : data.duration,
        timerID : data.timerID,   // Without this, the function works fine
        start : data.start,
        state : data.state
    }
  }, function (error, result)
  {
    if (error)
    {
        console.log("updateTimer FAILED !!!");
        console.log("reason : " + error.message);
    }
  } );
  return ret;
}

Anyway, the Timeout work fine. 无论如何,超时工作正常。 The callback is called with the right value. 使用正确的值调用回调。 But I need the ID to disarmed the Timeout in certain conditions... 但是在某些情况下,我需要ID来解除超时限制。

If you have an idea or the solution, feel free =) 如果您有想法或解决方案,请放心=)

This is basically a shortcoming of setTimeout , which should only ever be used on a sessional basis for exactly this reason. 基本上,这是setTimeout的缺点,出于这个原因,只能在会话中使用它。 You can't store the handle in your DB as it's not an EJSON -compatible value, and I'm unaware of any way to pick out an individual EJSON-compatible sub-object which can then be used to retrieve (and stop) the timeout in question (although I stand to be corrected). 您无法将句柄存储在数据库中,因为它不是EJSON兼容的值,而且我不知道有任何方法可以挑选出一个单独的EJSON兼容子对象,然后该子对象可用于检索(并停止)有问题的超时(尽管我可以纠正)。

The solution is to use a package for scheduling if your requirement is anything other than very simple. 解决方案是,如果您的要求不是非常简单,则使用软件包进行计划。 Have a look at percolate:synced-cron , which I used for this tutorial . 看一下本教程使用的percolate:synced-cron

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM