From ab6a8ad3aac1ba9493119ab00c824f48da745c0e Mon Sep 17 00:00:00 2001 From: Isaiah Becker-Mayer Date: Sat, 9 Jul 2022 10:16:46 -0400 Subject: [PATCH] typescriptifying ApplyMap --- .../session/leftpanel/LeftPanelViewModel.js | 4 +- src/observable/index.ts | 29 +--------- .../map/{ApplyMap.js => ApplyMap.ts} | 54 ++++++++++++++----- src/observable/map/FilteredMap.ts | 2 +- 4 files changed, 47 insertions(+), 42 deletions(-) rename src/observable/map/{ApplyMap.js => ApplyMap.ts} (52%) diff --git a/src/domain/session/leftpanel/LeftPanelViewModel.js b/src/domain/session/leftpanel/LeftPanelViewModel.js index 8c8d71a2..8e814151 100644 --- a/src/domain/session/leftpanel/LeftPanelViewModel.js +++ b/src/domain/session/leftpanel/LeftPanelViewModel.js @@ -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) { diff --git a/src/observable/index.ts b/src/observable/index.ts index 47ab84ca..dfd272fd 100644 --- a/src/observable/index.ts +++ b/src/observable/index.ts @@ -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)); - } -}); \ No newline at end of file +export { ConcatList } from "./list/ConcatList"; \ No newline at end of file diff --git a/src/observable/map/ApplyMap.js b/src/observable/map/ApplyMap.ts similarity index 52% rename from src/observable/map/ApplyMap.js rename to src/observable/map/ApplyMap.ts index 6be7278a..da485af9 100644 --- a/src/observable/map/ApplyMap.js +++ b/src/observable/map/ApplyMap.ts @@ -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 extends BaseObservableMap { + private _source: BaseObservableMap; + private _subscription?: SubscriptionHandle; + private _apply?: Apply; + private _config: BaseObservableMapConfig; + + constructor(source: BaseObservableMap, apply?: Apply) { super(); this._source = source; this._apply = apply; - this._subscription = null; + this._config = config(); + } + + join(...otherMaps: Array): JoinedMap { + return this._config.join(this, ...otherMaps); + } + + mapValues(mapper: any, updater?: (params: any) => void): MappedMap { + 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 { + return this._config.filterValues(this, filter); } hasApply() { return !!this._apply; } - setApply(apply) { + setApply(apply?: Apply) { this._apply = apply; - if (apply) { + if (this._apply) { this.applyOnce(this._apply); } } - applyOnce(apply) { + applyOnce(apply: Apply) { 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 = (key: K, value: V, params?: any) => void; \ No newline at end of file diff --git a/src/observable/map/FilteredMap.ts b/src/observable/map/FilteredMap.ts index a9603d6d..8b544072 100644 --- a/src/observable/map/FilteredMap.ts +++ b/src/observable/map/FilteredMap.ts @@ -23,7 +23,7 @@ import {SortedMapList} from "../list/SortedMapList.js"; export class FilteredMap extends BaseObservableMap { private _source: BaseObservableMap; - private _config: BaseObservableMapConfig + private _config: BaseObservableMapConfig; private _filter: (value: V, key: K) => boolean; private _included?: Map; private _subscription?: SubscriptionHandle;