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.
|
|
|
|
*/
|
|
|
|
|
2020-04-21 00:56:39 +05:30
|
|
|
import {SimpleTile} from "./SimpleTile.js";
|
|
|
|
import {UpdateAction} from "../UpdateAction.js";
|
2019-03-09 00:34:56 +05:30
|
|
|
|
2020-04-21 00:56:39 +05:30
|
|
|
export class GapTile extends SimpleTile {
|
2020-10-28 22:07:45 +05:30
|
|
|
constructor(options) {
|
2019-03-09 05:10:03 +05:30
|
|
|
super(options);
|
|
|
|
this._loading = false;
|
|
|
|
this._error = null;
|
2021-09-15 19:27:31 +05:30
|
|
|
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() {
|
2021-09-08 20:09:46 +05:30
|
|
|
if (!this._loading && !this._entry.edgeReached) {
|
2019-03-09 05:10:03 +05:30
|
|
|
this._loading = true;
|
2020-08-17 18:41:39 +05:30
|
|
|
this.emitChange("isLoading");
|
2019-03-09 05:10:03 +05:30
|
|
|
try {
|
2020-10-28 22:07:45 +05:30
|
|
|
await this._room.fillGap(this._entry, 10);
|
2019-03-09 05:10:03 +05:30
|
|
|
} catch (err) {
|
2020-10-28 22:07:45 +05:30
|
|
|
console.error(`room.fillGap(): ${err.message}:\n${err.stack}`);
|
2019-03-09 05:10:03 +05:30
|
|
|
this._error = err;
|
2020-08-17 18:41:39 +05:30
|
|
|
this.emitChange("error");
|
2020-08-20 18:58:22 +05:30
|
|
|
// 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;
|
2020-08-17 18:41:39 +05:30
|
|
|
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() {
|
2021-11-05 20:12:07 +05:30
|
|
|
// we do (up to 10) backfills while no new tiles have been added to the timeline
|
|
|
|
// because notifyVisible won't be called again until something gets added to the timeline
|
2021-11-05 17:25:29 +05:30
|
|
|
let depth = 0;
|
2021-11-05 20:12:07 +05:30
|
|
|
let canFillMore;
|
|
|
|
this._siblingChanged = false;
|
2021-11-05 17:25:29 +05:30
|
|
|
do {
|
2021-11-05 20:12:07 +05:30
|
|
|
canFillMore = await this.fill();
|
2021-11-05 17:25:29 +05:30
|
|
|
depth = depth + 1;
|
2021-11-05 20:12:07 +05:30
|
|
|
} while (depth < 10 && !this._siblingChanged && canFillMore && !this.isDisposed);
|
2021-09-08 15:35:59 +05:30
|
|
|
}
|
|
|
|
|
2021-09-15 19:27:31 +05:30
|
|
|
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() {
|
2021-11-05 20:12:07 +05:30
|
|
|
// if the sibling of the gap changed while calling room.fill(),
|
|
|
|
// we intepret this as at least one new tile has been added to
|
|
|
|
// the timeline. See notifyVisible why this is important.
|
2021-11-05 17:25:29 +05:30
|
|
|
this._siblingChanged = true;
|
2021-09-15 19:27:31 +05:30
|
|
|
}
|
|
|
|
|
2022-01-14 20:36:29 +05:30
|
|
|
updateEntry(entry, params, tilesCreator) {
|
|
|
|
super.updateEntry(entry, params, tilesCreator);
|
2019-06-13 01:27:13 +05:30
|
|
|
if (!entry.isGap) {
|
|
|
|
return UpdateAction.Remove();
|
|
|
|
} else {
|
|
|
|
return UpdateAction.Nothing();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-02 03:33:55 +05:30
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2021-05-27 12:40:10 +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}});
|
2021-05-27 12:40:10 +05:30
|
|
|
await tile.fill();
|
|
|
|
await tile.fill();
|
|
|
|
await tile.fill();
|
|
|
|
assert.equal(currentToken, 8);
|
|
|
|
}
|
|
|
|
}
|
2021-08-06 23:32:41 +05:30
|
|
|
}
|