hydrogen-web/src/domain/ViewModel.ts

150 lines
4.6 KiB
TypeScript
Raw Normal View History

2020-08-05 22:08:55 +05:30
/*
Copyright 2020 Bruno Windels <bruno@windels.cloud>
2022-02-14 22:20:17 +05:30
Copyright 2022 The Matrix.org Foundation C.I.C.
2020-08-05 22:08:55 +05:30
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.
*/
2020-04-18 22:46:16 +05:30
// ViewModel should just be an eventemitter, not an ObservableValue
// as in some cases it would really be more convenient to have multiple events (like telling the timeline to scroll down)
// we do need to return a disposable from EventEmitter.on, or at least have a method here to easily track a subscription to an EventEmitter
import {EventEmitter} from "../utils/EventEmitter";
2021-11-16 14:13:35 +05:30
import {Disposables} from "../utils/Disposables";
2022-02-14 22:20:17 +05:30
import type {Disposable} from "../utils/Disposables";
import type {Platform} from "../platform/web/Platform";
import type {Clock} from "../platform/web/dom/Clock";
import type {ILogger} from "../logging/types";
import type {Navigation} from "./navigation/Navigation";
import type {URLRouter} from "./navigation/URLRouter";
export type Options = {
2022-02-14 22:20:17 +05:30
platform: Platform
logger: ILogger
urlCreator: URLRouter
navigation: Navigation
emitChange?: (params: any) => void
}
export class ViewModel<O extends Options = Options> extends EventEmitter<{change: never}> {
private disposables?: Disposables;
private _isDisposed = false;
private _options: O;
constructor(options: O) {
2020-04-10 02:49:49 +05:30
super();
this._options = options;
2020-04-10 02:49:49 +05:30
}
2022-02-14 22:20:17 +05:30
childOptions<T extends Object>(explicitOptions: T): T & Options {
return Object.assign({}, this._options, explicitOptions);
2020-04-10 02:49:49 +05:30
}
get options(): O { return this._options; }
2020-10-16 21:36:20 +05:30
// makes it easier to pass through dependencies of a sub-view model
2022-02-14 22:20:17 +05:30
getOption<N extends keyof O>(name: N): O[N] {
2020-10-16 21:36:20 +05:30
return this._options[name];
}
2022-03-07 11:33:51 +05:30
observeNavigation(type: string, onChange: (value: string | true | undefined, type: string) => void) {
2022-03-03 15:36:25 +05:30
const segmentObservable = this.navigation.observe(type);
2022-03-07 11:33:51 +05:30
const unsubscribe = segmentObservable.subscribe((value: string | true | undefined) => {
2022-03-03 15:36:25 +05:30
onChange(value, type);
})
this.track(unsubscribe);
}
2022-02-14 22:20:17 +05:30
track<D extends Disposable>(disposable: D): D {
if (!this.disposables) {
this.disposables = new Disposables();
}
2020-09-10 20:10:30 +05:30
return this.disposables.track(disposable);
2020-04-10 02:49:49 +05:30
}
2022-02-14 22:20:17 +05:30
untrack(disposable: Disposable): undefined {
if (this.disposables) {
return this.disposables.untrack(disposable);
}
2022-02-14 22:20:17 +05:30
return undefined;
}
2022-02-14 22:20:17 +05:30
dispose(): void {
if (this.disposables) {
this.disposables.dispose();
}
this._isDisposed = true;
}
2022-02-14 22:20:17 +05:30
get isDisposed(): boolean {
return this._isDisposed;
}
2022-02-14 22:20:17 +05:30
disposeTracked(disposable: Disposable | undefined): undefined {
if (this.disposables) {
return this.disposables.disposeTracked(disposable);
}
2022-02-14 22:20:17 +05:30
return undefined;
}
// TODO: this will need to support binding
// if any of the expr is a function, assume the function is a binding, and return a binding function ourselves
//
// translated string should probably always be bindings, unless we're fine with a refresh when changing the language?
// we probably are, if we're using routing with a url, we could just refresh.
i18n(parts: TemplateStringsArray, ...expr: any[]) {
// just concat for now
2021-02-16 20:08:43 +05:30
let result = "";
for (let i = 0; i < parts.length; ++i) {
result = result + parts[i];
if (i < expr.length) {
result = result + expr[i];
}
}
return result;
}
2022-02-14 22:20:17 +05:30
updateOptions(options: O): void {
this._options = Object.assign(this._options, options);
}
2022-02-14 22:20:17 +05:30
emitChange(changedProps: any): void {
if (this._options.emitChange) {
this._options.emitChange(changedProps);
} else {
this.emit("change", changedProps);
}
2020-04-10 02:49:49 +05:30
}
2022-02-14 22:20:17 +05:30
get platform(): Platform {
return this._options.platform;
}
2022-02-14 22:20:17 +05:30
get clock(): Clock {
return this._options.platform.clock;
}
2022-02-14 22:20:17 +05:30
get logger(): ILogger {
2021-02-12 22:36:14 +05:30
return this.platform.logger;
2021-02-12 01:37:18 +05:30
}
2022-02-14 22:20:17 +05:30
get urlCreator(): URLRouter {
return this._options.urlCreator;
}
2022-02-14 22:20:17 +05:30
get navigation(): Navigation {
return this._options.navigation;
}
2020-04-10 02:49:49 +05:30
}