Adds types for common functions

This commit is contained in:
Isaiah Becker-Mayer 2022-07-09 12:15:32 -04:00
parent 674e7bd1c6
commit be570cafb0
9 changed files with 51 additions and 46 deletions

View File

@ -46,7 +46,7 @@ export class MemberListViewModel extends ViewModel {
const vm = new MemberTileViewModel(this.childOptions({member, emitChange, mediaRepository}));
this.nameDisambiguator.disambiguate(vm);
return vm;
}
};
const updater = (params, vm, newMember) => {
vm.updateFrom(newMember);
this.nameDisambiguator.disambiguate(vm);

View File

@ -16,7 +16,7 @@ limitations under the License.
import {BaseObservableMap, BaseObservableMapConfig} from "./BaseObservableMap";
import {SubscriptionHandle} from "../BaseObservable";
import {config} from "./config";
import {config, Mapper, Updater, Comparator, Filter} from "./config";
import {JoinedMap} from "./JoinedMap.js";
import {MappedMap} from "./MappedMap.js";
import {FilteredMap} from "./FilteredMap.js";
@ -40,15 +40,15 @@ export class ApplyMap<K, V> extends BaseObservableMap<K, V> {
return this._config.join(this, ...otherMaps);
}
mapValues(mapper: any, updater?: (params: any) => void): MappedMap<K, V> {
mapValues(mapper: Mapper<V>, updater?: Updater<V>): MappedMap<K, V> {
return this._config.mapValues(this, mapper, updater);
}
sortValues(comparator: (a: V, b: V) => number): SortedMapList {
sortValues(comparator: Comparator<V>): SortedMapList {
return this._config.sortValues(this, comparator);
}
filterValues(filter: (v: V, k: K) => boolean): FilteredMap<K, V> {
filterValues(filter: Filter<K, V>): FilteredMap<K, V> {
return this._config.filterValues(this, filter);
}

View File

@ -19,6 +19,7 @@ import {JoinedMap} from "../map/JoinedMap.js";
import {MappedMap} from "../map/MappedMap.js";
import {FilteredMap} from "../map/FilteredMap.js";
import {SortedMapList} from "../list/SortedMapList.js";
import {Mapper, Updater, Comparator, Filter} from "./config";
export interface IMapObserver<K, V> {
onReset(): void;
@ -29,9 +30,9 @@ export interface IMapObserver<K, V> {
export type BaseObservableMapConfig<K, V> = {
join(_this: BaseObservableMap<K, V>, ...otherMaps: Array<BaseObservableMap<K, V>>): JoinedMap<K, V>;
mapValues(_this: BaseObservableMap<K, V>, mapper: any, updater?: (params: any) => void): MappedMap<K, V>;
sortValues(_this: BaseObservableMap<K, V>, comparator: (a: V, b: V) => number): SortedMapList;
filterValues(_this: BaseObservableMap<K, V>, filter: (v: V, k: K) => boolean): FilteredMap<K, V>;
mapValues(_this: BaseObservableMap<K, V>, mapper: any, updater?: Updater<V>): MappedMap<K, V>;
sortValues(_this: BaseObservableMap<K, V>, comparator: Comparator<V>): SortedMapList;
filterValues(_this: BaseObservableMap<K, V>, filter: Filter<K, V>): FilteredMap<K, V>;
}
export abstract class BaseObservableMap<K, V> extends BaseObservable<IMapObserver<K, V>> {
@ -67,9 +68,9 @@ export abstract class BaseObservableMap<K, V> extends BaseObservable<IMapObserve
// to easily use the default implementation in a class that extends
// this one (which is most likely what you want to do).
abstract join(...otherMaps: Array<typeof this>): JoinedMap<K, V>;
abstract mapValues(mapper: any, updater?: (params: any) => void): MappedMap<K, V>;
abstract sortValues(comparator: (a: V, b: V) => number): SortedMapList;
abstract filterValues(filter: (v: V, k: K) => boolean): FilteredMap<K, V>;
abstract mapValues(mapper: Mapper<V>, updater?: Updater<V>): MappedMap<K, V>;
abstract sortValues(comparator: Comparator<V>): SortedMapList;
abstract filterValues(filter: Filter<K, V>): FilteredMap<K, V>;
abstract [Symbol.iterator](): Iterator<[K, V]>;
abstract get size(): number;

View File

@ -16,7 +16,7 @@ limitations under the License.
import {BaseObservableMap, BaseObservableMapConfig} from "./BaseObservableMap";
import {SubscriptionHandle} from "../BaseObservable";
import {config} from "./config";
import {config, Mapper, Updater, Comparator, Filter} from "./config";
import {JoinedMap} from "./JoinedMap.js";
import {MappedMap} from "./MappedMap.js";
import {SortedMapList} from "../list/SortedMapList.js";
@ -24,11 +24,11 @@ import {SortedMapList} from "../list/SortedMapList.js";
export class FilteredMap<K, V> extends BaseObservableMap<K, V> {
private _source: BaseObservableMap<K, V>;
private _config: BaseObservableMapConfig<K, V>;
private _filter: (value: V, key: K) => boolean;
private _filter: Filter<K, V>;
private _included?: Map<K, boolean>;
private _subscription?: SubscriptionHandle;
constructor(source: BaseObservableMap<K, V>, filter: (value: V, key: K) => boolean) {
constructor(source: BaseObservableMap<K, V>, filter: Filter<K, V>) {
super();
this._source = source;
this._filter = filter;
@ -39,19 +39,19 @@ export class FilteredMap<K, V> extends BaseObservableMap<K, V> {
return this._config.join(this, ...otherMaps);
}
mapValues(mapper: any, updater?: (params: any) => void): MappedMap<K, V>{
mapValues(mapper: Mapper<V>, updater?: Updater<V>): MappedMap<K, V>{
return this._config.mapValues(this, mapper, updater);
}
sortValues(comparator: (a: V, b: V) => number): SortedMapList {
sortValues(comparator: Comparator<V>): SortedMapList {
return this._config.sortValues(this, comparator);
}
filterValues(filter: (v: V, k: K) => boolean): FilteredMap<K, V> {
filterValues(filter: Filter<K, V>): FilteredMap<K, V> {
return this._config.filterValues(this, filter);
}
setFilter(filter: (value: V, key: K) => boolean) {
setFilter(filter: Filter<K, V>) {
this._filter = filter;
if (this._subscription) {
this._reapplyFilter();

View File

@ -15,7 +15,7 @@ limitations under the License.
*/
import {BaseObservableMap, BaseObservableMapConfig} from "./BaseObservableMap";
import {config} from "./config";
import {config, Mapper, Updater, Comparator, Filter} from "./config";
import {FilteredMap} from "./FilteredMap.js";
import {MappedMap} from "./MappedMap.js";
import {SortedMapList} from "../list/SortedMapList.js";
@ -37,15 +37,15 @@ export class JoinedMap<K, V> extends BaseObservableMap<K, V> {
return this._config.join(this, ...otherMaps);
}
mapValues(mapper: any, updater?: (params: any) => void): MappedMap<K, V> {
mapValues(mapper: Mapper<V>, updater?: Updater<V>): MappedMap<K, V> {
return this._config.mapValues(this, mapper, updater);
}
sortValues(comparator: (a: V, b: V) => number): SortedMapList {
sortValues(comparator: Comparator<V>): SortedMapList {
return this._config.sortValues(this, comparator);
}
filterValues(filter: (v: V, k: K) => boolean): FilteredMap<K, V> {
filterValues(filter: Filter<K, V>): FilteredMap<K, V> {
return this._config.filterValues(this, filter);
}

View File

@ -15,7 +15,7 @@ limitations under the License.
*/
import {BaseObservableMap, BaseObservableMapConfig} from "./BaseObservableMap";
import {config} from "./config";
import {config, Mapper, Updater, Comparator, Filter} from "./config";
import {FilteredMap} from "./FilteredMap.js";
import {MappedMap} from "./MappedMap.js";
import {JoinedMap} from "./JoinedMap.js";
@ -42,15 +42,15 @@ export class LogMap<K, V> extends BaseObservableMap<K, V> {
return this._config.join(this, ...otherMaps);
}
mapValues(mapper: any, updater?: (params: any) => void): MappedMap<K, V> {
mapValues(mapper: Mapper<V>, updater?: Updater<V>): MappedMap<K, V> {
return this._config.mapValues(this, mapper, updater);
}
sortValues(comparator: (a: V, b: V) => number): SortedMapList {
sortValues(comparator: Comparator<V>): SortedMapList {
return this._config.sortValues(this, comparator);
}
filterValues(filter: (v: V, k: K) => boolean): FilteredMap<K, V> {
filterValues(filter: Filter<K, V>): FilteredMap<K, V> {
return this._config.filterValues(this, filter);
}

View File

@ -15,7 +15,7 @@ limitations under the License.
*/
import {BaseObservableMap, BaseObservableMapConfig} from "./BaseObservableMap";
import {config} from "./config";
import {config, Mapper, Updater, Comparator, Filter} from "./config";
import {JoinedMap} from "./JoinedMap.js";
import {FilteredMap} from "./FilteredMap.js";
import {SortedMapList} from "../list/SortedMapList.js";
@ -50,15 +50,15 @@ export class MappedMap<K, V> extends BaseObservableMap<K, V> {
return this._config.join(this, ...otherMaps);
}
mapValues(mapper: any, updater?: (params: any) => void): MappedMap<K, V>{
mapValues(mapper: Mapper<V>, updater?: Updater<V>): MappedMap<K, V>{
return this._config.mapValues(this, mapper, updater);
}
sortValues(comparator: (a: V, b: V) => number): SortedMapList {
sortValues(comparator: Comparator<V>): SortedMapList {
return this._config.sortValues(this, comparator);
}
filterValues(filter: (v: V, k: K) => boolean): FilteredMap<K, V> {
filterValues(filter: Filter<K, V>): FilteredMap<K, V> {
return this._config.filterValues(this, filter);
}
@ -128,11 +128,4 @@ export class MappedMap<K, V> extends BaseObservableMap<K, V> {
get(key: K): V | undefined {
return this._mappedValues.get(key);
}
}
type Mapper<V> = (
value: V,
emitSpontaneousUpdate: any,
) => V;
type Updater<V> = (params: any, mappedValue?: V, value?: V) => void;
}

View File

@ -15,7 +15,7 @@ limitations under the License.
*/
import {BaseObservableMap, BaseObservableMapConfig} from "./BaseObservableMap";
import {config} from "./config";
import {config, Mapper, Updater, Comparator, Filter} from "./config";
import {JoinedMap} from "./JoinedMap.js";
import {MappedMap} from "./MappedMap.js";
import {FilteredMap} from "./FilteredMap.js";
@ -36,15 +36,15 @@ export class ObservableMap<K, V> extends BaseObservableMap<K, V> {
return this._config.join(this, ...otherMaps);
}
mapValues(mapper: any, updater?: (params: any) => void): MappedMap<K, V> {
mapValues(mapper: Mapper<V>, updater?: Updater<V>): MappedMap<K, V> {
return this._config.mapValues(this, mapper, updater);
}
sortValues(comparator: (a: V, b: V) => number): SortedMapList {
sortValues(comparator: Comparator<V>): SortedMapList {
return this._config.sortValues(this, comparator);
}
filterValues(filter: (v: V, k: K) => boolean): FilteredMap<K, V> {
filterValues(filter: Filter<K, V>): FilteredMap<K, V> {
return this._config.filterValues(this, filter);
}

View File

@ -30,14 +30,25 @@ export function config<K, V>(): BaseObservableMapConfig<K, V> {
join: (_this: BaseObservableMap<K, V>, ...otherMaps: Array<BaseObservableMap<K, V>>): JoinedMap<K, V> => {
return new JoinedMap([_this].concat(otherMaps));
},
mapValues: (_this: BaseObservableMap<K, V>, mapper: any, updater: (params: any) => void): MappedMap<K, V> => {
mapValues: (_this: BaseObservableMap<K, V>, mapper: Mapper<V>, updater?: Updater<V>): MappedMap<K, V> => {
return new MappedMap(_this, mapper, updater);
},
sortValues: (_this: BaseObservableMap<K, V>, comparator: (a: V, b: V) => number): SortedMapList => {
sortValues: (_this: BaseObservableMap<K, V>, comparator: Comparator<V>): SortedMapList => {
return new SortedMapList(_this, comparator);
},
filterValues: (_this: BaseObservableMap<K, V>, filter: (v: V, k: K) => boolean): FilteredMap<K, V> => {
filterValues: (_this: BaseObservableMap<K, V>, filter: Filter<K, V>): FilteredMap<K, V> => {
return new FilteredMap(_this, filter);
}
};
}
}
export type Mapper<V> = (
value: V,
emitSpontaneousUpdate: any,
) => V;
export type Updater<V> = (params: any, mappedValue?: V, value?: V) => void;
export type Comparator<V> = (a: V, b: V) => number;
export type Filter<K, V> = (v: V, k: K) => boolean;