typescriptifying ApplyMap

This commit is contained in:
Isaiah Becker-Mayer 2022-07-09 10:16:46 -04:00
parent 95c65280ef
commit ab6a8ad3aa
4 changed files with 47 additions and 42 deletions

View File

@ -20,8 +20,8 @@ import {RoomTileViewModel} from "./RoomTileViewModel.js";
import {InviteTileViewModel} from "./InviteTileViewModel.js";
import {RoomBeingCreatedTileViewModel} from "./RoomBeingCreatedTileViewModel.js";
import {RoomFilter} from "./RoomFilter.js";
import {ApplyMap} from "../../../observable/map/ApplyMap.js";
import {addPanelIfNeeded} from "../../navigation/index";
import {ApplyMap} from "../../../observable/map/ApplyMap";
import {addPanelIfNeeded} from "../../navigation";
export class LeftPanelViewModel extends ViewModel {
constructor(options) {

View File

@ -14,36 +14,11 @@ See the License for the specific language governing permissions and
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
export { ObservableMap } from "./map/ObservableMap";
export { ObservableArray } from "./list/ObservableArray";
export { SortedArray } from "./list/SortedArray";
export { MappedList } from "./list/MappedList";
export { AsyncMappedList } from "./list/AsyncMappedList";
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));
}
});
export { ConcatList } from "./list/ConcatList";

View File

@ -14,45 +14,73 @@ See the License for the specific language governing permissions and
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();
this._source = source;
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() {
return !!this._apply;
}
setApply(apply) {
setApply(apply?: Apply<K, V>) {
this._apply = apply;
if (apply) {
if (this._apply) {
this.applyOnce(this._apply);
}
}
applyOnce(apply) {
applyOnce(apply: Apply<K, V>) {
for (const [key, value] of this._source) {
apply(key, value);
}
}
onAdd(key, value) {
onAdd(key: K, value: V) {
if (this._apply) {
this._apply(key, value);
}
this.emitAdd(key, value);
}
onRemove(key, value) {
onRemove(key: K, value: V) {
this.emitRemove(key, value);
}
onUpdate(key, value, params) {
onUpdate(key: K, value: V, params: any) {
if (this._apply) {
this._apply(key, value, params);
}
@ -69,7 +97,7 @@ export class ApplyMap extends BaseObservableMap {
onUnsubscribeLast() {
super.onUnsubscribeLast();
this._subscription = this._subscription();
if (this._subscription) this._subscription = this._subscription();
}
onReset() {
@ -87,7 +115,9 @@ export class ApplyMap extends BaseObservableMap {
return this._source.size;
}
get(key) {
get(key: K) {
return this._source.get(key);
}
}
type Apply<K, V> = (key: K, value: V, params?: any) => void;

View File

@ -23,7 +23,7 @@ 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 _config: BaseObservableMapConfig<K, V>;
private _filter: (value: V, key: K) => boolean;
private _included?: Map<K, boolean>;
private _subscription?: SubscriptionHandle;