vector-im-hydrogen-web/src/matrix/room/timeline/Timeline.js

113 lines
3.7 KiB
JavaScript
Raw Normal View History

2020-08-05 18:38:55 +02:00
/*
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 {SortedArray, MappedList, ConcatList} from "../../../observable/index.js";
2020-09-10 16:40:30 +02:00
import {Disposables} from "../../../utils/Disposables.js";
import {Direction} from "./Direction.js";
import {TimelineReader} from "./persistence/TimelineReader.js";
import {PendingEventEntry} from "./entries/PendingEventEntry.js";
export class Timeline {
2020-03-21 23:40:40 +01:00
constructor({roomId, storage, closeCallback, fragmentIdComparer, pendingEvents, user}) {
this._roomId = roomId;
this._storage = storage;
this._closeCallback = closeCallback;
this._fragmentIdComparer = fragmentIdComparer;
2020-09-10 16:40:30 +02:00
this._disposables = new Disposables();
this._remoteEntries = new SortedArray((a, b) => a.compare(b));
2019-05-19 20:49:46 +02:00
this._timelineReader = new TimelineReader({
roomId: this._roomId,
storage: this._storage,
fragmentIdComparer: this._fragmentIdComparer
});
2020-09-10 16:40:30 +02:00
this._readerRequest = null;
const localEntries = new MappedList(pendingEvents, pe => {
return new PendingEventEntry({pendingEvent: pe, user});
}, (pee, params) => {
pee.notifyUpdate(params);
});
this._allEntries = new ConcatList(this._remoteEntries, localEntries);
}
/** @package */
async load() {
2020-09-10 16:40:30 +02:00
// 30 seems to be a good amount to fill the entire screen
const readerRequest = this._disposables.track(this._timelineReader.readFromEnd(30));
try {
const entries = await readerRequest.complete();
this._remoteEntries.setManySorted(entries);
} finally {
this._disposables.disposeTracked(readerRequest);
}
}
replaceEntries(entries) {
for (const entry of entries) {
this._remoteEntries.replace(entry);
}
}
2020-05-07 19:14:53 +02:00
// TODO: should we rather have generic methods for
// - adding new entries
// - updating existing entries (redaction, relations)
/** @package */
appendLiveEntries(newEntries) {
this._remoteEntries.setManySorted(newEntries);
}
2020-03-21 23:40:40 +01:00
/** @package */
addGapEntries(newEntries) {
this._remoteEntries.setManySorted(newEntries);
2019-03-09 00:41:06 +01:00
}
2020-03-21 23:40:40 +01:00
// tries to prepend `amount` entries to the `entries` list.
2019-03-09 00:41:06 +01:00
async loadAtTop(amount) {
const firstEventEntry = this._remoteEntries.array.find(e => !!e.eventType);
2019-06-16 17:29:33 +02:00
if (!firstEventEntry) {
return;
2019-03-09 00:41:06 +01:00
}
2020-09-10 16:40:30 +02:00
const readerRequest = this._disposables.track(this._timelineReader.readFrom(
2019-06-16 17:29:33 +02:00
firstEventEntry.asEventKey(),
Direction.Backward,
amount
2020-09-10 16:40:30 +02:00
));
try {
const entries = await readerRequest.complete();
this._remoteEntries.setManySorted(entries);
} finally {
this._disposables.disposeTracked(readerRequest);
}
}
/** @public */
get entries() {
return this._allEntries;
}
/** @public */
dispose() {
2020-05-04 22:21:56 +02:00
if (this._closeCallback) {
this._disposables.dispose();
2020-05-04 22:21:56 +02:00
this._closeCallback();
this._closeCallback = null;
}
}
enableEncryption(decryptEntries) {
this._timelineReader.enableEncryption(decryptEntries);
}
}