Extract some gap filling functionality into a helper method

This commit is contained in:
Danila Fedorin 2021-09-07 13:17:44 -07:00
parent ae6e211150
commit 299abe3e7e

View file

@ -266,27 +266,7 @@ export class BaseRoom extends EventEmitter {
}
}
/** @public */
fillGap(fragmentEntry, amount, log = null) {
// TODO move some/all of this out of BaseRoom
return this._platform.logger.wrapOrRun(log, "fillGap", async log => {
log.set("id", this.id);
log.set("fragment", fragmentEntry.fragmentId);
log.set("dir", fragmentEntry.direction.asApiString());
if (fragmentEntry.edgeReached) {
log.set("edgeReached", true);
return;
}
const response = await this._hsApi.messages(this._roomId, {
from: fragmentEntry.token,
dir: fragmentEntry.direction.asApiString(),
limit: amount,
filter: {
lazy_load_members: true,
include_redundant_members: true,
}
}, {log}).response();
async _fetchEvents(callback, log) {
const txn = await this._storage.readWriteTxn([
this._storage.storeNames.pendingEvents,
this._storage.storeNames.timelineEvents,
@ -296,9 +276,6 @@ export class BaseRoom extends EventEmitter {
let extraGapFillChanges;
let gapResult;
try {
// detect remote echos of pending messages in the gap
extraGapFillChanges = await this._writeGapFill(response.chunk, txn, log);
// write new events into gap
const relationWriter = new RelationWriter({
roomId: this._roomId,
fragmentIdComparer: this._fragmentIdComparer,
@ -310,7 +287,9 @@ export class BaseRoom extends EventEmitter {
fragmentIdComparer: this._fragmentIdComparer,
relationWriter
});
gapResult = await gapWriter.writeFragmentFill(fragmentEntry, response, txn, log);
const callbackResult = await callback(txn, gapWriter);
extraGapFillChanges = callbackResult.extraGapFillChanges;
gapResult = callbackResult.gapResult;
} catch (err) {
txn.abort();
throw err;
@ -332,6 +311,35 @@ export class BaseRoom extends EventEmitter {
this._timeline.replaceEntries(gapResult.updatedEntries);
this._timeline.addEntries(gapResult.entries);
}
}
/** @public */
fillGap(fragmentEntry, amount, log = null) {
// TODO move some/all of this out of BaseRoom
return this._platform.logger.wrapOrRun(log, "fillGap", async log => {
log.set("id", this.id);
log.set("fragment", fragmentEntry.fragmentId);
log.set("dir", fragmentEntry.direction.asApiString());
if (fragmentEntry.edgeReached) {
log.set("edgeReached", true);
return;
}
const response = await this._hsApi.messages(this._roomId, {
from: fragmentEntry.token,
dir: fragmentEntry.direction.asApiString(),
limit: amount,
filter: {
lazy_load_members: true,
include_redundant_members: true,
}
}, {log}).response();
await this._fetchEvents(async (txn, gapWriter) => {
// detect remote echos of pending messages in the gap
const extraGapFillChanges = await this._writeGapFill(response.chunk, txn, log);
const gapResult = await gapWriter.writeFragmentFill(fragmentEntry, response, txn, log);
return { extraGapFillChanges, gapResult };
}, log);
});
}