通过 Push-Diffusion 检测主题的移除
Detect removal of topics via Push-Diffusion
我订阅了一个主题树,并使用它来更新 HTML table 中的值。有时会添加新的子主题并删除旧的子主题。我需要保持我的 UI 同步,当未知主题的值到达时检测到新主题,但我无法发现订阅的主题何时被删除。
如何检测主题何时被删除?
如果您使用 topic selector 订阅主题树,例如 ?myTopicTreeRoot//
,那么您在拨打此订阅电话时,还可以说明取消订阅时会发生什么情况,以及订阅,例如:
session.subscribe("?myTopicTreeRoot//").on({
open: function(subscription) {
console.log('Opened subscription for: ' + subscription.selector);
},
update : function(update, topic) {
console.log('Update for ' + topic + ' : ' + update);
},
subscribe : function(details, topic) {
//This will notify you of every new subscription to a topic in your subtree
console.log('Subscribed to : ' + topic);
},
unsubscribe : function(reason, topic) {
//This will notify you of every unsubscription from a topic in your subtree
console.log('Unsubscribed from : ' + topic);
}
});
属性 函数 subscribe
和 unsubscribe
被调用用于主题 myTopicTreeRoot
和任何后代,例如myTopicTreeRoot/foo/bar/baz
。
重要:当主题被移除时,任何订阅都会结束,这又会调用unsubscribe
。
注意最好用subscribe属性函数。如果订阅无状态主题,则使用对 update
的第一次调用来暗示新主题的存在是不明智的。
我订阅了一个主题树,并使用它来更新 HTML table 中的值。有时会添加新的子主题并删除旧的子主题。我需要保持我的 UI 同步,当未知主题的值到达时检测到新主题,但我无法发现订阅的主题何时被删除。
如何检测主题何时被删除?
如果您使用 topic selector 订阅主题树,例如 ?myTopicTreeRoot//
,那么您在拨打此订阅电话时,还可以说明取消订阅时会发生什么情况,以及订阅,例如:
session.subscribe("?myTopicTreeRoot//").on({
open: function(subscription) {
console.log('Opened subscription for: ' + subscription.selector);
},
update : function(update, topic) {
console.log('Update for ' + topic + ' : ' + update);
},
subscribe : function(details, topic) {
//This will notify you of every new subscription to a topic in your subtree
console.log('Subscribed to : ' + topic);
},
unsubscribe : function(reason, topic) {
//This will notify you of every unsubscription from a topic in your subtree
console.log('Unsubscribed from : ' + topic);
}
});
属性 函数 subscribe
和 unsubscribe
被调用用于主题 myTopicTreeRoot
和任何后代,例如myTopicTreeRoot/foo/bar/baz
。
重要:当主题被移除时,任何订阅都会结束,这又会调用unsubscribe
。
注意最好用subscribe属性函数。如果订阅无状态主题,则使用对 update
的第一次调用来暗示新主题的存在是不明智的。