mirror of
https://github.com/vector-im/hydrogen-web.git
synced 2025-01-22 10:11:39 +01:00
FilteredMap
This commit is contained in:
parent
7645eb8753
commit
f1751a24b0
@ -80,5 +80,5 @@ export function tests() {
|
|||||||
assert.equal(c.firstSubscribeCalls, 1);
|
assert.equal(c.firstSubscribeCalls, 1);
|
||||||
assert.equal(c.firstUnsubscribeCalls, 1);
|
assert.equal(c.firstUnsubscribeCalls, 1);
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
@ -31,7 +31,7 @@ export type BaseObservableMapConfig<K, V> = {
|
|||||||
join(_this: BaseObservableMap<K, V>, ...otherMaps: Array<BaseObservableMap<K, V>>): JoinedMap;
|
join(_this: BaseObservableMap<K, V>, ...otherMaps: Array<BaseObservableMap<K, V>>): JoinedMap;
|
||||||
mapValues(_this: BaseObservableMap<K, V>, mapper: any, updater?: (params: any) => void): MappedMap;
|
mapValues(_this: BaseObservableMap<K, V>, mapper: any, updater?: (params: any) => void): MappedMap;
|
||||||
sortValues(_this: BaseObservableMap<K, V>, comparator?: (a: any, b: any) => number): SortedMapList;
|
sortValues(_this: BaseObservableMap<K, V>, comparator?: (a: any, b: any) => number): SortedMapList;
|
||||||
filterValues(_this: BaseObservableMap<K, V>, filter: (v: V, k: K) => boolean): FilteredMap;
|
filterValues(_this: BaseObservableMap<K, V>, filter: (v: V, k: K) => boolean): FilteredMap<K, V>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export abstract class BaseObservableMap<K, V> extends BaseObservable<IMapObserver<K, V>> {
|
export abstract class BaseObservableMap<K, V> extends BaseObservable<IMapObserver<K, V>> {
|
||||||
@ -48,13 +48,13 @@ export abstract class BaseObservableMap<K, V> extends BaseObservable<IMapObserve
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
emitUpdate(key, value, params) {
|
emitUpdate(key: K, value: V, params: any) {
|
||||||
for(let h of this._handlers) {
|
for(let h of this._handlers) {
|
||||||
h.onUpdate(key, value, params);
|
h.onUpdate(key, value, params);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
emitRemove(key, value) {
|
emitRemove(key: K, value: V) {
|
||||||
for(let h of this._handlers) {
|
for(let h of this._handlers) {
|
||||||
h.onRemove(key, value);
|
h.onRemove(key, value);
|
||||||
}
|
}
|
||||||
@ -69,7 +69,7 @@ export abstract class BaseObservableMap<K, V> extends BaseObservable<IMapObserve
|
|||||||
abstract join(...otherMaps: Array<typeof this>): JoinedMap;
|
abstract join(...otherMaps: Array<typeof this>): JoinedMap;
|
||||||
abstract mapValues(mapper: any, updater?: (params: any) => void): MappedMap;
|
abstract mapValues(mapper: any, updater?: (params: any) => void): MappedMap;
|
||||||
abstract sortValues(comparator?: (a: any, b: any) => number): SortedMapList;
|
abstract sortValues(comparator?: (a: any, b: any) => number): SortedMapList;
|
||||||
abstract filterValues(filter: (v: V, k: K) => boolean): FilteredMap;
|
abstract filterValues(filter: (v: V, k: K) => boolean): FilteredMap<K, V>;
|
||||||
|
|
||||||
abstract [Symbol.iterator](): Iterator<[K, V]>;
|
abstract [Symbol.iterator](): Iterator<[K, V]>;
|
||||||
abstract get size(): number;
|
abstract get size(): number;
|
||||||
|
@ -14,19 +14,44 @@ See the License for the specific language governing permissions and
|
|||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import {BaseObservableMap} from "./BaseObservableMap";
|
import {BaseObservableMap, BaseObservableMapConfig} from "./BaseObservableMap";
|
||||||
|
import {SubscriptionHandle} from "../BaseObservable";
|
||||||
|
import {config} from "./config";
|
||||||
|
import {JoinedMap} from "./JoinedMap.js";
|
||||||
|
import {MappedMap} from "./MappedMap.js";
|
||||||
|
import {SortedMapList} from "../list/SortedMapList.js";
|
||||||
|
|
||||||
export class FilteredMap extends BaseObservableMap {
|
export class FilteredMap<K, V> extends BaseObservableMap<K, V> {
|
||||||
constructor(source, filter) {
|
private _source: BaseObservableMap<K, V>;
|
||||||
|
private _config: BaseObservableMapConfig<K, V>
|
||||||
|
private _filter: (value: V, key: K) => boolean;
|
||||||
|
private _included?: Map<K, boolean>;
|
||||||
|
private _subscription?: SubscriptionHandle;
|
||||||
|
|
||||||
|
constructor(source: BaseObservableMap<K, V>, filter: (value: V, key: K) => boolean) {
|
||||||
super();
|
super();
|
||||||
this._source = source;
|
this._source = source;
|
||||||
this._filter = filter;
|
this._filter = filter;
|
||||||
/** @type {Map<string, bool>} */
|
this._config = config<K, V>();
|
||||||
this._included = null;
|
|
||||||
this._subscription = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
setFilter(filter) {
|
join(...otherMaps: Array<typeof this>): JoinedMap {
|
||||||
|
return this._config.join(this, ...otherMaps);
|
||||||
|
}
|
||||||
|
|
||||||
|
mapValues(mapper: any, updater?: (params: any) => void): MappedMap{
|
||||||
|
return this._config.mapValues(this, mapper, updater);
|
||||||
|
}
|
||||||
|
|
||||||
|
sortValues(comparator?: (a: any, b: any) => number): SortedMapList {
|
||||||
|
return this._config.sortValues(this, comparator);
|
||||||
|
}
|
||||||
|
|
||||||
|
filterValues(filter: (v: V, k: K) => boolean): FilteredMap<K, V> {
|
||||||
|
return this._config.filterValues(this, filter);
|
||||||
|
}
|
||||||
|
|
||||||
|
setFilter(filter: (value: V, key: K) => boolean) {
|
||||||
this._filter = filter;
|
this._filter = filter;
|
||||||
if (this._subscription) {
|
if (this._subscription) {
|
||||||
this._reapplyFilter();
|
this._reapplyFilter();
|
||||||
@ -58,30 +83,38 @@ export class FilteredMap extends BaseObservableMap {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this._included = null;
|
this._included = undefined;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
onAdd(key, value) {
|
onAdd(key: K, value: V) {
|
||||||
if (this._filter) {
|
if (this._filter) {
|
||||||
|
if (this._included) {
|
||||||
const included = this._filter(value, key);
|
const included = this._filter(value, key);
|
||||||
this._included.set(key, included);
|
this._included.set(key, included);
|
||||||
if (!included) {
|
if (!included) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
throw new Error("Internal logic error: FilteredMap._included used before initialized");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
this.emitAdd(key, value);
|
this.emitAdd(key, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
onRemove(key, value) {
|
onRemove(key: K, value: V) {
|
||||||
const wasIncluded = !this._filter || this._included.get(key);
|
const wasIncluded = !this._filter || this._included?.get(key);
|
||||||
|
if (this._included) {
|
||||||
this._included.delete(key);
|
this._included.delete(key);
|
||||||
if (wasIncluded) {
|
if (wasIncluded) {
|
||||||
this.emitRemove(key, value);
|
this.emitRemove(key, value);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
throw new Error("Internal logic error: FilteredMap._included used before initialized");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
onUpdate(key, value, params) {
|
onUpdate(key: K, value: V, params: any) {
|
||||||
// if an update is emitted while calling source.subscribe() from onSubscribeFirst, ignore it
|
// if an update is emitted while calling source.subscribe() from onSubscribeFirst, ignore it
|
||||||
if (!this._included) {
|
if (!this._included) {
|
||||||
return;
|
return;
|
||||||
@ -96,7 +129,7 @@ export class FilteredMap extends BaseObservableMap {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_emitForUpdate(wasIncluded, isIncluded, key, value, params = null) {
|
_emitForUpdate(wasIncluded: boolean | undefined, isIncluded: boolean, key: K, value: V, params: any = null) {
|
||||||
if (wasIncluded && !isIncluded) {
|
if (wasIncluded && !isIncluded) {
|
||||||
this.emitRemove(key, value);
|
this.emitRemove(key, value);
|
||||||
} else if (!wasIncluded && isIncluded) {
|
} else if (!wasIncluded && isIncluded) {
|
||||||
@ -114,9 +147,11 @@ export class FilteredMap extends BaseObservableMap {
|
|||||||
|
|
||||||
onUnsubscribeLast() {
|
onUnsubscribeLast() {
|
||||||
super.onUnsubscribeLast();
|
super.onUnsubscribeLast();
|
||||||
this._included = null;
|
this._included = undefined;
|
||||||
|
if (this._subscription) {
|
||||||
this._subscription = this._subscription();
|
this._subscription = this._subscription();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
onReset() {
|
onReset() {
|
||||||
this._reapplyFilter();
|
this._reapplyFilter();
|
||||||
@ -124,12 +159,12 @@ export class FilteredMap extends BaseObservableMap {
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Symbol.iterator]() {
|
[Symbol.iterator]() {
|
||||||
return new FilterIterator(this._source, this._included);
|
return new FilterIterator<K, V>(this._source, this._included);
|
||||||
}
|
}
|
||||||
|
|
||||||
get size() {
|
get size() {
|
||||||
let count = 0;
|
let count = 0;
|
||||||
this._included.forEach(included => {
|
this._included?.forEach(included => {
|
||||||
if (included) {
|
if (included) {
|
||||||
count += 1;
|
count += 1;
|
||||||
}
|
}
|
||||||
@ -145,9 +180,11 @@ export class FilteredMap extends BaseObservableMap {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class FilterIterator {
|
class FilterIterator<K, V> {
|
||||||
constructor(map, _included) {
|
private _included?: Map<K, boolean>
|
||||||
this._included = _included;
|
private _sourceIterator: Iterator<[K, V], any, undefined>
|
||||||
|
constructor(map: BaseObservableMap<K, V>, included?: Map<K, boolean>) {
|
||||||
|
this._included = included;
|
||||||
this._sourceIterator = map[Symbol.iterator]();
|
this._sourceIterator = map[Symbol.iterator]();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -159,14 +196,14 @@ class FilterIterator {
|
|||||||
return sourceResult;
|
return sourceResult;
|
||||||
}
|
}
|
||||||
const key = sourceResult.value[0];
|
const key = sourceResult.value[0];
|
||||||
if (this._included.get(key)) {
|
if (this._included?.get(key)) {
|
||||||
return sourceResult;
|
return sourceResult;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
import {ObservableMap} from "../";
|
import {ObservableMap} from "..";
|
||||||
export function tests() {
|
export function tests() {
|
||||||
return {
|
return {
|
||||||
"filter preloaded list": assert => {
|
"filter preloaded list": assert => {
|
||||||
@ -174,9 +211,22 @@ export function tests() {
|
|||||||
source.add("one", 1);
|
source.add("one", 1);
|
||||||
source.add("two", 2);
|
source.add("two", 2);
|
||||||
source.add("three", 3);
|
source.add("three", 3);
|
||||||
const oddNumbers = new FilteredMap(source, x => x % 2 !== 0);
|
const oddNumbers = new FilteredMap(source, (x: number) => x % 2 !== 0);
|
||||||
// can only iterate after subscribing
|
// can only iterate after subscribing
|
||||||
oddNumbers.subscribe({});
|
oddNumbers.subscribe({
|
||||||
|
onAdd() {
|
||||||
|
return;
|
||||||
|
},
|
||||||
|
onRemove() {
|
||||||
|
return;
|
||||||
|
},
|
||||||
|
onUpdate() {
|
||||||
|
return;
|
||||||
|
},
|
||||||
|
onReset() {
|
||||||
|
return;
|
||||||
|
},
|
||||||
|
});
|
||||||
assert.equal(oddNumbers.size, 2);
|
assert.equal(oddNumbers.size, 2);
|
||||||
const it = oddNumbers[Symbol.iterator]();
|
const it = oddNumbers[Symbol.iterator]();
|
||||||
assert.deepEqual(it.next().value, ["one", 1]);
|
assert.deepEqual(it.next().value, ["one", 1]);
|
||||||
@ -199,7 +249,7 @@ export function tests() {
|
|||||||
source.add("num1", 1);
|
source.add("num1", 1);
|
||||||
source.add("num2", 2);
|
source.add("num2", 2);
|
||||||
source.add("num3", 3);
|
source.add("num3", 3);
|
||||||
const oddMap = new FilteredMap(source, x => x % 2 !== 0);
|
const oddMap = new FilteredMap(source, (x: number) => x % 2 !== 0);
|
||||||
oddMap.subscribe({
|
oddMap.subscribe({
|
||||||
onAdd() {
|
onAdd() {
|
||||||
count_add += 1;
|
count_add += 1;
|
||||||
@ -209,6 +259,9 @@ export function tests() {
|
|||||||
},
|
},
|
||||||
onUpdate() {
|
onUpdate() {
|
||||||
count_update += 1;
|
count_update += 1;
|
||||||
|
},
|
||||||
|
onReset() {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
source.set("num3", 4);
|
source.set("num3", 4);
|
||||||
@ -218,5 +271,5 @@ export function tests() {
|
|||||||
assert.strictEqual(count_update, 1);
|
assert.strictEqual(count_update, 1);
|
||||||
assert.strictEqual(count_remove, 1);
|
assert.strictEqual(count_remove, 1);
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
}
|
}
|
@ -44,7 +44,7 @@ export class ObservableMap<K, V> extends BaseObservableMap<K, V> {
|
|||||||
return this._config.sortValues(this, comparator);
|
return this._config.sortValues(this, comparator);
|
||||||
}
|
}
|
||||||
|
|
||||||
filterValues(filter: (v: V, k: K) => boolean): FilteredMap {
|
filterValues(filter: (v: V, k: K) => boolean): FilteredMap<K, V> {
|
||||||
return this._config.filterValues(this, filter);
|
return this._config.filterValues(this, filter);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -36,7 +36,7 @@ export function config<K, V>(): BaseObservableMapConfig<K, V> {
|
|||||||
sortValues: (_this: BaseObservableMap<K, V>, comparator?: (a: any, b: any) => number): SortedMapList => {
|
sortValues: (_this: BaseObservableMap<K, V>, comparator?: (a: any, b: any) => number): SortedMapList => {
|
||||||
return new SortedMapList(_this, comparator);
|
return new SortedMapList(_this, comparator);
|
||||||
},
|
},
|
||||||
filterValues: (_this: BaseObservableMap<K, V>, filter: (v: V, k: K) => boolean): FilteredMap => {
|
filterValues: (_this: BaseObservableMap<K, V>, filter: (v: V, k: K) => boolean): FilteredMap<K, V> => {
|
||||||
return new FilteredMap(_this, filter);
|
return new FilteredMap(_this, filter);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user