mirror of
https://github.com/vector-im/hydrogen-web.git
synced 2024-12-23 11:35:04 +01:00
typescriptifying ApplyMap
This commit is contained in:
parent
95c65280ef
commit
ab6a8ad3aa
@ -20,8 +20,8 @@ import {RoomTileViewModel} from "./RoomTileViewModel.js";
|
|||||||
import {InviteTileViewModel} from "./InviteTileViewModel.js";
|
import {InviteTileViewModel} from "./InviteTileViewModel.js";
|
||||||
import {RoomBeingCreatedTileViewModel} from "./RoomBeingCreatedTileViewModel.js";
|
import {RoomBeingCreatedTileViewModel} from "./RoomBeingCreatedTileViewModel.js";
|
||||||
import {RoomFilter} from "./RoomFilter.js";
|
import {RoomFilter} from "./RoomFilter.js";
|
||||||
import {ApplyMap} from "../../../observable/map/ApplyMap.js";
|
import {ApplyMap} from "../../../observable/map/ApplyMap";
|
||||||
import {addPanelIfNeeded} from "../../navigation/index";
|
import {addPanelIfNeeded} from "../../navigation";
|
||||||
|
|
||||||
export class LeftPanelViewModel extends ViewModel {
|
export class LeftPanelViewModel extends ViewModel {
|
||||||
constructor(options) {
|
constructor(options) {
|
||||||
|
@ -14,11 +14,7 @@ See the License for the specific language governing permissions and
|
|||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import {SortedMapList} from "./list/SortedMapList.js";
|
|
||||||
import {FilteredMap} from "./map/FilteredMap.js";
|
|
||||||
import {MappedMap} from "./map/MappedMap.js";
|
|
||||||
import {JoinedMap} from "./map/JoinedMap.js";
|
|
||||||
import {BaseObservableMap} from "./map/BaseObservableMap";
|
|
||||||
// re-export "root" (of chain) collection
|
// re-export "root" (of chain) collection
|
||||||
export { ObservableMap } from "./map/ObservableMap";
|
export { ObservableMap } from "./map/ObservableMap";
|
||||||
export { ObservableArray } from "./list/ObservableArray";
|
export { ObservableArray } from "./list/ObservableArray";
|
||||||
@ -26,24 +22,3 @@ export { SortedArray } from "./list/SortedArray";
|
|||||||
export { MappedList } from "./list/MappedList";
|
export { MappedList } from "./list/MappedList";
|
||||||
export { AsyncMappedList } from "./list/AsyncMappedList";
|
export { AsyncMappedList } from "./list/AsyncMappedList";
|
||||||
export { ConcatList } from "./list/ConcatList";
|
export { ConcatList } from "./list/ConcatList";
|
||||||
|
|
||||||
|
|
||||||
// avoid circular dependency between these classes
|
|
||||||
// and BaseObservableMap (as they extend it)
|
|
||||||
Object.assign(BaseObservableMap.prototype, {
|
|
||||||
sortValues(comparator) {
|
|
||||||
return new SortedMapList(this, comparator);
|
|
||||||
},
|
|
||||||
|
|
||||||
mapValues(mapper, updater) {
|
|
||||||
return new MappedMap(this, mapper, updater);
|
|
||||||
},
|
|
||||||
|
|
||||||
filterValues(filter) {
|
|
||||||
return new FilteredMap(this, filter);
|
|
||||||
},
|
|
||||||
|
|
||||||
join(...otherMaps) {
|
|
||||||
return new JoinedMap([this].concat(otherMaps));
|
|
||||||
}
|
|
||||||
});
|
|
@ -14,45 +14,73 @@ 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 {FilteredMap} from "./FilteredMap.js";
|
||||||
|
import {SortedMapList} from "../list/SortedMapList.js";
|
||||||
|
|
||||||
export class ApplyMap extends BaseObservableMap {
|
|
||||||
constructor(source, apply) {
|
export class ApplyMap<K, V> extends BaseObservableMap<K, V> {
|
||||||
|
private _source: BaseObservableMap<K, V>;
|
||||||
|
private _subscription?: SubscriptionHandle;
|
||||||
|
private _apply?: Apply<K, V>;
|
||||||
|
private _config: BaseObservableMapConfig<K, V>;
|
||||||
|
|
||||||
|
constructor(source: BaseObservableMap<K, V>, apply?: Apply<K, V>) {
|
||||||
super();
|
super();
|
||||||
this._source = source;
|
this._source = source;
|
||||||
this._apply = apply;
|
this._apply = apply;
|
||||||
this._subscription = null;
|
this._config = config<K, V>();
|
||||||
|
}
|
||||||
|
|
||||||
|
join(...otherMaps: Array<typeof this>): JoinedMap<K, V> {
|
||||||
|
return this._config.join(this, ...otherMaps);
|
||||||
|
}
|
||||||
|
|
||||||
|
mapValues(mapper: any, updater?: (params: any) => void): MappedMap<K, V> {
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
hasApply() {
|
hasApply() {
|
||||||
return !!this._apply;
|
return !!this._apply;
|
||||||
}
|
}
|
||||||
|
|
||||||
setApply(apply) {
|
setApply(apply?: Apply<K, V>) {
|
||||||
this._apply = apply;
|
this._apply = apply;
|
||||||
if (apply) {
|
if (this._apply) {
|
||||||
this.applyOnce(this._apply);
|
this.applyOnce(this._apply);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
applyOnce(apply) {
|
applyOnce(apply: Apply<K, V>) {
|
||||||
for (const [key, value] of this._source) {
|
for (const [key, value] of this._source) {
|
||||||
apply(key, value);
|
apply(key, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
onAdd(key, value) {
|
onAdd(key: K, value: V) {
|
||||||
if (this._apply) {
|
if (this._apply) {
|
||||||
this._apply(key, value);
|
this._apply(key, value);
|
||||||
}
|
}
|
||||||
this.emitAdd(key, value);
|
this.emitAdd(key, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
onRemove(key, value) {
|
onRemove(key: K, value: V) {
|
||||||
this.emitRemove(key, value);
|
this.emitRemove(key, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
onUpdate(key, value, params) {
|
onUpdate(key: K, value: V, params: any) {
|
||||||
if (this._apply) {
|
if (this._apply) {
|
||||||
this._apply(key, value, params);
|
this._apply(key, value, params);
|
||||||
}
|
}
|
||||||
@ -69,7 +97,7 @@ export class ApplyMap extends BaseObservableMap {
|
|||||||
|
|
||||||
onUnsubscribeLast() {
|
onUnsubscribeLast() {
|
||||||
super.onUnsubscribeLast();
|
super.onUnsubscribeLast();
|
||||||
this._subscription = this._subscription();
|
if (this._subscription) this._subscription = this._subscription();
|
||||||
}
|
}
|
||||||
|
|
||||||
onReset() {
|
onReset() {
|
||||||
@ -87,7 +115,9 @@ export class ApplyMap extends BaseObservableMap {
|
|||||||
return this._source.size;
|
return this._source.size;
|
||||||
}
|
}
|
||||||
|
|
||||||
get(key) {
|
get(key: K) {
|
||||||
return this._source.get(key);
|
return this._source.get(key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Apply<K, V> = (key: K, value: V, params?: any) => void;
|
@ -23,7 +23,7 @@ import {SortedMapList} from "../list/SortedMapList.js";
|
|||||||
|
|
||||||
export class FilteredMap<K, V> extends BaseObservableMap<K, V> {
|
export class FilteredMap<K, V> extends BaseObservableMap<K, V> {
|
||||||
private _source: BaseObservableMap<K, V>;
|
private _source: BaseObservableMap<K, V>;
|
||||||
private _config: BaseObservableMapConfig<K, V>
|
private _config: BaseObservableMapConfig<K, V>;
|
||||||
private _filter: (value: V, key: K) => boolean;
|
private _filter: (value: V, key: K) => boolean;
|
||||||
private _included?: Map<K, boolean>;
|
private _included?: Map<K, boolean>;
|
||||||
private _subscription?: SubscriptionHandle;
|
private _subscription?: SubscriptionHandle;
|
||||||
|
Loading…
Reference in New Issue
Block a user