From 73b83fdab8f055600ee043f1e57415b0326b895b Mon Sep 17 00:00:00 2001 From: Isaiah Becker-Mayer Date: Sun, 10 Jul 2022 00:21:30 -0400 Subject: [PATCH] Changes config from a function that returns an objectinto a more aptly named BaseObservableMapDefaults class --- src/observable/map/ApplyMap.ts | 16 +++--- src/observable/map/BaseObservableMap.ts | 18 ++---- .../map/BaseObservableMapDefaults.ts | 55 +++++++++++++++++++ src/observable/map/FilteredMap.ts | 15 +++-- src/observable/map/JoinedMap.ts | 15 +++-- src/observable/map/LogMap.ts | 16 +++--- src/observable/map/MappedMap.ts | 16 +++--- src/observable/map/ObservableMap.ts | 15 +++-- src/observable/map/config.ts | 54 ------------------ 9 files changed, 105 insertions(+), 115 deletions(-) create mode 100644 src/observable/map/BaseObservableMapDefaults.ts delete mode 100644 src/observable/map/config.ts diff --git a/src/observable/map/ApplyMap.ts b/src/observable/map/ApplyMap.ts index 0fef91c9..f8d09914 100644 --- a/src/observable/map/ApplyMap.ts +++ b/src/observable/map/ApplyMap.ts @@ -14,9 +14,9 @@ See the License for the specific language governing permissions and limitations under the License. */ -import {BaseObservableMap, BaseObservableMapConfig} from "./BaseObservableMap"; +import {BaseObservableMap} from "./BaseObservableMap"; import {SubscriptionHandle} from "../BaseObservable"; -import {config, Mapper, Updater, Comparator, Filter} from "./config"; +import {BaseObservableMapDefaults, Mapper, Updater, Comparator, Filter} from "./BaseObservableMapDefaults"; import {JoinedMap} from "./JoinedMap"; import {MappedMap} from "./MappedMap"; import {FilteredMap} from "./FilteredMap"; @@ -24,16 +24,16 @@ import {SortedMapList} from "../list/SortedMapList.js"; export class ApplyMap extends BaseObservableMap { + private _defaults = new BaseObservableMapDefaults(); 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._config = config(); } hasApply(): boolean { @@ -105,19 +105,19 @@ export class ApplyMap extends BaseObservableMap { } join(...otherMaps: Array): JoinedMap { - return this._config.join(this, ...otherMaps); + return this._defaults.join(this, ...otherMaps); } mapValues(mapper: Mapper, updater?: Updater): MappedMap { - return this._config.mapValues(this, mapper, updater); + return this._defaults.mapValues(this, mapper, updater); } sortValues(comparator: Comparator): SortedMapList { - return this._config.sortValues(this, comparator); + return this._defaults.sortValues(this, comparator); } filterValues(filter: Filter): FilteredMap { - return this._config.filterValues(this, filter); + return this._defaults.filterValues(this, filter); } } diff --git a/src/observable/map/BaseObservableMap.ts b/src/observable/map/BaseObservableMap.ts index 98781955..02dedce5 100644 --- a/src/observable/map/BaseObservableMap.ts +++ b/src/observable/map/BaseObservableMap.ts @@ -19,7 +19,7 @@ import {JoinedMap} from "../map/JoinedMap"; import {MappedMap} from "../map/MappedMap"; import {FilteredMap} from "../map/FilteredMap"; import {SortedMapList} from "../list/SortedMapList.js"; -import {Mapper, Updater, Comparator, Filter} from "./config"; +import {Mapper, Updater, Comparator, Filter} from "./BaseObservableMapDefaults"; export interface IMapObserver { onReset(): void; @@ -28,13 +28,6 @@ export interface IMapObserver { onRemove(key: K, value: V): void } -export type BaseObservableMapConfig = { - join(_this: BaseObservableMap, ...otherMaps: Array>): JoinedMap; - mapValues(_this: BaseObservableMap, mapper: any, updater?: Updater): MappedMap; - sortValues(_this: BaseObservableMap, comparator: Comparator): SortedMapList; - filterValues(_this: BaseObservableMap, filter: Filter): FilteredMap; -} - export abstract class BaseObservableMap extends BaseObservable> { emitReset(): void { for(let h of this._handlers) { @@ -62,10 +55,11 @@ export abstract class BaseObservableMap extends BaseObservable): JoinedMap; abstract mapValues(mapper: Mapper, updater?: Updater): MappedMap; diff --git a/src/observable/map/BaseObservableMapDefaults.ts b/src/observable/map/BaseObservableMapDefaults.ts new file mode 100644 index 00000000..46d9148e --- /dev/null +++ b/src/observable/map/BaseObservableMapDefaults.ts @@ -0,0 +1,55 @@ +/* +Copyright 2022 Isaiah Becker-Mayer + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +import {BaseObservableMap} from "./BaseObservableMap"; +import {FilteredMap} from "./FilteredMap"; +import {MappedMap} from "./MappedMap"; +import {JoinedMap} from "./JoinedMap"; +import {SortedMapList} from "../list/SortedMapList.js"; + + +// This class is used as a default implementation of +// the respective abstract functions in BaseObservableMap. +// It is kept as its own class in its own file in order to avoid a circular +// dependency between the classes that extend BaseObservableMap which are +// instantiated here (i.e. `new JoinedMap()`). +export class BaseObservableMapDefaults { + join(_this: BaseObservableMap, ...otherMaps: Array>): JoinedMap { + return new JoinedMap([_this].concat(otherMaps)); + } + + mapValues(_this: BaseObservableMap, mapper: Mapper, updater?: Updater): MappedMap { + return new MappedMap(_this, mapper, updater); + } + + sortValues(_this: BaseObservableMap, comparator: Comparator): SortedMapList { + return new SortedMapList(_this, comparator); + } + + filterValues(_this: BaseObservableMap, filter: Filter): FilteredMap { + return new FilteredMap(_this, filter); + } +} + +export type Mapper = ( + value: V, + emitSpontaneousUpdate: any, +) => V; + +export type Updater = (params: any, mappedValue?: V, value?: V) => void; + +export type Comparator = (a: V, b: V) => number; + +export type Filter = (v: V, k: K) => boolean; \ No newline at end of file diff --git a/src/observable/map/FilteredMap.ts b/src/observable/map/FilteredMap.ts index 00006a6d..b0ed2e82 100644 --- a/src/observable/map/FilteredMap.ts +++ b/src/observable/map/FilteredMap.ts @@ -14,16 +14,16 @@ See the License for the specific language governing permissions and limitations under the License. */ -import {BaseObservableMap, BaseObservableMapConfig} from "./BaseObservableMap"; +import {BaseObservableMap} from "./BaseObservableMap"; import {SubscriptionHandle} from "../BaseObservable"; -import {config, Mapper, Updater, Comparator, Filter} from "./config"; +import {BaseObservableMapDefaults, Mapper, Updater, Comparator, Filter} from "./BaseObservableMapDefaults"; import {JoinedMap} from "./JoinedMap"; import {MappedMap} from "./MappedMap"; import {SortedMapList} from "../list/SortedMapList.js"; export class FilteredMap extends BaseObservableMap { + private _defaults = new BaseObservableMapDefaults(); private _source: BaseObservableMap; - private _config: BaseObservableMapConfig; private _filter: Filter; private _included?: Map; private _subscription?: SubscriptionHandle; @@ -32,7 +32,6 @@ export class FilteredMap extends BaseObservableMap { super(); this._source = source; this._filter = filter; - this._config = config(); } setFilter(filter: Filter): void { @@ -165,19 +164,19 @@ export class FilteredMap extends BaseObservableMap { } join(...otherMaps: Array): JoinedMap { - return this._config.join(this, ...otherMaps); + return this._defaults.join(this, ...otherMaps); } mapValues(mapper: Mapper, updater?: Updater): MappedMap{ - return this._config.mapValues(this, mapper, updater); + return this._defaults.mapValues(this, mapper, updater); } sortValues(comparator: Comparator): SortedMapList { - return this._config.sortValues(this, comparator); + return this._defaults.sortValues(this, comparator); } filterValues(filter: Filter): FilteredMap { - return this._config.filterValues(this, filter); + return this._defaults.filterValues(this, filter); } } diff --git a/src/observable/map/JoinedMap.ts b/src/observable/map/JoinedMap.ts index bf9f8b97..d4164f79 100644 --- a/src/observable/map/JoinedMap.ts +++ b/src/observable/map/JoinedMap.ts @@ -14,8 +14,8 @@ See the License for the specific language governing permissions and limitations under the License. */ -import {BaseObservableMap, BaseObservableMapConfig} from "./BaseObservableMap"; -import {config, Mapper, Updater, Comparator, Filter} from "./config"; +import {BaseObservableMap} from "./BaseObservableMap"; +import {BaseObservableMapDefaults, Mapper, Updater, Comparator, Filter} from "./BaseObservableMapDefaults"; import {FilteredMap} from "./FilteredMap"; import {MappedMap} from "./MappedMap"; import {SortedMapList} from "../list/SortedMapList.js"; @@ -23,14 +23,13 @@ import {SubscriptionHandle} from "../BaseObservable"; export class JoinedMap extends BaseObservableMap { + private _defaults = new BaseObservableMapDefaults(); protected _sources: BaseObservableMap[]; - private _config: BaseObservableMapConfig; private _subscriptions?: SourceSubscriptionHandler[]; constructor(sources: BaseObservableMap[]) { super(); this._sources = sources; - this._config = config(); } onAdd(source: BaseObservableMap, key: K, value: V): void { @@ -136,19 +135,19 @@ export class JoinedMap extends BaseObservableMap { } join(...otherMaps: Array): JoinedMap { - return this._config.join(this, ...otherMaps); + return this._defaults.join(this, ...otherMaps); } mapValues(mapper: Mapper, updater?: Updater): MappedMap { - return this._config.mapValues(this, mapper, updater); + return this._defaults.mapValues(this, mapper, updater); } sortValues(comparator: Comparator): SortedMapList { - return this._config.sortValues(this, comparator); + return this._defaults.sortValues(this, comparator); } filterValues(filter: Filter): FilteredMap { - return this._config.filterValues(this, filter); + return this._defaults.filterValues(this, filter); } } diff --git a/src/observable/map/LogMap.ts b/src/observable/map/LogMap.ts index 70fe623a..7b857678 100644 --- a/src/observable/map/LogMap.ts +++ b/src/observable/map/LogMap.ts @@ -14,8 +14,8 @@ See the License for the specific language governing permissions and limitations under the License. */ -import {BaseObservableMap, BaseObservableMapConfig} from "./BaseObservableMap"; -import {config, Mapper, Updater, Comparator, Filter} from "./config"; +import {BaseObservableMap} from "./BaseObservableMap"; +import {BaseObservableMapDefaults, Mapper, Updater, Comparator, Filter} from "./BaseObservableMapDefaults"; import {FilteredMap} from "./FilteredMap"; import {MappedMap} from "./MappedMap"; import {JoinedMap} from "./JoinedMap"; @@ -25,17 +25,15 @@ import {ILogItem, LabelOrValues} from "../../logging/types"; import {LogLevel} from "../../logging/LogFilter"; export class LogMap extends BaseObservableMap { + private _defaults = new BaseObservableMapDefaults(); private _source: BaseObservableMap; private _subscription?: SubscriptionHandle; private _log: ILogItem; - private _config: BaseObservableMapConfig - constructor(source: BaseObservableMap, log: ILogItem) { super(); this._source = source; this._log = log; - this._config = config(); } private log(labelOrValues: LabelOrValues, logLevel?: LogLevel): ILogItem { @@ -88,19 +86,19 @@ export class LogMap extends BaseObservableMap { } join(...otherMaps: Array): JoinedMap { - return this._config.join(this, ...otherMaps); + return this._defaults.join(this, ...otherMaps); } mapValues(mapper: Mapper, updater?: Updater): MappedMap { - return this._config.mapValues(this, mapper, updater); + return this._defaults.mapValues(this, mapper, updater); } sortValues(comparator: Comparator): SortedMapList { - return this._config.sortValues(this, comparator); + return this._defaults.sortValues(this, comparator); } filterValues(filter: Filter): FilteredMap { - return this._config.filterValues(this, filter); + return this._defaults.filterValues(this, filter); } } diff --git a/src/observable/map/MappedMap.ts b/src/observable/map/MappedMap.ts index 95f49c2b..3d9e3a3c 100644 --- a/src/observable/map/MappedMap.ts +++ b/src/observable/map/MappedMap.ts @@ -14,8 +14,8 @@ See the License for the specific language governing permissions and limitations under the License. */ -import {BaseObservableMap, BaseObservableMapConfig} from "./BaseObservableMap"; -import {config, Mapper, Updater, Comparator, Filter} from "./config"; +import {BaseObservableMap} from "./BaseObservableMap"; +import {BaseObservableMapDefaults, Mapper, Updater, Comparator, Filter} from "./BaseObservableMapDefaults"; import {JoinedMap} from "./JoinedMap"; import {FilteredMap} from "./FilteredMap"; import {SortedMapList} from "../list/SortedMapList.js"; @@ -26,12 +26,13 @@ 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? */ export class MappedMap extends BaseObservableMap { + private _defaults = new BaseObservableMapDefaults(); private _source: BaseObservableMap; private _mapper: Mapper; private _updater?: Updater; private _mappedValues: Map; private _subscription?: SubscriptionHandle; - private _config: BaseObservableMapConfig + constructor( source: BaseObservableMap, @@ -43,7 +44,6 @@ export class MappedMap extends BaseObservableMap { this._mapper = mapper; this._updater = updater; this._mappedValues = new Map(); - this._config = config(); } _emitSpontaneousUpdate(key: K, params: any): void { @@ -115,18 +115,18 @@ export class MappedMap extends BaseObservableMap { } join(...otherMaps: Array): JoinedMap { - return this._config.join(this, ...otherMaps); + return this._defaults.join(this, ...otherMaps); } mapValues(mapper: Mapper, updater?: Updater): MappedMap{ - return this._config.mapValues(this, mapper, updater); + return this._defaults.mapValues(this, mapper, updater); } sortValues(comparator: Comparator): SortedMapList { - return this._config.sortValues(this, comparator); + return this._defaults.sortValues(this, comparator); } filterValues(filter: Filter): FilteredMap { - return this._config.filterValues(this, filter); + return this._defaults.filterValues(this, filter); } } \ No newline at end of file diff --git a/src/observable/map/ObservableMap.ts b/src/observable/map/ObservableMap.ts index 950fb818..4b98c089 100644 --- a/src/observable/map/ObservableMap.ts +++ b/src/observable/map/ObservableMap.ts @@ -14,8 +14,8 @@ See the License for the specific language governing permissions and limitations under the License. */ -import {BaseObservableMap, BaseObservableMapConfig} from "./BaseObservableMap"; -import {config, Mapper, Updater, Comparator, Filter} from "./config"; +import {BaseObservableMap} from "./BaseObservableMap"; +import {BaseObservableMapDefaults, Mapper, Updater, Comparator, Filter} from "./BaseObservableMapDefaults"; import {JoinedMap} from "./JoinedMap"; import {MappedMap} from "./MappedMap"; import {FilteredMap} from "./FilteredMap"; @@ -23,12 +23,11 @@ import {SortedMapList} from "../list/SortedMapList.js"; export class ObservableMap extends BaseObservableMap { - private _config: BaseObservableMapConfig + private _defaults = new BaseObservableMapDefaults(); private readonly _values: Map; constructor(initialValues?: (readonly [K, V])[]) { super(); - this._config = config(); this._values = new Map(initialValues); } @@ -101,19 +100,19 @@ export class ObservableMap extends BaseObservableMap { } join(...otherMaps: Array): JoinedMap { - return this._config.join(this, ...otherMaps); + return this._defaults.join(this, ...otherMaps); } mapValues(mapper: Mapper, updater?: Updater): MappedMap { - return this._config.mapValues(this, mapper, updater); + return this._defaults.mapValues(this, mapper, updater); } sortValues(comparator: Comparator): SortedMapList { - return this._config.sortValues(this, comparator); + return this._defaults.sortValues(this, comparator); } filterValues(filter: Filter): FilteredMap { - return this._config.filterValues(this, filter); + return this._defaults.filterValues(this, filter); } }; diff --git a/src/observable/map/config.ts b/src/observable/map/config.ts deleted file mode 100644 index fab0bcf8..00000000 --- a/src/observable/map/config.ts +++ /dev/null @@ -1,54 +0,0 @@ -/* -Copyright 2022 Isaiah Becker-Mayer - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ -import {BaseObservableMap, BaseObservableMapConfig} from "./BaseObservableMap"; -import {FilteredMap} from "./FilteredMap"; -import {MappedMap} from "./MappedMap"; -import {JoinedMap} from "./JoinedMap"; -import {SortedMapList} from "../list/SortedMapList.js"; - - -// This function is used as a default implementation of -// the respective abstract functions in BaseObservableMap. -// We implement it this way in order to avoid a circular -// dependency between the classes that are instantiated here -// (i.e. `new JoinedMap()`) and BaseObservableMap (as they extend it). -export function config(): BaseObservableMapConfig { - return { - join: (_this: BaseObservableMap, ...otherMaps: Array>): JoinedMap => { - return new JoinedMap([_this].concat(otherMaps)); - }, - mapValues: (_this: BaseObservableMap, mapper: Mapper, updater?: Updater): MappedMap => { - return new MappedMap(_this, mapper, updater); - }, - sortValues: (_this: BaseObservableMap, comparator: Comparator): SortedMapList => { - return new SortedMapList(_this, comparator); - }, - filterValues: (_this: BaseObservableMap, filter: Filter): FilteredMap => { - return new FilteredMap(_this, filter); - } - }; -} - -export type Mapper = ( - value: V, - emitSpontaneousUpdate: any, -) => V; - -export type Updater = (params: any, mappedValue?: V, value?: V) => void; - -export type Comparator = (a: V, b: V) => number; - -export type Filter = (v: V, k: K) => boolean; \ No newline at end of file