Closed thani-sh closed 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?
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.
@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
@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.
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.