我在下面的代码中使用派生存储。感觉像是一个奇怪的构造,因为我仅将派生构造用于动态$ 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();
});
更新:我可以使用派生商店的set和return选项在真实的$ norm Svelte商店中更改$ norm。下面是我自己的答案中的代码。
但是真正的问题是:我可以更新订阅。要更改订阅但不取消订阅吗?