make SimpleTile inherit from ViewModel

to use same update mechanism and have viewmodel infra available for tile
This commit is contained in:
Bruno Windels 2020-08-17 15:11:39 +02:00
parent 9745c58144
commit cf0af775e3
4 changed files with 12 additions and 13 deletions

View file

@ -70,6 +70,10 @@ export class ViewModel extends EventEmitter {
return result;
}
updateOptions(options) {
this._options = Object.assign(this._options, options);
}
emitChange(changedProps) {
if (this._options.emitChange) {
this._options.emitChange(changedProps);

View file

@ -29,16 +29,16 @@ export class GapTile extends SimpleTile {
// prevent doing this twice
if (!this._loading) {
this._loading = true;
this.emitUpdate("isLoading");
this.emitChange("isLoading");
try {
await this._timeline.fillGap(this._entry, 10);
} catch (err) {
console.error(`timeline.fillGap(): ${err.message}:\n${err.stack}`);
this._error = err;
this.emitUpdate("error");
this.emitChange("error");
} finally {
this._loading = false;
this.emitUpdate("isLoading");
this.emitChange("isLoading");
}
}
}

View file

@ -62,7 +62,7 @@ export class MessageTile extends SimpleTile {
const isContinuation = prev && prev instanceof MessageTile && prev.sender === this.sender;
if (isContinuation !== this._isContinuation) {
this._isContinuation = isContinuation;
this.emitUpdate("isContinuation");
this.emitChange("isContinuation");
}
}
}

View file

@ -15,11 +15,12 @@ limitations under the License.
*/
import {UpdateAction} from "../UpdateAction.js";
import {ViewModel} from "../../../../ViewModel.js";
export class SimpleTile {
export class SimpleTile extends ViewModel {
constructor({entry}) {
super();
this._entry = entry;
this._emitUpdate = null;
}
// view model props for all subclasses
// hmmm, could also do instanceof ... ?
@ -38,12 +39,6 @@ export class SimpleTile {
return false;
}
emitUpdate(paramName) {
if (this._emitUpdate) {
this._emitUpdate(this, paramName);
}
}
get internalId() {
return this._entry.asEventKey().toString();
}
@ -53,7 +48,7 @@ export class SimpleTile {
}
// TilesCollection contract below
setUpdateEmit(emitUpdate) {
this._emitUpdate = emitUpdate;
this.updateOptions({emitChange: paramName => emitUpdate(this, paramName)});
}
get upperEntry() {