gpt4 book ai didi

javascript - 了解 Meteor 订阅

转载 作者:行者123 更新时间:2023-11-29 21:13:16 25 4
gpt4 key购买 nike

我不明白 react-meteor-data 中的这个例子

import { createContainer } from 'meteor/react-meteor-data';

export default FooContainer = createContainer(() => {
// Do all your reactive data access in this method.
// Note that this subscription will get cleaned up when your component is unmounted
var handle = Meteor.subscribe("todoList", this.props.id);

return {
currentUser: Meteor.user(),
listLoading: ! handle.ready(),
tasks: Tasks.find({listId: this.props.id}).fetch(),
};
}, Foo);

为什么建议在卸载 Component停止 订阅,但在这种情况下,没有任何努力停止任何东西?那么,Meteor 如何处理订阅呢?什么时候清理收藏品?每次执行跟踪器回调时订阅是否都在堆积?

或者 Meteor 是否足够聪明,知道什么时候 Meteor.subscribe 被调用并且对订阅施展魔法?

最佳答案

ReactMeteorData 容器在响应式(Reactive) Tracker 中运行 createContainer 的回调计算。

它的一个功能是在计算无效或停止时停止订阅。

如果函数重新运行产生相同的订阅,(相同的发布,相同的参数)库足够聪明,不会取消并重新创建相同的订阅。

当组件被卸载时,计算停止,订阅被取消并且不会重新创建(因为回调不会再次调用)因此自动取消订阅。

If you call Meteor.subscribe within a reactive computation, for example using Tracker.autorun, the subscription will automatically be cancelled when the computation is invalidated or stopped; it is not necessary to call stop on subscriptions made from inside autorun. However, if the next iteration of your run function subscribes to the same record set (same name and parameters), Meteor is smart enough to skip a wasteful unsubscribe/resubscribe.

(来源:Meteor Docs)

关于javascript - 了解 Meteor 订阅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40681529/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com