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 */ async _fetchEvents(callback, log) {
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();
const txn = await this._storage.readWriteTxn([ const txn = await this._storage.readWriteTxn([
this._storage.storeNames.pendingEvents, this._storage.storeNames.pendingEvents,
this._storage.storeNames.timelineEvents, this._storage.storeNames.timelineEvents,
@ -296,9 +276,6 @@ export class BaseRoom extends EventEmitter {
let extraGapFillChanges; let extraGapFillChanges;
let gapResult; let gapResult;
try { 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({ const relationWriter = new RelationWriter({
roomId: this._roomId, roomId: this._roomId,
fragmentIdComparer: this._fragmentIdComparer, fragmentIdComparer: this._fragmentIdComparer,
@ -310,7 +287,9 @@ export class BaseRoom extends EventEmitter {
fragmentIdComparer: this._fragmentIdComparer, fragmentIdComparer: this._fragmentIdComparer,
relationWriter relationWriter
}); });
gapResult = await gapWriter.writeFragmentFill(fragmentEntry, response, txn, log); const callbackResult = await callback(txn, gapWriter);
extraGapFillChanges = callbackResult.extraGapFillChanges;
gapResult = callbackResult.gapResult;
} catch (err) { } catch (err) {
txn.abort(); txn.abort();
throw err; throw err;
@ -332,6 +311,35 @@ export class BaseRoom extends EventEmitter {
this._timeline.replaceEntries(gapResult.updatedEntries); this._timeline.replaceEntries(gapResult.updatedEntries);
this._timeline.addEntries(gapResult.entries); 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);
}); });
} }