使流星出版物对时间具有反应性(Make a meteor publication reactive to time)

编程入门 行业动态 更新时间:2024-10-26 22:26:32
使流星出版物对时间具有反应性(Make a meteor publication reactive to time)

我在确定如何使订阅对某个发布查询有反应方面遇到了一些麻烦。

我的出版物如下:

Meteor.publish('allData', function(){ return Data.find({TTL: {$gt: new Date()}}) })

如您所见,文档包含TTL字段。 只要生存时间仍然大于当前时间,它就可以发送给客户端,如果没有,则不应该。

订阅:

this.autorun(() => { this.subscribe('allData') })

在初始加载时,所有数据都很好,但是每当TTL到期时,除非我重新加载页面,否则文档将保留在客户端上。 无论如何反过来处理这个,使过期的文件从客户端消失?

I'm having a little trouble figuring out how to make a subscription reactive to a certain publication query.

My publication is as follows:

Meteor.publish('allData', function(){ return Data.find({TTL: {$gt: new Date()}}) })

As you can see, the documents contain a TTL field. As long as the Time To Live is still greater that the current time, it can be sent to the client, if not, it shouldn't.

the subscription:

this.autorun(() => { this.subscribe('allData') })

On the initial load, all data is fine, but whenever the TTL expires, the document remains on the client unless I reload the page. Is there anyway to handle this reactively, making the expired documents disappear from the client?

最满意答案

ReactiveVar和自动运行的组合为我做了诀窍。 这可能是矫枉过正,但它确实有效。

let cutoffTimestamp = new ReactiveVar(); Meteor.setInterval(function() { cutoffTimestamp.set(Date.now() - 1); }, 60000); Meteor.publish("myPub", function() { this.autorun(function() { return myCollection.find({ timestamp: { $gte: cutoffTimestamp.get(); } }); }); });

A combination of ReactiveVar and an autorun did the trick for me. It's possible that this is overkill, but it works.

let cutoffTimestamp = new ReactiveVar(); Meteor.setInterval(function() { cutoffTimestamp.set(Date.now() - 1); }, 60000); Meteor.publish("myPub", function() { this.autorun(function() { return myCollection.find({ timestamp: { $gte: cutoffTimestamp.get(); } }); }); });

更多推荐

本文发布于:2023-07-24 10:43:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1244925.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:出版物   流星   时间   meteor   reactive

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!