问题描述
我试图弄清楚 光标 .observe 在流星内部的运行方式,但没有发现任何相关信息.文档说
I was trying to figure it out how cursor.observe runs inside meteor, but found nothing about it.Docs says
我想更好地了解实时查询的含义.
I would like to understand better what live query means.
- 我的观察者函数将在哪里执行?流星还是蒙哥?
当我们不仅有一个订阅 observer 的用户时,每个客户端都会运行一个实例,从而导致性能和竞态条件问题.
When we have more than just a user subscribing an observer, one instance runs for each client, leading us to a performance and race condition issue.
- 如何像
singleton
那样实现我的observe
?只能运行一个实例.
- How can I implement my
observe
to it be like asingleton
? Just one instance running for all.
There was a third question here, but now it is a separated question: How to avoid race conditions on cursor.observe?
推荐答案
服务器端,截至目前,observe
的工作方式如下:
Server side, as of right now, observe
works as follows:
- 构造与查询匹配的文档集.
- 定期使用查询轮询数据库,并进行一些更改,将相关事件发送给回调.
- 当匹配数据被流星本身更改/插入到mongo中时,发出相关事件,使上面的步骤2短路.
有计划(可能在下一版本中)自动确保共享具有相同参数的订阅调用.因此,基本上,自动为您照顾了单身人士.
There are plans (possibly in the next release) to automatically ensure that calls to subscribe that have the same arguments are shared. So basically taking care of the singleton part for you automatically.
当然,您自己也可以实现类似的目标,但是我认为这对于流星团队来说是当务之急,因此在这一点上可能不值得付出努力.
Certainly you could achieve something like this yourself, but I believe it's a high priority for the meteor team, so it's probably not worth the effort at this point.
这篇关于cursor.observe如何工作以及如何避免多个实例运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!