Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

collectionData fires twice #50

Open
benzmarkus opened this issue Jan 14, 2022 · 11 comments
Open

collectionData fires twice #50

benzmarkus opened this issue Jan 14, 2022 · 11 comments

Comments

@benzmarkus
Copy link

Using the newest version and if i use collectionData(query(ref)) it fires once so this is correct.
If I am using collectionData(query(ref, ...whereStatements) and with one where statement it fires twice to the subscribe method returns twice.

@mvgijssel
Copy link

I'm experiencing the exact same issue using rxfire 6.0.3 also with a collectionData and a where condition.

@davideast
Copy link
Collaborator

Hey @benzmarkus and @mvgijssel! Thanks for the report! Can one of you create a small sample in StackBlitz, a small GitHub repo, or something of the like? I'd be happy to debug.

@ronnelreposo
Copy link

The same experience @davideast, I tried the code snipper in example-use, without where statement.

@mlahp7
Copy link

mlahp7 commented Mar 9, 2022

We are experiencing this issue as well.
It doesn't happen every time collectionData is called, but on subsequent calls. The first chunk of data is only a subset of the result we are expecting, the second chunk that comes through is the full result we expect.

@masterodi
Copy link

Did anyone find a solution or workaround for this?

@SpraylnlPray
Copy link

@davideast As @ronnelreposo mentioned it can be reproduced with the snippet in the example use from the readme. Do you need more information to be able to reproduce the issue?

@SpraylnlPray
Copy link

SpraylnlPray commented Jun 5, 2022

I have a (ugly but working) workaround for the time being. For me the first time I subscribed to the query it behaved correctly, but afterwards the problem @benzmarkus describes occurred.

In my case the query is called within an effect from a component store. Inside the store I have a variable called firstSubscription, that tracks if its the first subscription or not (Captain Obvious right there).
If that variable is false, I skip the first emission from collectionData:

this.effect((queryParam$: Observable<ParamType>) =>
  queryParam$.pipe(
    distinctUntilChanged(),
    withLatestFrom(this.state$),
    switchMap(([param, state]: [ParamType, ComponentState]) =>
      this._dataService.getDataAccordingToParam(param).pipe(  // returns collectionData with where statements
        skip(state.firstSubscription ? 0 : 1), // skip emission accordingly
      )
    ),
    tap(() =>
      this.patchState({ state.firstSubscription: false })
    )
    .... // transformat data
    tap((data: Data[]) => {
      this.patchState({
        data
      });
    })
  )
)(this.queryParam$);

Hope this helps someone

@narongdejsrn
Copy link

Another workaround that I found is to use throttleTime

For example

.pipe(
  throttleTime(500),
)

@markklesel
Copy link

@dependabot reopen

@Supamiu
Copy link

Supamiu commented Oct 20, 2022

Any news on that? It's quite impactful when some other operations depend on that and thus fire twice themselves, throttleTime and other operators being just hotfixes that don't really fix the main issue.

@jits
Copy link

jits commented Aug 15, 2023

This is likely related to the includeMetadataChanges option being hard coded to true when setting up the onSnapshot, which will always trigger the snapshot callback twice after writes (as this is the behaviour of the underlying Firebase library – it performs optimistic updates client-side, and then updates the hasPendingWrites metadata flag after the server-side update is confirmed).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests