如何在GWT中“处理队列中的待处理事件”?

时间:2014-04-21 12:22:57

标签: javascript gwt

我试图自己解决this question,因为到目前为止还没有回答。目标是创建一个Timer“任务”,并且同步等待它完成。在“慢”(可控延迟)URL上使用XHR请求,我已经超越忙等待进入阻塞等待。但是,虽然我不再浪费CPU,但仍未处理待处理的事件。

我认为我真正需要的是做一个循环,告诉GWT / JavaScript处理事件队列中的所有“挂起事件”,直到我的Timer事件被执行。似乎有一种方法可以使用以下内容在 Firefox 中执行此操作:

// Since FF3, we'll have to ask for user permission to execute XPCOM objects.
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
// Get the current thread.
var thread = Components.classes["@mozilla.org/thread-manager;1"]
.getService(Components.interfaces.nsIThreadManager).currentThread;
// Create an inner property to be used later as a notifier.
this.delayed = true;
// Execute "this.delayed = false" after it finishes.
setTimeout("this.delayed = false;", millis);
// Keep looping until "this.delayed = false"
while (this.delayed) {
    // This code will not freeze your browser
    thread.processNextEvent(true);
}

但这不适用于GWTTestCase(htmlunit);所以,我的问题是:

在GWT中有类似thread.processNextEvent()吗?

在回答之前请考虑到这一点:

  1. 我正在创建单元测试
  2. 这永远不会在客户端浏览器或服务器中执行。
  3. 因此,“最佳做法是使用异步代码”建议不相关
  4. 即使“使用异步代码”是相关的,它根本没有帮助,因为 JUnit样式的测试不支持异步代码测试(至少不在单线程环境中! )。

0 个答案:

没有答案