流星观察永远运行 [英] Meteor observe running forever
问题描述
根据观察文档,我必须定义并调用stop函数,否则我的观察者将永远运行.关键是,例如,如果用户决定在预期时间之前关闭浏览器,如何调用 stop ?如果连接已死,我如何调用 stop?
According to observe docs, I must define and call a stop function, or my observer will run forever. The point, is how to call stop if the user decided to close his browser before the expected time, for example? How can I call stop if a conection is dead?
推荐答案
您实际上无需担心断开连接.任何时候订阅停止(无论是客户端断开连接,还是调用 unsub,或者您调用 this.stop
),服务器都会在拆除之前运行所有注册的 onStop
处理程序主动订阅.(顺便说一下,断开连接时不会立即发生拆卸,但最终会发生.我们的想法是稍等片刻,以防客户端尝试重新连接并恢复其会话.)
You don't actually need to worry about disconnects. Any time the subscription stops (whether the client disconnects, or calls unsub, or you call this.stop
), the server will run all the registered onStop
handlers before tearing down the active subscription. (Teardown doesn't happen immediately on disconnect, by the way, but it will eventually. The idea is to wait a bit, in case the client tries to reconnect and resume its session.)
您做需要做的是确保注册一个 onStop
处理程序来清理您的代码使用的任何资源.同样,这需要在订阅停止时发生.onStop
是正确的钩子.例如,在活动观察上调用 stop
非常重要,就像示例中的第一个代码节一样.
What you do need to do is make sure to register an onStop
handler that cleans up any resources your code used. Again, that needs to happen anytime the subscription is stopped. onStop
is the right hook. It's critically important to call stop
on an active observe, for example, just like in the first code stanza in the example.
这篇关于流星观察永远运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!