mirror of
https://github.com/vector-im/hydrogen-web.git
synced 2024-12-23 03:25:12 +01:00
Merge pull request #1005 from vector-im/bwindels/observe-size
add `ObservableMap.observeSize` operator
This commit is contained in:
commit
1a0b1670c8
@ -18,6 +18,7 @@ import {BaseObservable} from "../BaseObservable";
|
||||
import {JoinedMap} from "./index";
|
||||
import {MappedMap} from "./index";
|
||||
import {FilteredMap} from "./index";
|
||||
import {BaseObservableValue, MapSizeObservableValue} from "../value/index";
|
||||
import {SortedMapList} from "../list/SortedMapList.js";
|
||||
|
||||
|
||||
@ -66,19 +67,23 @@ export abstract class BaseObservableMap<K, V> extends BaseObservable<IMapObserve
|
||||
|
||||
join<E extends BaseObservableMap<K, V>>(...otherMaps: Array<E>): JoinedMap<K, V> {
|
||||
return new JoinedMap([this as BaseObservableMap<K, V>].concat(otherMaps));
|
||||
}
|
||||
}
|
||||
|
||||
mapValues<MappedV>(mapper: Mapper<V, MappedV>, updater?: Updater<V, MappedV>): MappedMap<K, V, MappedV> {
|
||||
return new MappedMap(this, mapper, updater);
|
||||
}
|
||||
mapValues<MappedV>(mapper: Mapper<V, MappedV>, updater?: Updater<V, MappedV>): MappedMap<K, V, MappedV> {
|
||||
return new MappedMap(this, mapper, updater);
|
||||
}
|
||||
|
||||
sortValues(comparator: Comparator<V>): SortedMapList {
|
||||
return new SortedMapList(this, comparator);
|
||||
}
|
||||
sortValues(comparator: Comparator<V>): SortedMapList {
|
||||
return new SortedMapList(this, comparator);
|
||||
}
|
||||
|
||||
filterValues(filter: Filter<K, V>): FilteredMap<K, V> {
|
||||
return new FilteredMap(this, filter);
|
||||
}
|
||||
filterValues(filter: Filter<K, V>): FilteredMap<K, V> {
|
||||
return new FilteredMap(this, filter);
|
||||
}
|
||||
|
||||
observeSize(): BaseObservableValue<number> {
|
||||
return new MapSizeObservableValue(this);
|
||||
}
|
||||
|
||||
abstract [Symbol.iterator](): Iterator<[K, V]>;
|
||||
abstract get size(): number;
|
||||
|
71
src/observable/value/MapSizeObservableValue.ts
Normal file
71
src/observable/value/MapSizeObservableValue.ts
Normal file
@ -0,0 +1,71 @@
|
||||
/*
|
||||
Copyright 2022 The Matrix.org Foundation C.I.C.
|
||||
|
||||
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 {BaseObservableValue} from "./index";
|
||||
import {BaseObservableMap} from "../map/index";
|
||||
import type {SubscriptionHandle} from "../BaseObservable";
|
||||
|
||||
export class MapSizeObservableValue<K, V> extends BaseObservableValue<number> {
|
||||
private subscription?: SubscriptionHandle;
|
||||
|
||||
constructor(private readonly map: BaseObservableMap<K, V>)
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
onSubscribeFirst(): void {
|
||||
this.subscription = this.map.subscribe({
|
||||
onAdd: (key: K, value: V) => {
|
||||
this.emit(this.get());
|
||||
},
|
||||
onRemove: (key: K, value: V) => {
|
||||
this.emit(this.get());
|
||||
},
|
||||
onUpdate: (key: K, value: V) => {},
|
||||
onReset: () => {
|
||||
this.emit(this.get());
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
onUnsubscribeLast(): void {
|
||||
this.subscription = this.subscription?.();
|
||||
}
|
||||
|
||||
get(): number {
|
||||
return this.map.size;
|
||||
}
|
||||
}
|
||||
|
||||
import {ObservableMap} from "../map/index";
|
||||
|
||||
export function tests() {
|
||||
return {
|
||||
"emits update on add and remove": assert => {
|
||||
const map = new ObservableMap<string, number>();
|
||||
const size = new MapSizeObservableValue(map);
|
||||
const updates: number[] = [];
|
||||
size.subscribe(size => {
|
||||
updates.push(size);
|
||||
});
|
||||
map.add("hello", 1);
|
||||
map.add("world", 2);
|
||||
map.remove("world");
|
||||
map.remove("hello");
|
||||
assert.deepEqual(updates, [1, 2, 1, 0]);
|
||||
}
|
||||
};
|
||||
}
|
@ -12,4 +12,5 @@ export {EventObservableValue} from './EventObservableValue';
|
||||
export {FlatMapObservableValue} from './FlatMapObservableValue';
|
||||
export {PickMapObservableValue} from './PickMapObservableValue';
|
||||
export {RetainedObservableValue} from './RetainedObservableValue';
|
||||
export {MapSizeObservableValue} from './MapSizeObservableValue';
|
||||
export {ObservableValue} from './ObservableValue';
|
||||
|
Loading…
Reference in New Issue
Block a user