From 7645eb875374c33593ba2f85a7891061ac78e503 Mon Sep 17 00:00:00 2001 From: Isaiah Becker-Mayer Date: Mon, 4 Jul 2022 19:47:43 -0400 Subject: [PATCH] moves config into its own file --- src/observable/index.ts | 31 ++---------------- src/observable/map/BaseObservableMap.ts | 7 ++++ src/observable/map/ObservableMap.ts | 14 ++++---- src/observable/map/config.ts | 43 +++++++++++++++++++++++++ 4 files changed, 61 insertions(+), 34 deletions(-) create mode 100644 src/observable/map/config.ts diff --git a/src/observable/index.ts b/src/observable/index.ts index 1b05f798..47ab84ca 100644 --- a/src/observable/index.ts +++ b/src/observable/index.ts @@ -18,40 +18,15 @@ 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, BaseObservableMapConfig} from "./map/BaseObservableMap"; -import {ObservableMapInternal} from "./map/ObservableMap"; -// re-export "root" (of chain) collections +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) -function config(): BaseObservableMapConfig { - return { - join: (_this: BaseObservableMap, ...otherMaps: Array>): JoinedMap => { - return new JoinedMap([_this].concat(otherMaps)); - }, - mapValues: (_this: BaseObservableMap, mapper: any, updater?: (params: any) => void): MappedMap => { - return new MappedMap(_this, mapper, updater); - }, - sortValues: (_this: BaseObservableMap, comparator?: (a: any, b: any) => number): SortedMapList => { - return new SortedMapList(_this, comparator); - }, - filterValues: (_this: BaseObservableMap, filter: (v: V, k: K) => boolean): FilteredMap => { - return new FilteredMap(_this, filter); - } - }; -}; - - -export class ObservableMap extends ObservableMapInternal { - constructor(initialValues?: (readonly [K, V])[]) { - super(config(), initialValues); - } -} // avoid circular dependency between these classes // and BaseObservableMap (as they extend it) diff --git a/src/observable/map/BaseObservableMap.ts b/src/observable/map/BaseObservableMap.ts index 39189cdc..5eee5f95 100644 --- a/src/observable/map/BaseObservableMap.ts +++ b/src/observable/map/BaseObservableMap.ts @@ -60,10 +60,17 @@ export abstract class BaseObservableMap extends BaseObservable): JoinedMap; abstract mapValues(mapper: any, updater?: (params: any) => void): MappedMap; abstract sortValues(comparator?: (a: any, b: any) => number): SortedMapList; abstract filterValues(filter: (v: V, k: K) => boolean): FilteredMap; + abstract [Symbol.iterator](): Iterator<[K, V]>; abstract get size(): number; abstract get(key: K): V | undefined; diff --git a/src/observable/map/ObservableMap.ts b/src/observable/map/ObservableMap.ts index ced58df1..85e46703 100644 --- a/src/observable/map/ObservableMap.ts +++ b/src/observable/map/ObservableMap.ts @@ -15,18 +15,20 @@ limitations under the License. */ import {BaseObservableMap, BaseObservableMapConfig} from "./BaseObservableMap"; -import {JoinedMap} from "../map/JoinedMap.js"; -import {MappedMap} from "../map/MappedMap.js"; -import {FilteredMap} from "../map/FilteredMap.js"; +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 ObservableMapInternal extends BaseObservableMap { + +export class ObservableMap extends BaseObservableMap { private _config: BaseObservableMapConfig private readonly _values: Map; - constructor(config: BaseObservableMapConfig, initialValues?: (readonly [K, V])[]) { + constructor(initialValues?: (readonly [K, V])[]) { super(); - this._config = config; + this._config = config(); this._values = new Map(initialValues); } diff --git a/src/observable/map/config.ts b/src/observable/map/config.ts new file mode 100644 index 00000000..7a7ea0c4 --- /dev/null +++ b/src/observable/map/config.ts @@ -0,0 +1,43 @@ +/* +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.js"; +import {MappedMap} from "./MappedMap.js"; +import {JoinedMap} from "./JoinedMap.js"; +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: any, updater?: (params: any) => void): MappedMap => { + return new MappedMap(_this, mapper, updater); + }, + sortValues: (_this: BaseObservableMap, comparator?: (a: any, b: any) => number): SortedMapList => { + return new SortedMapList(_this, comparator); + }, + filterValues: (_this: BaseObservableMap, filter: (v: V, k: K) => boolean): FilteredMap => { + return new FilteredMap(_this, filter); + } + }; +}; \ No newline at end of file