This repository has been archived on 2022-08-19. You can view files and clone it, but cannot push or open issues or pull requests.
hydrogen-web/src/domain/session/room/timeline/tiles/GapTile.js

131 lines
3.8 KiB
JavaScript
Raw Normal View History

2020-08-05 22:08:55 +05:30
/*
Copyright 2020 Bruno Windels <bruno@windels.cloud>
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 {SimpleTile} from "./SimpleTile.js";
import {UpdateAction} from "../UpdateAction.js";
2019-03-09 00:34:56 +05:30
export class GapTile extends SimpleTile {
constructor(options) {
2019-03-09 05:10:03 +05:30
super(options);
this._loading = false;
this._error = null;
this._isAtTop = true;
2021-11-05 17:25:29 +05:30
this._siblingChanged = false;
2019-03-09 00:34:56 +05:30
}
2019-03-09 05:10:03 +05:30
async fill() {
if (!this._loading && !this._entry.edgeReached) {
2019-03-09 05:10:03 +05:30
this._loading = true;
this.emitChange("isLoading");
2019-03-09 05:10:03 +05:30
try {
await this._room.fillGap(this._entry, 10);
2019-03-09 05:10:03 +05:30
} catch (err) {
console.error(`room.fillGap(): ${err.message}:\n${err.stack}`);
2019-03-09 05:10:03 +05:30
this._error = err;
this.emitChange("error");
// rethrow so caller of this method
// knows not to keep calling this for now
throw err;
2019-06-02 19:16:24 +05:30
} finally {
this._loading = false;
this.emitChange("isLoading");
2019-03-09 05:10:03 +05:30
}
2021-11-05 17:25:29 +05:30
return true;
2019-03-09 05:10:03 +05:30
}
2021-11-05 17:25:29 +05:30
return false;
2019-03-09 05:10:03 +05:30
}
2021-11-05 17:25:29 +05:30
async notifyVisible() {
let depth = 0;
let result;
do {
result = await this.fill();
depth = depth + 1;
} while (depth < 10 && !this._siblingChanged && result && !this.isDisposed)
this._siblingChanged = false;
}
get isAtTop() {
return this._isAtTop;
}
updatePreviousSibling(prev) {
super.updatePreviousSibling(prev);
const isAtTop = !prev;
if (this._isAtTop !== isAtTop) {
this._isAtTop = isAtTop;
this.emitChange("isAtTop");
}
2021-11-05 17:25:29 +05:30
this._siblingChanged = true;
}
updateNextSibling() {
this._siblingChanged = true;
}
updateEntry(entry, params) {
super.updateEntry(entry, params);
if (!entry.isGap) {
return UpdateAction.Remove();
} else {
return UpdateAction.Nothing();
}
}
get shape() {
return "gap";
}
2019-03-09 05:10:03 +05:30
get isLoading() {
return this._loading;
}
get error() {
if (this._error) {
const dir = this._entry.prev_batch ? "previous" : "next";
return `Could not load ${dir} messages: ${this._error.message}`;
}
return null;
2019-03-09 00:34:56 +05:30
}
}
import {FragmentBoundaryEntry} from "../../../../../matrix/room/timeline/entries/FragmentBoundaryEntry.js";
export function tests() {
return {
"uses updated token to fill": async assert => {
let currentToken = 5;
const fragment = {
id: 0,
previousToken: currentToken,
roomId: "!abc"
};
const room = {
async fillGap(entry) {
assert.equal(entry.token, currentToken);
currentToken += 1;
const newEntry = entry.withUpdatedFragment(Object.assign({}, fragment, {previousToken: currentToken}));
tile.updateEntry(newEntry);
}
};
2021-08-06 23:32:41 +05:30
const tile = new GapTile({entry: new FragmentBoundaryEntry(fragment, true), roomVM: {room}});
await tile.fill();
await tile.fill();
await tile.fill();
assert.equal(currentToken, 8);
}
}
2021-08-06 23:32:41 +05:30
}