【问题标题】:How to control Meteor Session update and database subscription latency?如何控制 Meteor Session 更新和数据库订阅延迟?
【发布时间】:2013-03-21 09:15:24
【问题描述】:

我试图通过 Meteor.autorun 更改 Collection (GraphData) 订阅来限制客户端缓存。但我注意到服务器端仅在更改 Session(浏览器 GUI)后才发布数据。对吗?

在客户端,我有以下咖啡脚本代码:

Meteor.startup ->  
  Meteor.autorun () ->
    Meteor.subscribe 'graphdata', Session.get 'graph_name'

在一个函数中,draw_graph,我有

Session.set 'graph_sub', false    
Session.set 'graph_name', item_name
ready = Session.get 'graph_sub'
while !(ready)    
  Meteor.setTimeout (ready = Session.get 'graph_sub'), 1000
Do something with the GraphData subscription

在服务器端我有

Meteor.startup ->  
  Meteor.publish 'graphdata', (name) -> 
    if name?
      GraphData.find({name: name})
      Session.set 'graph_sub', true

我希望在 Session.set 'graph_name', item_name 之后触发服务器端发布,但我注意到我陷入了 while 循环。

我的理解正确吗?无论如何要强制 Session 变量更改在没有 Session 更改的情况下在服务器端引起注意?

【问题讨论】:

  • 循环 ready = false; setTimeout((-> ready = true), 1000) until ready 不会像你想要的那样工作,它会导致内部 (-> ready = true) 被称为无数次。 (编辑:将其写成单行以适应这种单行注释格式;编译为 JS 以更清楚地查看问题。)
  • 它显然不会在您提供的单行中工作,因为 ready 永远不会改变。在我的代码中,一旦服务器端将graph_sub设置为true,我就准备好更改为true。我最初的问题是关于服务器端未能将 graph_sub 设置为 true。
  • 是的,这可能有点过时,但我确实认为ready 最终应该在一秒钟后改变(如果达到那么远,然后再改变十亿次),直到那时@ 987654327@ 已通过 while 循环被调用了可怕的次数。

标签: meteor


【解决方案1】:
while !(Session.get 'graph_sub')    
  Meteor.setTimeout (Session.get 'graph_sub'), 1000

第二行不应该是Session.set吗?否则会话值永远不会改变,while 循环永远不会结束。

除了错字之外,我很困惑你为什么首先使用setTimeoutgraph_sub。这还不够吗?

if Meteor.isClient
  Meteor.startup ->
    Meteor.autorun ->
      Meteor.subscribe 'graphdata', Session.get 'graph_name'

if Meteor.isServer
  Meteor.startup ->
    Meteor.publish 'graphdata', (name) ->
      GraphData.find name: name

【讨论】:

  • 是的,对不起。我使用 graph_sub 作为检查订阅成功的一种方式,因为 Collection 可能非常大并且需要一些时间来订阅。我最初拥有的正是您所拥有的,但没有延迟,我的流程在订阅完成之前就结束了购买
【解决方案2】:

我认为你应该在Meteor.subscribe的回调中Do something with the GraphData subscription

Meteor.startup ->  
  Meteor.autorun () ->
    Meteor.subscribe 'graphdata', Session.get 'graph_name', () ->
      Do something with the GraphData subscription

另请注意,自 0.5.8 以来,会话在服务器端不再可用:https://github.com/meteor/meteor/blob/master/History.md#v058

【讨论】:

  • GraphData 集合中有三组数据:应用程序、设备和网关,它们作为graph_name 传入。 Do something with GraphData 订阅因每个不同的数据集而异。我无法将其移至 Meteor.subscribe 下。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-22
  • 2012-03-06
  • 1970-01-01
  • 1970-01-01
  • 2018-09-03
相关资源
最近更新 更多