mirror of
https://github.com/vector-im/hydrogen-web.git
synced 2024-12-23 03:25:12 +01:00
moves config into its own file
This commit is contained in:
parent
b33db1df36
commit
7645eb8753
@ -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<K, V>(): BaseObservableMapConfig<K, V> {
|
||||
return {
|
||||
join: (_this: BaseObservableMap<K, V>, ...otherMaps: Array<BaseObservableMap<K, V>>): JoinedMap => {
|
||||
return new JoinedMap([_this].concat(otherMaps));
|
||||
},
|
||||
mapValues: (_this: BaseObservableMap<K, V>, mapper: any, updater?: (params: any) => void): MappedMap => {
|
||||
return new MappedMap(_this, mapper, updater);
|
||||
},
|
||||
sortValues: (_this: BaseObservableMap<K, V>, comparator?: (a: any, b: any) => number): SortedMapList => {
|
||||
return new SortedMapList(_this, comparator);
|
||||
},
|
||||
filterValues: (_this: BaseObservableMap<K, V>, filter: (v: V, k: K) => boolean): FilteredMap => {
|
||||
return new FilteredMap(_this, filter);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
export class ObservableMap<K, V> extends ObservableMapInternal<K, V> {
|
||||
constructor(initialValues?: (readonly [K, V])[]) {
|
||||
super(config<K, V>(), initialValues);
|
||||
}
|
||||
}
|
||||
|
||||
// avoid circular dependency between these classes
|
||||
// and BaseObservableMap (as they extend it)
|
||||
|
@ -60,10 +60,17 @@ export abstract class BaseObservableMap<K, V> extends BaseObservable<IMapObserve
|
||||
}
|
||||
}
|
||||
|
||||
// The following group of functions have a default implementation
|
||||
// in the neighboring `config.ts`. See the comment in that file for
|
||||
// the explanation for why the default implementation isn't defined
|
||||
// here. See the neighboring `ObservableMap.ts` for an example of how
|
||||
// to easily use the default implementation in a class that extends
|
||||
// this one (which is most likely what you want to do).
|
||||
abstract join(...otherMaps: Array<typeof this>): 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;
|
||||
|
@ -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<K, V> extends BaseObservableMap<K, V> {
|
||||
|
||||
export class ObservableMap<K, V> extends BaseObservableMap<K, V> {
|
||||
private _config: BaseObservableMapConfig<K, V>
|
||||
private readonly _values: Map<K, V>;
|
||||
|
||||
constructor(config: BaseObservableMapConfig<K, V>, initialValues?: (readonly [K, V])[]) {
|
||||
constructor(initialValues?: (readonly [K, V])[]) {
|
||||
super();
|
||||
this._config = config;
|
||||
this._config = config<K, V>();
|
||||
this._values = new Map(initialValues);
|
||||
}
|
||||
|
||||
|
43
src/observable/map/config.ts
Normal file
43
src/observable/map/config.ts
Normal file
@ -0,0 +1,43 @@
|
||||
/*
|
||||
Copyright 2022 Isaiah Becker-Mayer <ibeckermayer@gmail.com>
|
||||
|
||||
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<K, V>(): BaseObservableMapConfig<K, V> {
|
||||
return {
|
||||
join: (_this: BaseObservableMap<K, V>, ...otherMaps: Array<BaseObservableMap<K, V>>): JoinedMap => {
|
||||
return new JoinedMap([_this].concat(otherMaps));
|
||||
},
|
||||
mapValues: (_this: BaseObservableMap<K, V>, mapper: any, updater?: (params: any) => void): MappedMap => {
|
||||
return new MappedMap(_this, mapper, updater);
|
||||
},
|
||||
sortValues: (_this: BaseObservableMap<K, V>, comparator?: (a: any, b: any) => number): SortedMapList => {
|
||||
return new SortedMapList(_this, comparator);
|
||||
},
|
||||
filterValues: (_this: BaseObservableMap<K, V>, filter: (v: V, k: K) => boolean): FilteredMap => {
|
||||
return new FilteredMap(_this, filter);
|
||||
}
|
||||
};
|
||||
};
|
Loading…
Reference in New Issue
Block a user