Google Sketchup 插件的 Javascript 事件循环/消息泵
我正在开发一个使用 Ruby API 编写的 Google Sketchup 插件。此 API 中有一个 WebDialog 类,可用于呈现HTML 并在插件代码的 WebDialog 和 Ruby 端之间移动数据。我正在使用此类为我的插件构建 UI。
数据从 WebDialog 异步发送到 Ruby 端。由于文档不合格,我最初并没有意识到这一点,现在我已经开始使用我的插件了,它开始给我带来一些问题。具体来说:当从WebDialog向Ruby端进行多次连续调用时,仅执行最后一次调用。因此,我显然需要设计某种“桥梁”来防止从 WebDialog 到 Ruby 端的调用丢失——我认为,这基本上是一个“事件循环”或“消息泵”系统。
我的问题是我不知道如何做到这一点。我希望有人能为我提供某种资源,为这样的系统如何工作奠定一个框架——需要什么样的检查、执行的顺序等等。我知道这可能是一项非常复杂的任务,但我只需要一些基本的东西:基本上,一种在我向 Ruby 发送请求时使 Javascript 停止的方法,直到我得到我需要的数据为止,并处理可能出现的任何潜在错误。作物。
任何帮助将非常感激!
I'm working on a plugin for Google Sketchup that is written using the Ruby API. Within this API is a WebDialog class which one can use to render HTML and move data between the WebDialog and the Ruby side of the plugin code. I'm using this class to build a UI for my plugin.
Data is sent from the WebDialog to the Ruby side asynchronously. Due to subpar documentation I was not initially aware of this and now that I'm a ways into my plugin it's began to create some problems for me. Specifically: when multiple successive calls are made from the WebDialog to the Ruby side, only the last call is executed. So, I clearly need to devise some sort of "bridge" which will prevent calls from the WebDialog to the Ruby side from getting lost -- which is, I think, basically an "event loop" or "message pump" system.
My problem is that I haven't a good idea of how to do this. What I'm hoping is that someone can provide me with some sort of resource that lays out a framework for how such a system should work -- what sort of checks are needed, the sequence in which they're performed, etc. I know this can be a terrifically complex task, but I only need something basic: basically, a way of making Javascript stop when I send a request to Ruby, not proceeding until I get the data I need back, and dealing with any potential errors that may crop up.
Any help would be very much appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在 WebDialog 类上花费了大量时间。我计划写一个这样的泵,但我发现我可以用不同的方式来做,并获得更可靠的结果。
(我的 WebDialog 发现:http://forums.sketchucation.com/viewtopic。 php?f=180&t=23445 )
替代方法
SketchUp > JavaScript
我的替代方法是我没有尝试将数据从 WebDialog 推送到 Ruby。但相反,Ruby 泵送了 WebDialog,因为
Webdialog.execute_script
是同步的。我向 WebDialog 发送一条带有查询的命令。然后 Javascript 对其进行处理并将结果放入隐藏的 INPUT 元素中,然后我使用“WebDialog.get_element_value”来获取其中的内容。
我将所有这些都封装到一个包装方法中,该方法将处理返回值并将其转换为适当的 Ruby 对象。 http://www.thomthom.net /software/sketchup/tt_lib2/doc/TT/GUI/Window.html#call_script-instance_method
概要是:
所有这些都是同步的。
Javascript泵
Javascript> SketchUp
如果你确实需要从 JS 中获取信息,那么我认为你需要这样做:
这个概念是存储您的消息,然后将控制权移交给 SketchUp 端,SketchUp 端可以同步泵送消息。
(未经检验的理论。)
I've spent a great deal of time with the WebDialog class. I planned to write such a pump, but I found that I could do it differently with more reliable results.
( My WebDialog findings: http://forums.sketchucation.com/viewtopic.php?f=180&t=23445 )
Alternative Method
SketchUp > JavaScript
My alternative method was that I didn't try to push data from the WebDialog to Ruby. But instead had Ruby pump the WebDialog because
Webdialog.execute_script
is synchronous.I send a command to the WebDialog with a query. The Javascript then processes this and put the result into a hidden INPUT element which I then use ´WebDialog.get_element_value` to fetch the content of.
All of this I wrapped up into a wrapper method the will process the return value and convert it into appropriate Ruby objects. http://www.thomthom.net/software/sketchup/tt_lib2/doc/TT/GUI/Window.html#call_script-instance_method
The outline is:
All this is synchronous.
Javascript Pump
Javascript > SketchUp
If you really need to pump information from JS, then I think you need to do something like this:
The concept would be to store up your messages and then hand over control to the SketchUp side which can pump it synchronously.
(Untested theory.)