Support updater in MappedMap

Signed-off-by: RMidhunSuresh <rmidhunsuresh@gmail.com>
This commit is contained in:
RMidhunSuresh 2021-06-22 17:33:15 +05:30
parent 0819dcb29e
commit f41c835e91
2 changed files with 5 additions and 3 deletions

View file

@ -34,8 +34,8 @@ Object.assign(BaseObservableMap.prototype, {
return new SortedMapList(this, comparator); return new SortedMapList(this, comparator);
}, },
mapValues(mapper) { mapValues(mapper, updater) {
return new MappedMap(this, mapper); return new MappedMap(this, mapper, updater);
}, },
filterValues(filter) { filterValues(filter) {

View file

@ -20,10 +20,11 @@ so a mapped value can emit updates on it's own with this._emitSpontaneousUpdate
how should the mapped value be notified of an update though? and can it then decide to not propagate the update? how should the mapped value be notified of an update though? and can it then decide to not propagate the update?
*/ */
export class MappedMap extends BaseObservableMap { export class MappedMap extends BaseObservableMap {
constructor(source, mapper) { constructor(source, mapper, updater) {
super(); super();
this._source = source; this._source = source;
this._mapper = mapper; this._mapper = mapper;
this._updater = updater;
this._mappedValues = new Map(); this._mappedValues = new Map();
} }
@ -55,6 +56,7 @@ export class MappedMap extends BaseObservableMap {
} }
const mappedValue = this._mappedValues.get(key); const mappedValue = this._mappedValues.get(key);
if (mappedValue !== undefined) { if (mappedValue !== undefined) {
this._updater?.(mappedValue, params, value);
// TODO: map params somehow if needed? // TODO: map params somehow if needed?
this.emitUpdate(key, mappedValue, params); this.emitUpdate(key, mappedValue, params);
} }