Handle export cancellation

This commit is contained in:
Jaiwanth 2021-06-27 20:55:54 +05:30
parent 398d49245f
commit d46fe678b0
6 changed files with 107 additions and 54 deletions

View file

@ -21,16 +21,18 @@ export default abstract class Exporter {
protected client: MatrixClient;
protected writer: WritableStreamDefaultWriter<any>;
protected fileStream: WritableStream<any>;
protected cancelled: boolean;
protected constructor(
protected room: Room,
protected exportType: exportTypes,
protected exportOptions?: exportOptions,
) {
this.cancelled = false;
this.files = [];
this.client = MatrixClientPeg.get();
window.addEventListener("beforeunload", this.onBeforeUnload);
window.addEventListener("onunload", this.abortExport);
window.addEventListener("onunload", this.abortWriter);
}
protected onBeforeUnload(e: BeforeUnloadEvent) {
@ -55,7 +57,8 @@ export default abstract class Exporter {
// Create a writable stream to the directory
this.fileStream = streamSaver.createWriteStream(filename);
console.info("Generating a ZIP...");
if (!this.cancelled) console.info("Generating a ZIP...");
else return this.cleanUp();
this.writer = this.fileStream.getWriter();
const files = this.files;
@ -67,21 +70,37 @@ export default abstract class Exporter {
},
});
if (this.cancelled) return this.cleanUp();
console.info("Writing to the file system...")
const reader = readableZipStream.getReader()
await this.pumpToFileStream(reader);
}
protected cleanUp() {
console.log("Cleaning up...");
window.removeEventListener("beforeunload", this.onBeforeUnload);
window.removeEventListener("onunload", this.abortWriter);
return "";
}
public async cancelExport() {
console.log("Cancelling export...");
this.cancelled = true;
await this.abortWriter();
}
protected async downloadPlainText(fileName: string, text: string): Promise<any> {
this.fileStream = streamSaver.createWriteStream(fileName);
this.writer = this.fileStream.getWriter()
const data = new TextEncoder().encode(text);
if (this.cancelled) return this.cleanUp();
await this.writer.write(data);
await this.writer.close();
}
protected async abortExport(): Promise<void> {
protected async abortWriter(): Promise<void> {
await this.fileStream?.abort();
await this.writer?.abort();
}
@ -134,6 +153,11 @@ export default abstract class Exporter {
const eventsPerCrawl = Math.min(limit, 1000);
const res: any = await this.client.createMessagesRequest(this.room.roomId, prevToken, eventsPerCrawl, "b");
if (this.cancelled) {
this.cleanUp();
return [];
}
if (res.chunk.length === 0) break;
limit -= res.chunk.length;

View file

@ -314,7 +314,7 @@ export default class HTMLExporter extends Exporter {
let prevEvent = null;
for (let i = 0; i < events.length; i++) {
const event = events[i];
console.log("Processing event " + i + " out of " + events.length);
if (this.cancelled) return this.cleanUp();
if (!haveTileForEvent(event)) continue;
content += this._wantsDateSeparator(event, prevEvent) ? this.getDateSeparator(event) : "";
@ -349,17 +349,18 @@ export default class HTMLExporter extends Exporter {
this.addFile(`icons/${iconName}`, new Blob([exportIcons[iconName]]));
}
console.info("HTML creation successful!");
await this.downloadZIP();
const exportEnd = performance.now();
console.info("Export successful!")
console.log(`Exported ${res.length} events in ${(exportEnd - fetchStart)/1000} seconds`);
if (this.cancelled) {
console.info("Export cancelled successfully");
} else {
console.info("Export successful!")
console.log(`Exported ${res.length} events in ${(exportEnd - fetchStart)/1000} seconds`);
}
window.removeEventListener("beforeunload", this.onBeforeUnload);
window.removeEventListener("onunload", this.abortExport);
this.cleanUp();
}
}

View file

@ -64,6 +64,7 @@ ${json}
protected async createOutput(events: MatrixEvent[]) {
let content = "";
for (const event of events) {
if (this.cancelled) return this.cleanUp();
if (!haveTileForEvent(event)) continue;
content += await this.getJSONString(event);
}
@ -93,11 +94,14 @@ ${json}
const exportEnd = performance.now();
console.info("Export successful!")
console.log(`Exported ${res.length} events in ${(exportEnd - fetchStart)/1000} seconds`);
if (this.cancelled) {
console.info("Export cancelled successfully");
} else {
console.info("Export successful!")
console.log(`Exported ${res.length} events in ${(exportEnd - fetchStart)/1000} seconds`);
}
window.removeEventListener("beforeunload", this.onBeforeUnload);
window.removeEventListener("onunload", this.abortExport);
this.cleanUp()
}
}

View file

@ -81,6 +81,7 @@ export default class PlainTextExporter extends Exporter {
protected async createOutput(events: MatrixEvent[]) {
let content = "";
for (const event of events) {
if (this.cancelled) return this.cleanUp();
if (!haveTileForEvent(event)) continue;
const textForEvent = await this._textForEvent(event);
content += textForEvent && `${new Date(event.getTs()).toLocaleString()} - ${textForEvent}\n`;
@ -101,6 +102,8 @@ export default class PlainTextExporter extends Exporter {
console.info("Creating output...");
const text = await this.createOutput(res);
if (this.cancelled) return this.cleanUp();
if (this.files.length) {
this.addFile("export.txt", new Blob([text]));
await this.downloadZIP();
@ -111,11 +114,14 @@ export default class PlainTextExporter extends Exporter {
const exportEnd = performance.now();
console.info("Export successful!")
console.log(`Exported ${res.length} events in ${(exportEnd - fetchStart)/1000} seconds`);
if (this.cancelled) {
console.info("Export cancelled successfully");
} else {
console.info("Export successful!")
console.log(`Exported ${res.length} events in ${(exportEnd - fetchStart)/1000} seconds`);
}
window.removeEventListener("onunload", this.abortExport);
window.removeEventListener("beforeunload", this.onBeforeUnload);
this.cleanUp();
}
}

View file

@ -1,8 +1,4 @@
import { Room } from "matrix-js-sdk/src/models/room";
import { _t } from "../../languageHandler";
import HTMLExporter from "./HtmlExport";
import JSONExporter from "./JSONExport";
import PlainTextExporter from "./PlainTextExport";
export enum exportFormats {
HTML = "HTML",
@ -48,23 +44,3 @@ export interface exportOptions {
maxSize: number;
}
const exportConversationalHistory = async (
room: Room,
format: string,
exportType: exportTypes,
exportOptions?: exportOptions,
) => {
switch (format) {
case exportFormats.HTML:
await new HTMLExporter(room, exportType, exportOptions).export();
break;
case exportFormats.JSON:
await new JSONExporter(room, exportType, exportOptions).export();
break;
case exportFormats.PLAIN_TEXT:
await new PlainTextExporter(room, exportType, exportOptions).export();
break;
}
};
export default exportConversationalHistory;