itsmeurbi / squid-use-observable-issue

0 stars 0 forks source link

Purpose

This repository is meant to be used only as a playground to replicate and test whay I believe is an unexpected behaviour using the useObservable hook from @squidcloud/react lib

Assumptions

This repository assums you have a Squid project with a proper MongoDB integration with at least one collection

Setup

Issue description

System use case

We have a react application that uses Squid as its backend since one year ago. Right now we are working on an new integration with an external service that is going to have access to one of our databases; the same one Squid has access to. This external system will inject new records to one particular collection, and we want to be aware of those changes so we can display those new records without the need of refreshing the page

The flow we want to implement is something like this:

Issue

We're usign Squid to retrieve the data from MongoDB, so while playing around with useObservable hook, I found what I think is an unexpected behavior. One you define the hook in your component, to keep track of changes made by Squid in a particular collection, the future queries made to the same collection stops reflecting what really is in the database, meaning it does not return any change made by external services.

Say you have an empty component that exceutes a function that each n seconds fetches the data in a collection, liki this:

useCollection('my_collection', 'integration_name').query().dereference().snapshot().then((data) => data)

It'll correctly reflect what data is in the database, meaning new recrods added directly to the database are being shown with no issues. All this stops working as soon as you add the useObservable hook to your component like:

  useObservable(
    () => {
      return useCollection('my_collection', 'integration_name').query().dereference().snapshots();
    }, { }, []
  );

The previous query is no longer reflecting the data in MongoDB. No matter if you add or remove multiple collections to the database, query keeps returning the exact same records as the first time it requested data

Evidences:

The following video serves as an evidence for the described issue Please see how data retrieval breaks as soon as the useCollection hook is commented out Video

https://github.com/itsmeurbi/squid-use-observable-issue/blob/main/evidence.mp4