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.
|
|
|
|
*/
|
|
|
|
|
2020-04-21 00:56:39 +05:30
|
|
|
import {BaseObservableMap} from "./BaseObservableMap.js";
|
2019-02-21 04:18:16 +05:30
|
|
|
|
2020-04-21 00:56:39 +05:30
|
|
|
export class FilteredMap extends BaseObservableMap {
|
2020-10-05 21:48:44 +05:30
|
|
|
constructor(source, filter) {
|
2019-02-27 01:18:57 +05:30
|
|
|
super();
|
|
|
|
this._source = source;
|
2020-10-05 21:48:44 +05:30
|
|
|
this._filter = filter;
|
|
|
|
/** @type {Map<string, bool>} */
|
|
|
|
this._included = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
setFilter(filter) {
|
|
|
|
this._filter = filter;
|
|
|
|
this.update();
|
|
|
|
}
|
|
|
|
|
|
|
|
update() {
|
|
|
|
// TODO: need to check if we have a subscriber already? If not, we really should not iterate the source?
|
|
|
|
if (this._filter) {
|
|
|
|
this._included = this._included || new Map();
|
|
|
|
for (const [key, value] of this._source) {
|
|
|
|
this._included.set(key, this._filter(value, key));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
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) {
|
|
|
|
if (this._filter && !this._included.get(key)) {
|
|
|
|
return;
|
2019-02-21 04:18:16 +05:30
|
|
|
}
|
2020-10-05 21:48:44 +05:30
|
|
|
this.emitRemove(key, value);
|
2019-02-21 04:18:16 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
onChange(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);
|
|
|
|
|
|
|
|
if (wasIncluded && !isIncluded) {
|
|
|
|
this.emitRemove(key, value);
|
|
|
|
} else if (!wasIncluded && isIncluded) {
|
|
|
|
this.emitAdd(key, value);
|
|
|
|
} else if (!wasIncluded && !isIncluded) {
|
|
|
|
return;
|
|
|
|
} // fall through to emitChange
|
2019-02-21 04:18:16 +05:30
|
|
|
}
|
2020-10-05 21:48:44 +05:30
|
|
|
this.emitChange(key, value, params);
|
2019-02-21 04:18:16 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
onSubscribeFirst() {
|
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;
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 => {
|
|
|
|
|
|
|
|
},
|
2019-02-21 04:18:16 +05:30
|
|
|
}
|
|
|
|
}
|