使用Svelte / RxJs / RxFire订阅文档。如何更新订阅

我在下面的代码中使用派生存储。感觉像是一个奇怪的构造,因为我仅将派生构造用于动态$ 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。下面是我自己的答案中的代码。

但是真正的问题是:我可以更新订阅。要更改订阅但不取消订阅吗?

iskingwang 回答:使用Svelte / RxJs / RxFire订阅文档。如何更新订阅

好吧,大致了解一下您想要在此处描述的内容。

当变量/存储区更改时,您实际上可以使用reactive declaration执行代码。

在这种情况下是执行resubscribe方法:

let normDocRef = null;
let normData = null;
let normSubscription = null;

$: {
  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;
      };
    });
  }
}

onDestroy(() => {
  if (normSubscription) normSubscription.unsubscribe();
}); 

这里的关键是,在编译此代码时,Svelte知道该代码块取决于$session,因此只要$session更改,它将重新执行代码块。

如果您想将其重构为另一个函数,则需要确保Svelte知道该函数取决于$session,即:

$: resubscribe_norm($session);

在这里,斯维尔特(Svelte)可以告诉您,如果$session发生了变化,则需要再次致电resubscribe_norm

,

我已经有了答案,但是没有意识到。

在派生的商店代码下面使用set()和return()选项。
会话更改时,return()将自动退订。
所以仍然是退订,而不是更新...但是感觉很好。很好!

let normDocRef = null;
let normSubscription = null

const norm = derived(
  session,($session,set) => {
    normDocRef = db.doc(`uploads/${$session.a_id}_${$session.year}`);
    normSubscription = doc(normDocRef).subscribe(snapshot => {
      if (snapshot.exists) {
        set(snapshot.data());
      } else {
        set({}); // clear
      };
    });
    return () => {  
      normSubscription.unsubscribe();
    };
  },{}  // initial value
);

$: console.log('$norm',$norm);  // Now it is a real store

onDestroy(() => {
  if (normSubscription) {
    normSubscription.unsubscribe();
  }
});
本文链接:https://www.f2er.com/3120551.html

大家都在问