我在下面的代码中使用了派生商店。这感觉像是一个奇怪的构造,因为我只将派生构造用于动态 $session 依赖项并获取 normData。但不是 $norm。我只使用一次 $norm 来启动派生商店。
尽管如此,它似乎工作正常。但是如果 $session 发生变化,我必须更新订阅。是否可以在不先取消订阅的情况下更新 RxFire / RxJs 订阅?
let normDocRef = null;
let normData = null;
let normSubscription = null;
const norm = derived(
session,
$session => {
normDocRef = db.doc(`uploads/${$session.a_id}_${$session.year}`);
// renew the subscription if $session changes
if (normSubscription)
normSubscription.unsubscribe();
normSubscription = doc(normDocRef).subscribe(snapshot => {
if (snapshot.exists) {
normData = snapshot.data();
} else {
normData = null;
};
});
},
);
$norm; // kick off the derived store to monitor $session
// show the data and updates
$: console.log(normData);
onDestroy(() => {
if (normSubscription) normSubscription.unsubscribe();
});
更新:我可以使用派生商店的设置和返回选项来更改真正的 $norm Svelte 商店中的 $norm。下面的代码在我自己的答案中。
但真正的问题是:我可以更新订阅吗?更改订阅而不取消订阅?