Fixes MappedMap type system

This commit is contained in:
Isaiah Becker-Mayer 2022-08-19 19:02:06 -04:00
parent dd01e70b4a
commit 92ed503700
3 changed files with 14 additions and 14 deletions

View File

@ -66,7 +66,7 @@ export abstract class BaseObservableMap<K, V> extends BaseObservable<IMapObserve
return this._defaults.join(this, ...otherMaps);
}
mapValues(mapper: Mapper<V>, updater?: Updater<V>): MappedMap<K, V> {
mapValues<MappedV>(mapper: Mapper<V, MappedV>, updater?: Updater<V, MappedV>): MappedMap<K, V, MappedV> {
return this._defaults.mapValues(this, mapper, updater);
}

View File

@ -44,7 +44,7 @@ export class BaseObservableMapTransformers<K, V> {
return new JoinedMap([_this].concat(otherMaps));
}
mapValues(_this: BaseObservableMap<K, V>, mapper: Mapper<V>, updater?: Updater<V>): MappedMap<K, V> {
mapValues<MappedV>(_this: BaseObservableMap<K, V>, mapper: Mapper<V, MappedV>, updater?: Updater<V, MappedV>): MappedMap<K, V, MappedV> {
return new MappedMap(_this, mapper, updater);
}
@ -57,12 +57,12 @@ export class BaseObservableMapTransformers<K, V> {
}
}
export type Mapper<V> = (
export type Mapper<V, MappedV> = (
value: V,
emitSpontaneousUpdate: any,
) => V;
) => MappedV;
export type Updater<V> = (params: any, mappedValue?: V, value?: V) => void;
export type Updater<V, MappedV> = (params: any, mappedValue?: MappedV, value?: V) => void;
export type Comparator<V> = (a: V, b: V) => number;

View File

@ -23,24 +23,24 @@ import {SubscriptionHandle} from "../BaseObservable";
so a mapped value can emit updates on it's own with this._emitSpontaneousUpdate that is passed in the mapping function
how should the mapped value be notified of an update though? and can it then decide to not propagate the update?
*/
export class MappedMap<K, V> extends BaseObservableMap<K, V> {
export class MappedMap<K, V, MappedV> extends BaseObservableMap<K, MappedV> {
private _source: BaseObservableMap<K, V>;
private _mapper: Mapper<V>;
private _updater?: Updater<V>;
private _mappedValues: Map<K, V>;
private _mapper: Mapper<V, MappedV>;
private _updater?: Updater<V, MappedV>;
private _mappedValues: Map<K, MappedV>;
private _subscription?: SubscriptionHandle;
constructor(
source: BaseObservableMap<K, V>,
mapper: Mapper<V>,
updater?: Updater<V>
mapper: Mapper<V, MappedV>,
updater?: Updater<V, MappedV>
) {
super(new BaseObservableMapTransformers<K, V>());
super(new BaseObservableMapTransformers<K, MappedV>());
this._source = source;
this._mapper = mapper;
this._updater = updater;
this._mappedValues = new Map<K, V>();
this._mappedValues = new Map<K, MappedV>();
}
_emitSpontaneousUpdate(key: K, params: any): void {
@ -107,7 +107,7 @@ export class MappedMap<K, V> extends BaseObservableMap<K, V> {
return this._mappedValues.size;
}
get(key: K): V | undefined {
get(key: K): MappedV | undefined {
return this._mappedValues.get(key);
}
}