cvara / rxdb-hooks

React hooks for integrating with RxDB
MIT License
136 stars 6 forks source link

Question: useRXCollection not able to notice deletion and recreation of collection in one sitting #58

Open de-robat opened 1 year ago

de-robat commented 1 year ago

Hello, i am currenlty facing an issue where users of my app are logging in and out right after eachother. while logging out im removing some user specific collections, which leads to the replicationstate of collections beeing reset in the rxdb itself. so far so good.

but now it happen that users login right after again, and the old references to useRXCollections are now destroyed:true, as one can expect.

Im recreating the collections properly before the user can login again, but this is not picked up by rxdb-hooks anymore as if the collection has been found immidiatley there is no subscription to the "newCollection$" subject:

useEffect(() => {
        if (!db) {
            return;
        }
        const found = db[name];
        if (found) {
            setCollection(found); <------ here we wont subscribe
        } else {
            const sub = db.newCollections$.subscribe(col => {
                if (col[name]) {
                    // We don't unsubscribe so that we get notified
                    // and update collection if it gets deleted/recreated
                    setCollection(col[name]);
                }
            });
            return () => {
                sub.unsubscribe();
            };
        }
    }, [db, name]);

i am now wondering why this is the behaviour rxdb-hooks opt for, and if i get side effects im not aware of if i subscribe to the subject anyhow. Or ist this just a usecase that has not been tought of and therfore is not covered?

russelldavis commented 1 year ago

Looks like this was fixed here: https://github.com/cvara/rxdb-hooks/commit/36c6bcf711236de296c9d3c7a73653454c8a3ed2#diff-8056012e2d5a20d01339760c71c5d9363406b3de875d94246041ae2b0e52fbaa