简体   繁体   English

如何使用事件驱动的模型“继续尝试直到可行”?

[英]How to “keep trying until it works” with an event-driven model?

I'm writing code that looks like this: 我正在编写看起来像这样的代码:

function someFunction()
{
    doStuffWithCallback(function(success)
    {
        if(success)
        {
            ...
            // do stuff here
            ...
        }
        else
            someFunction();
    });
}

Basically, keep trying "doStuffWithCallback" until the callback receives the green light. 基本上,继续尝试“ doStuffWithCallback”,直到回调收到绿灯为止。 Generally, this would be done with a loop, but how can I do this with an event-driven model without causing infinite recursion in the event that success is never reached? 通常,这将通过循环来完成,但是在从未达到成功的情况下,如何使用事件驱动的模型执行此操作而又不会引起无限递归? ( success may be dependent on things like external servers, so if they're down, it will continually be false). success可能取决于外部服务器之类的东西,因此,如果它们出现故障,它将一直是错误的)。

The structure of the code cannot be changed much; 代码的结构不能太多更改。 I have to be using callbacks to accomplish what I want because of the API I'm using. 由于要使用的API,我必须使用回调来完成所需的操作。

Your event-driven model should not look like this: 您的事件驱动模型不应如下所示:

  • Component dispatches an event 组件调度事件
  • Application kernel receives event 应用程序内核接收事件
  • Application kernel distributes event to registered handlers 应用程序内核将事件分发给已注册的处理程序
  • A component registered as a handler may dispatch another event. 注册为处理程序的组件可以调度另一个事件。 In which case, repeat. 在这种情况下,请重复。

Your event-driven model should look like this: 您的事件驱动模型如下所示:

  • Component dispatches an event 组件调度事件
  • Application kernel receives event 应用程序内核接收事件
  • Application kernel queues event 应用程序内核排队事件
  • Next "tick", application kernel distributes all queued events to registered handles 接下来的“滴答声”,应用程序内核将所有排队的事件分发到已注册的句柄
  • A component registered as a handler may dispatch another event. 注册为处理程序的组件可以调度另一个事件。 In which case, repeat. 在这种情况下,请重复。

In this way, your event dispatch mechanism is asynchronous, and you won't have any recursive calls at all. 这样,您的事件分配机制是异步的,并且根本不会进行任何递归调用。

Then, because you still don't want events being dispatched forever, you might introduce some limit internal to the component that defines how many times the event will be re-dispatched. 然后,由于您仍不希望事件永远被调度,因此您可能会在组件内部引入一些限制, 定义事件将被重新调度的次数。 A simple integer variable will do. 一个简单的整数变量就可以。

At the very least, implement a back-off mechanism that might wait for progressively longer intervals each time the callback fails, before queuing a repeat attempt. 至少要实现一种回退机制,该机制可能在每次回调失败之前等待逐渐更长的时间间隔,然后才对重复尝试进行排队。

You could introduce a counter, and call it with someFunction(1000) to try 1000 times. 您可以引入一个计数器,并使用someFunction(1000)进行调用以尝试1000次。

function someFunction(countDown)
{
    doStuffWithCallback(function(success)
    {
        if(success)
        {
            ...
            // do stuff here
            ...
        }
        else if(countDown > 0)
            someFunction(countDown - 1);
    });
}

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

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