Skip to content

Commit

Permalink
fixes import order according to https://medium.com/visual-development…
Browse files Browse the repository at this point in the history
  • Loading branch information
Isaiah Becker-Mayer committed Aug 20, 2022
1 parent 92ed503 commit 77f21f7
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 110 deletions.
2 changes: 1 addition & 1 deletion src/domain/session/leftpanel/LeftPanelViewModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ 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";
import {ApplyMap} from "../../../observable";
import {addPanelIfNeeded} from "../../navigation";

export class LeftPanelViewModel extends ViewModel {
Expand Down
3 changes: 1 addition & 2 deletions src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ export {
MappedList,
AsyncMappedList,
ConcatList,
ObservableMap
} from "./observable/index";
} from "./observable";
export {
BaseObservableValue,
ObservableValue,
Expand Down
2 changes: 1 addition & 1 deletion src/observable/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ limitations under the License.


// re-export "root" (of chain) collection
export { ObservableMap } from "./map/ObservableMap";
export { ObservableMap, ApplyMap, FilteredMap, JoinedMap, LogMap, MappedMap } from "./map";
export { ObservableArray } from "./list/ObservableArray";
export { SortedArray } from "./list/SortedArray";
export { MappedList } from "./list/MappedList";
Expand Down
10 changes: 7 additions & 3 deletions src/observable/map/ApplyMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,23 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import {BaseObservableMap} from "./BaseObservableMap";
import {BaseObservableMap} from "./index";
import {SubscriptionHandle} from "../BaseObservable";
import {BaseObservableMapTransformers} from "./BaseObservableMapTransformers";


/*
This class MUST never be imported directly from here.
Instead, it MUST be imported from index.ts. See the
top level comment in index.ts for details.
*/
export class ApplyMap<K, V> extends BaseObservableMap<K, V> {
private _source: BaseObservableMap<K, V>;
private _subscription?: SubscriptionHandle;
private _apply?: Apply<K, V>;


constructor(source: BaseObservableMap<K, V>, apply?: Apply<K, V>) {
super(new BaseObservableMapTransformers<K, V>());
super();
this._source = source;
this._apply = apply;
}
Expand Down
50 changes: 31 additions & 19 deletions src/observable/map/BaseObservableMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ limitations under the License.
*/

import {BaseObservable} from "../BaseObservable";
import {JoinedMap} from "../map/JoinedMap";
import {MappedMap} from "../map/MappedMap";
import {FilteredMap} from "../map/FilteredMap";
import {JoinedMap} from "./index";
import {MappedMap} from "./index";
import {FilteredMap} from "./index";
import {SortedMapList} from "../list/SortedMapList.js";
import type {BaseObservableMapTransformers, Mapper, Updater, Comparator, Filter} from "./BaseObservableMapTransformers";


export interface IMapObserver<K, V> {
Expand All @@ -29,12 +28,15 @@ export interface IMapObserver<K, V> {
onRemove(key: K, value: V): void
}

/*
This class MUST never be imported directly from here.
Instead, it MUST be imported from index.ts. See the
top level comment in index.ts for details.
*/
export abstract class BaseObservableMap<K, V> extends BaseObservable<IMapObserver<K, V>> {
private _defaults: BaseObservableMapTransformers<K, V>;

constructor(defaults: BaseObservableMapTransformers<K, V>) {
constructor() {
super();
this._defaults = defaults;
}

emitReset(): void {
Expand Down Expand Up @@ -63,23 +65,33 @@ export abstract class BaseObservableMap<K, V> extends BaseObservable<IMapObserve
}

join(...otherMaps: Array<typeof this>): JoinedMap<K, V> {
return this._defaults.join(this, ...otherMaps);
}
return new JoinedMap([this].concat(otherMaps));
}

mapValues<MappedV>(mapper: Mapper<V, MappedV>, updater?: Updater<V, MappedV>): MappedMap<K, V, MappedV> {
return this._defaults.mapValues(this, mapper, updater);
}
mapValues<MappedV>(mapper: Mapper<V, MappedV>, updater?: Updater<V, MappedV>): MappedMap<K, V, MappedV> {
return new MappedMap(this, mapper, updater);
}

sortValues(comparator: Comparator<V>): SortedMapList {
return this._defaults.sortValues(this, comparator);
}

filterValues(filter: Filter<K, V>): FilteredMap<K, V> {
return this._defaults.filterValues(this, filter);
}
sortValues(comparator: Comparator<V>): SortedMapList {
return new SortedMapList(this, comparator);
}

filterValues(filter: Filter<K, V>): FilteredMap<K, V> {
return new FilteredMap(this, filter);
}

abstract [Symbol.iterator](): Iterator<[K, V]>;
abstract get size(): number;
abstract get(key: K): V | undefined;
}

export type Mapper<V, MappedV> = (
value: V,
emitSpontaneousUpdate: any,
) => MappedV;

export type Updater<V, MappedV> = (params: any, mappedValue?: MappedV, value?: V) => void;

export type Comparator<V> = (a: V, b: V) => number;

export type Filter<K, V> = (v: V, k: K) => boolean;
69 changes: 0 additions & 69 deletions src/observable/map/BaseObservableMapTransformers.ts

This file was deleted.

10 changes: 7 additions & 3 deletions src/observable/map/FilteredMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,23 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import {BaseObservableMap} from "./BaseObservableMap";
import {BaseObservableMap, Filter} from "./index";
import {SubscriptionHandle} from "../BaseObservable";
import {BaseObservableMapTransformers, Filter} from "./BaseObservableMapTransformers";


/*
This class MUST never be imported directly from here.
Instead, it MUST be imported from index.ts. See the
top level comment in index.ts for details.
*/
export class FilteredMap<K, V> extends BaseObservableMap<K, V> {
private _source: BaseObservableMap<K, V>;
private _filter: Filter<K, V>;
private _included?: Map<K, boolean>;
private _subscription?: SubscriptionHandle;

constructor(source: BaseObservableMap<K, V>, filter: Filter<K, V>) {
super(new BaseObservableMapTransformers<K, V>());
super();
this._source = source;
this._filter = filter;
}
Expand Down
10 changes: 7 additions & 3 deletions src/observable/map/JoinedMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,21 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import {BaseObservableMap} from "./BaseObservableMap";
import {BaseObservableMapTransformers} from "./BaseObservableMapTransformers";
import {BaseObservableMap} from ".";
import {SubscriptionHandle} from "../BaseObservable";


/*
This class MUST never be imported directly from here.
Instead, it MUST be imported from index.ts. See the
top level comment in index.ts for details.
*/
export class JoinedMap<K, V> extends BaseObservableMap<K, V> {
protected _sources: BaseObservableMap<K, V>[];
private _subscriptions?: SourceSubscriptionHandler<K, V>[];

constructor(sources: BaseObservableMap<K, V>[]) {
super(new BaseObservableMapTransformers<K, V>());
super();
this._sources = sources;
}

Expand Down
10 changes: 7 additions & 3 deletions src/observable/map/LogMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,24 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import {BaseObservableMap} from "./BaseObservableMap";
import {BaseObservableMapTransformers} from "./BaseObservableMapTransformers";
import {BaseObservableMap} from "./index";
import {SubscriptionHandle} from "../BaseObservable";
import {ILogItem, LabelOrValues} from "../../logging/types";
import {LogLevel} from "../../logging/LogFilter";


/*
This class MUST never be imported directly from here.
Instead, it MUST be imported from index.ts. See the
top level comment in index.ts for details.
*/
export class LogMap<K, V> extends BaseObservableMap<K, V> {
private _source: BaseObservableMap<K, V>;
private _subscription?: SubscriptionHandle;
private _log: ILogItem;

constructor(source: BaseObservableMap<K, V>, log: ILogItem) {
super(new BaseObservableMapTransformers<K, V>());
super();
this._source = source;
this._log = log;
}
Expand Down
10 changes: 7 additions & 3 deletions src/observable/map/MappedMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import {BaseObservableMap} from "./BaseObservableMap";
import {BaseObservableMapTransformers, Mapper, Updater} from "./BaseObservableMapTransformers";
import {BaseObservableMap, Mapper, Updater} from "./index";
import {SubscriptionHandle} from "../BaseObservable";


/*
so a mapped value can emit updates on it's own with this._emitSpontaneousUpdate that is passed in the mapping function
how should the mapped value be notified of an update though? and can it then decide to not propagate the update?
*/
/*
This class MUST never be imported directly from here.
Instead, it MUST be imported from index.ts. See the
top level comment in index.ts for details.
*/
export class MappedMap<K, V, MappedV> extends BaseObservableMap<K, MappedV> {
private _source: BaseObservableMap<K, V>;
private _mapper: Mapper<V, MappedV>;
Expand All @@ -36,7 +40,7 @@ export class MappedMap<K, V, MappedV> extends BaseObservableMap<K, MappedV> {
mapper: Mapper<V, MappedV>,
updater?: Updater<V, MappedV>
) {
super(new BaseObservableMapTransformers<K, MappedV>());
super();
this._source = source;
this._mapper = mapper;
this._updater = updater;
Expand Down
10 changes: 7 additions & 3 deletions src/observable/map/ObservableMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import {BaseObservableMap} from "./BaseObservableMap";
import {BaseObservableMapTransformers} from "./BaseObservableMapTransformers";
import {BaseObservableMap} from "./index";


/*
This class MUST never be imported directly from here.
Instead, it MUST be imported from index.ts. See the
top level comment in index.ts for details.
*/
export class ObservableMap<K, V> extends BaseObservableMap<K, V> {
private readonly _values: Map<K, V>;

constructor(initialValues?: (readonly [K, V])[]) {
super(new BaseObservableMapTransformers<K, V>());
super();
this._values = new Map(initialValues);
}

Expand Down
16 changes: 16 additions & 0 deletions src/observable/map/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// In order to avoid a circular dependency problem at runtime between BaseObservableMap
// and the classes that extend it, it's important that:
//
// 1) It always remain the first module exported below.
// 2) Anything that imports any of the classes in this module
// ONLY import them from this index.ts file.
//
// See https://medium.com/visual-development/how-to-fix-nasty-circular-dependency-issues-once-and-for-all-in-javascript-typescript-a04c987cf0de
// for more on why this discipline is necessary.
export {BaseObservableMap, Mapper, Updater, Comparator, Filter} from './BaseObservableMap';
export {ApplyMap} from './ApplyMap';
export {FilteredMap} from './FilteredMap';
export {JoinedMap} from './JoinedMap';
export {LogMap} from './LogMap';
export {MappedMap} from './MappedMap';
export {ObservableMap} from './ObservableMap';

0 comments on commit 77f21f7

Please sign in to comment.