使用Svelte/RxJs/RxFire订阅文档.如何更新订阅 [英] Subscribe to a doc using Svelte / RxJs / RxFire. How can I update the subscription

查看:178
本文介绍了使用Svelte/RxJs/RxFire订阅文档.如何更新订阅的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面的代码中使用派生存储.感觉像是一个奇怪的构造,因为我仅将派生构造用于动态$ session依赖关系并获取normData.但是不是$ norm.我只用一次$ norm来启动派生商店.

I use a derived store in the code below. It feels like a strange construct because I only use the derived construct for the dynamic $session dependency and to get the normData. But not with $norm. I use $norm only once to kick off the derived store.

尽管如此,它似乎工作正常.但是,如果$ session更改,我必须续订.是否可以在不先取消订阅的情况下更新RxFire/RxJs订阅?

Nevertheless it seem to work fine. But I have to renew the subscription if the $session changes. Is it possible to update the RxFire / RxJs subscription without unsubscribing first?

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.下面是我自己的答案中的代码.

Update: I can use the set and return options of the derived store to change $norm in a real $norm Svelte store. Code below in my own answer.

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

But the real question is: Can I update a subscription. Change the subscription without the unsubscribe?

推荐答案

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

I already had the answer, but did not realize it.

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

Below the derived store code with the set() and return() options.
When the session changes the return() will unsubscribe automatically.
So still an unsubscribe and not an update ... but this feels good. Nice!

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.closed) {
    normSubscription.unsubscribe();
  }
});

API文档派生的存储区:

从一个或多个其他商店派生商店.只要这些依赖项发生变化(像$ session ),回调就会运行.

Derives a store from one or more other stores. Whenever those dependencies change (like the $session), the callback runs.

如果您从回调中返回一个函数",则当a)回调再次运行时(因为依赖关系已更改),它将在回调之前被调用(之前)或b)...

If you "return a function" from the callback, it will be called (before the callback) when a) the callback runs again (because the dependency changed), or b) ...

这篇关于使用Svelte/RxJs/RxFire订阅文档.如何更新订阅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆