This repository has been archived on 2022-08-19. You can view files and clone it, but cannot push or open issues or pull requests.
hydrogen-web/src/observable/map/FilteredMap.js

223 lines
6.6 KiB
JavaScript
Raw Normal View History

2020-08-05 22:08:55 +05:30
/*
Copyright 2020 Bruno Windels <bruno@windels.cloud>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import {BaseObservableMap} from "./BaseObservableMap";
2019-02-21 04:18:16 +05:30
export class FilteredMap extends BaseObservableMap {
2020-10-05 21:48:44 +05:30
constructor(source, filter) {
super();
this._source = source;
2020-10-05 21:48:44 +05:30
this._filter = filter;
/** @type {Map<string, bool>} */
this._included = null;
this._subscription = null;
2020-10-05 21:48:44 +05:30
}
setFilter(filter) {
this._filter = filter;
2021-04-27 19:45:20 +05:30
if (this._subscription) {
this._reapplyFilter();
}
2020-10-05 21:48:44 +05:30
}
/**
* reapply the filter
*/
2021-04-27 19:45:20 +05:30
_reapplyFilter(silent = false) {
2020-10-05 21:48:44 +05:30
if (this._filter) {
2021-04-27 19:45:20 +05:30
const oldIncluded = this._included;
2020-10-05 21:48:44 +05:30
this._included = this._included || new Map();
for (const [key, value] of this._source) {
const isIncluded = this._filter(value, key);
this._included.set(key, isIncluded);
2021-04-27 19:45:20 +05:30
if (!silent) {
const wasIncluded = oldIncluded ? oldIncluded.get(key) : true;
this._emitForUpdate(wasIncluded, isIncluded, key, value);
}
}
} else { // no filter
// did we have a filter before?
2021-04-27 19:45:20 +05:30
if (this._included && !silent) {
// add any non-included items again
for (const [key, value] of this._source) {
if (!this._included.get(key)) {
this.emitAdd(key, value);
}
}
2020-10-05 21:48:44 +05:30
}
this._included = null;
}
2019-02-21 04:18:16 +05:30
}
onAdd(key, value) {
2020-10-05 21:48:44 +05:30
if (this._filter) {
const included = this._filter(value, key);
this._included.set(key, included);
if (!included) {
return;
}
}
this.emitAdd(key, value);
2019-02-21 04:18:16 +05:30
}
2020-10-05 21:48:44 +05:30
onRemove(key, value) {
const wasIncluded = !this._filter || this._included.get(key);
this._included.delete(key);
if (wasIncluded) {
this.emitRemove(key, value);
2019-02-21 04:18:16 +05:30
}
}
onUpdate(key, value, params) {
guard against updates emitted while populating during first subscription This came up now because Timeline uses a MappedList to map PendingEvents to PendingEventEntries. In the map function, we setup links between entries to support local echo for relations. When opening a timeline that has unsent relations, the initial populating of the MappedList will try to emit an update for the target entry in remoteEntries. This all happens while the ListView of the timeline is calling subscribe and all collections in the chain are populating themselves based on their sources. This usually entails calling subscribe on the source, and now you are subscribed, iterate over the source (as you're not allowed to query an unsubscribed observable collection, as it might not be populated yet, and even if it did, it wouldn't be guaranteed to be up to date as events aren't flowing yet). So in this concrete example, TilesCollection hadn't populated its tiles yet and when the update to the target of the unsent relation reached TilesCollection, the tiles array was still null and it crashed. I thought what would be the best way to fix this and have a solid model for observable collections to ensure they are always compatible with each other. I considered splitting up the subscription process in two steps where you'd first populate the source and then explicitly start events flowing. I didn't go with this way because it's really only updates that make sense to be emitted during setup. A missed update wouldn't usually bring the collections out of sync like a missed add or remove would. It would just mean the UI isn't updated (or any subsequent filtered collections are not updated), but this should be fine to ignore during setup, as you can rely on the subscribing collections down the chain picking up the update while populating. If we ever want to support add or remove events during setup, we would have to explicitly support them, but for now they are correct to throw. So for now, just ignore update events that happen during setup where needed.
2021-05-27 13:32:05 +05:30
// if an update is emitted while calling source.subscribe() from onSubscribeFirst, ignore it
if (!this._included) {
return;
}
2020-10-05 21:48:44 +05:30
if (this._filter) {
const wasIncluded = this._included.get(key);
const isIncluded = this._filter(value, key);
this._included.set(key, isIncluded);
this._emitForUpdate(wasIncluded, isIncluded, key, value, params);
} else {
this.emitUpdate(key, value, params);
}
}
2020-10-05 21:48:44 +05:30
_emitForUpdate(wasIncluded, isIncluded, key, value, params = null) {
if (wasIncluded && !isIncluded) {
this.emitRemove(key, value);
} else if (!wasIncluded && isIncluded) {
this.emitAdd(key, value);
} else if (wasIncluded && isIncluded) {
this.emitUpdate(key, value, params);
2019-02-21 04:18:16 +05:30
}
}
onSubscribeFirst() {
this._subscription = this._source.subscribe(this);
2021-04-27 19:45:20 +05:30
this._reapplyFilter(true);
2019-02-21 04:18:16 +05:30
super.onSubscribeFirst();
}
onUnsubscribeLast() {
super.onUnsubscribeLast();
2020-10-05 21:48:44 +05:30
this._included = null;
this._subscription = this._subscription();
2019-02-21 04:18:16 +05:30
}
onReset() {
2021-04-27 19:45:20 +05:30
this._reapplyFilter();
2019-02-21 04:18:16 +05:30
this.emitReset();
}
[Symbol.iterator]() {
2020-10-05 21:48:44 +05:30
return new FilterIterator(this._source, this._included);
}
get size() {
let count = 0;
this._included.forEach(included => {
if (included) {
count += 1;
}
});
return count;
}
get(key) {
const value = this._source.get(key);
if (value && this._filter(value, key)) {
return value;
}
}
2020-10-05 21:48:44 +05:30
}
class FilterIterator {
constructor(map, _included) {
this._included = _included;
2021-04-27 19:45:20 +05:30
this._sourceIterator = map[Symbol.iterator]();
2020-10-05 21:48:44 +05:30
}
next() {
// eslint-disable-next-line no-constant-condition
while (true) {
const sourceResult = this._sourceIterator.next();
if (sourceResult.done) {
return sourceResult;
}
2021-04-27 19:45:20 +05:30
const key = sourceResult.value[0];
2020-10-05 21:48:44 +05:30
if (this._included.get(key)) {
return sourceResult;
}
}
}
}
import {ObservableMap} from "./ObservableMap";
2021-04-27 19:45:20 +05:30
export function tests() {
return {
"filter preloaded list": assert => {
const source = new ObservableMap();
source.add("one", 1);
source.add("two", 2);
source.add("three", 3);
const oddNumbers = new FilteredMap(source, x => x % 2 !== 0);
// can only iterate after subscribing
oddNumbers.subscribe({});
assert.equal(oddNumbers.size, 2);
const it = oddNumbers[Symbol.iterator]();
assert.deepEqual(it.next().value, ["one", 1]);
assert.deepEqual(it.next().value, ["three", 3]);
assert.equal(it.next().done, true);
},
// "filter added values": assert => {
// },
// "filter removed 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);
}
2021-04-27 19:45:20 +05:30
}
}