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

184 lines
5.2 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.js";
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;
this.update();
}
/**
* reapply the filter
*/
2020-10-05 21:48:44 +05:30
update() {
// TODO: need to check if we have a subscriber already? If not, we really should not iterate the source?
if (this._filter) {
const hadFilterBefore = !!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);
const wasIncluded = hadFilterBefore ? this._included.get(key) : true;
this._included.set(key, isIncluded);
this._emitForUpdate(wasIncluded, isIncluded, key, value);
}
} else { // no filter
// did we have a filter before?
if (this._included) {
// 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) {
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);
}
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);
2020-10-05 21:48:44 +05:30
this.update();
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() {
2020-10-05 21:48:44 +05:30
this.update();
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;
this._sourceIterator = map.entries();
}
next() {
// eslint-disable-next-line no-constant-condition
while (true) {
const sourceResult = this._sourceIterator.next();
if (sourceResult.done) {
return sourceResult;
}
const key = sourceResult.value[1];
if (this._included.get(key)) {
return sourceResult;
}
}
}
}
// import {ObservableMap} from "./ObservableMap.js";
// 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 odds = Array.from(new FilteredMap(source, x => x % 2 !== 0));
// assert.equal(odds.length, 2);
// },
// "filter added values": assert => {
// },
// "filter removed values": assert => {
// },
// "filter changed values": assert => {
// },
// }
// }