Fix bug in FilteredMap

Caused multiple updates to be triggered.

Signed-off-by: RMidhunSuresh <rmidhunsuresh@gmail.com>
This commit is contained in:
RMidhunSuresh 2021-06-24 16:26:33 +05:30
parent 404129f60e
commit dea0cad252

View file

@ -91,9 +91,10 @@ export class FilteredMap extends BaseObservableMap {
const isIncluded = this._filter(value, key); const isIncluded = this._filter(value, key);
this._included.set(key, isIncluded); this._included.set(key, isIncluded);
this._emitForUpdate(wasIncluded, isIncluded, key, value, params); this._emitForUpdate(wasIncluded, isIncluded, key, value, params);
} } else {
this.emitUpdate(key, value, params); this.emitUpdate(key, value, params);
} }
}
_emitForUpdate(wasIncluded, isIncluded, key, value, params = null) { _emitForUpdate(wasIncluded, isIncluded, key, value, params = null) {
if (wasIncluded && !isIncluded) { if (wasIncluded && !isIncluded) {
@ -191,5 +192,31 @@ export function tests() {
// "filter changed values": assert => { // "filter changed values": assert => {
// }, // },
"emits must trigger once": assert => {
const source = new ObservableMap();
let count_add = 0, count_update = 0, count_remove = 0;
source.add("num1", 1);
source.add("num2", 2);
source.add("num3", 3);
const oddMap = new FilteredMap(source, x => x % 2 !== 0);
oddMap.subscribe({
onAdd() {
count_add += 1;
},
onRemove() {
count_remove += 1;
},
onUpdate() {
count_update += 1;
}
});
source.set("num3", 4);
source.set("num3", 5);
source.set("num3", 7);
assert.strictEqual(count_add, 1);
assert.strictEqual(count_update, 1);
assert.strictEqual(count_remove, 1);
}
} }
} }