pubkey / rxdb

A fast, local first, reactive Database for JavaScript Applications https://rxdb.info/
https://rxdb.info/
Apache License 2.0
21k stars 1.02k forks source link

RxDB observers emit data when results are not affected #31

Closed thani-sh closed 7 years ago

thani-sh commented 7 years ago

I tried RxDB with the localstorage adapter. If I create an observer for a query and subscribe and then edit a document which do not come under the query's result. The observer will emit the result data again. Is this something RxDB wishes to solve? If interested, I can start a pull-request for this.

pubkey commented 7 years ago

@mnmtanish just to make sure I fully understood what you mean:

Current behaviour: When a RxQuery is observed and the collection changes data, the full result of the query is emited again, even when it is not affected by the data-change.

Your goal: When a RxQuery is observed and the collection changes data, the full result of the query is emited only if the query-result is affected by the data-change.

Did I get this right?

thani-sh commented 7 years ago

Yeah, although I'm not sure how to do it without slowing down RxDB. I thought of 2 ways to do this. I'm not sure which one will be faster or even whether RxDB needs this feature built into it.

1. Wrap the result observer Wrap the result observer, and when it emits, compare the result with the previous result and emit only if it changes.

function wrap(input: Observer<any>): Observer<any> {
  let prev = null
  return input.filter(data => {
    if(deepEqual(data, prev)) {
      return false
    }
    prev = data
    return true
  })
}

2. Re-run queries on changes The documents should have an id to identify them. When updating a collection, take the updated document ids updatedIds. Re-run all queries in the collection and get result document ids. If updated ids are there in result ids, emit results for tat query.


I'm planning to use RxDB result observers with Angular2. I'm looking for a way to avoid unnecessary UI renders. I did hear that Angular2 is smart about tracking changes so maybe It'll be faster if Angular2 did this itself. I'll start the PR if you thinks it'll be good for RxDB.

pubkey commented 7 years ago

@mnmtanish could you confirm or deny that the test in b83da0886c49160da2c9ca793dbb921742b30250 describes what you want? The code there basically does what you want, but I will refactor the RxQuery-observables in the near future, so please check the test.

EDIT: Also check 39fbf5b12b61844dff4457cc8183e2c1a5f82521

thani-sh commented 7 years ago

Yes, https://github.com/pubkey/rxdb/commit/39fbf5b12b61844dff4457cc8183e2c1a5f82521 describes it

pubkey commented 7 years ago

@mnmtanish I released the version 2.0.0 which covers the test. See CHANGELOG

Please check it out and tell me if all is ok.