Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

refactor: cleanup attachments progress event

thecodrr0c799e4d1fff
diff --git a/packages/core/api/sync/index.js b/packages/core/api/sync/index.js
index 90b4d3671..1af80b72b 100644
--- a/packages/core/api/sync/index.js
+++ b/packages/core/api/sync/index.js
@@ -25,7 +25,7 @@
* Syncing should pause until all the conflicts have been resolved
* And then it should continue.
*/
-import { EV, EVENTS } from "../../common";
+import { EV, EVENTS, sendAttachmentsProgressEvent } from "../../common";
import Constants from "../../utils/constants";
import http from "../../utils/http";
import TokenManager from "../token-manager";
@@ -155,12 +155,9 @@ export default class Sync {
const attachments = this._db.attachments.pending;
console.log("Uploading attachments", this._db.attachments.pending);
for (var i = 0; i < attachments.length; ++i) {
+ sendAttachmentsProgressEvent("upload", attachments.length, i);
+
const attachment = attachments[i];
- EV.publish(EVENTS.attachmentsLoading, {
- type: "upload",
- total: attachments.length,
- current: i,
- });
const { hash } = attachment.metadata;
const isUploaded = await this._db.fs.uploadFile(hash, hash);
@@ -168,10 +165,6 @@ export default class Sync {
await this._db.attachments.markAsUploaded(attachment.id);
}
- EV.publish(EVENTS.attachmentsLoading, {
- type: "upload",
- total: attachments.length,
- current: attachments.length,
- });
+ sendAttachmentsProgressEvent("upload", attachments.length);
}
}
diff --git a/packages/core/collections/attachments.js b/packages/core/collections/attachments.js
index e1e491257..6237f605b 100644
--- a/packages/core/collections/attachments.js
+++ b/packages/core/collections/attachments.js
@@ -2,7 +2,7 @@ import Collection from "./collection";
import id from "../utils/id";
import { deleteItem, hasItem } from "../utils/array";
import hosts from "../utils/constants";
-import { EV, EVENTS } from "../common";
+import { EV, EVENTS, sendAttachmentsProgressEvent } from "../common";
import dataurl from "../utils/dataurl";
import dayjs from "dayjs";
@@ -134,11 +134,7 @@ export default class Attachments extends Collection {
);
console.log("Downloading attachments", attachments);
for (let i = 0; i < attachments.length; i++) {
- EV.publish(EVENTS.attachmentsLoading, {
- type: "download",
- total: attachments.length,
- current: i,
- });
+ sendAttachmentsProgressEvent("download", attachments.length, i);
const { hash } = attachments[i].metadata;
@@ -156,11 +152,7 @@ export default class Attachments extends Collection {
}),
});
}
- EV.publish(EVENTS.attachmentsLoading, {
- type: "download",
- total: attachments.length,
- current: attachments.length,
- });
+ sendAttachmentsProgressEvent("download", attachments.length);
}
get deleted() {
diff --git a/packages/core/common.js b/packages/core/common.js
index b65d18030..57a673e5a 100644
--- a/packages/core/common.js
+++ b/packages/core/common.js
@@ -10,6 +10,14 @@ export async function sendCheckUserStatusEvent(type) {
return results.some((r) => r.type === type && r.result === true);
}
+export async function sendAttachmentsProgressEvent(type, total, current) {
+ EV.publish(EVENTS.attachmentsLoading, {
+ type,
+ total,
+ current: current || total,
+ });
+}
+
export const CLIENT_ID = "notesnook";
export const CHECK_IDS = {