Skip to content

localDocument.$.subscribe invoked twice for an atomic insert  #2471

Closed
@de-robat

Description

Hello, first of all kudos for this generally well thought of and nicely running software. This Issue is rather to be considered a question before opening a potential PR.

We recently run into a problem when upserting localDocuments. We have a line that reads as follows:

document.$.subscribe((doc: MyState) => {
        console.log(doc)
       ...
      })

And this one gets invoked twice for a single upsert. I could pinpoint the issue to this part of the code:

https://github.com/pubkey/rxdb/blob/master/src/plugins/local-documents.ts#L195
which reads like this:

_saveData(this: any, newData: any) {
        const oldData = this._dataSync$.getValue();
        newData = clone(newData);
        newData._id = LOCAL_PREFIX + this.id;

        const startTime = now();
        return this.parentPouch.put(newData)
            .then((res: any) => {
                const endTime = now();
                newData._rev = res.rev;
>>>         this._dataSync$.next(newData);

                const changeEvent = new RxChangeEvent(
                    'UPDATE',
                    this.id,
                    clone(this._data),
                    isRxDatabase(this.parent) ? this.parent.token : this.parent.database.token,
                    isRxCollection(this.parent) ? this.parent.name : null,
                    true,
                    startTime,
                    endTime,
                    oldData,
                    this
                );
>>>         this.$emit(changeEvent);
            });
    },

The two marked lines both invoke the observables listener. I do not understand if this is intended. To add some context, this basically leads for a react application of ours to invoke unnecessary (virtual) rendering cycles which we would like to avoid.

Thanks in advance!

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions