Skip to content

Commit

Permalink
Cancel backup in progress if another one is started.
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan McQuen <[email protected]>
  • Loading branch information
ryanpcmcquen committed Jun 16, 2021
1 parent 3b1b78e commit b6b7ec5
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 11 deletions.
38 changes: 29 additions & 9 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ const monkeyPatchConsole = (plugin: Plugin) => {

const logFile = `${plugin.manifest.dir}/logs.txt`;
const logs: string[] = [];
const logMessages = (prefix: string) => (...messages: unknown[]) => {
logs.push(`\n[${prefix}]`);
for (const message of messages) {
logs.push(String(message));
}
plugin.app.vault.adapter.write(logFile, logs.join(" "));
};
const logMessages =
(prefix: string) =>
(...messages: unknown[]) => {
logs.push(`\n[${prefix}]`);
for (const message of messages) {
logs.push(String(message));
}
plugin.app.vault.adapter.write(logFile, logs.join(" "));
};

console.debug = logMessages("debug");
console.error = logMessages("error");
Expand All @@ -39,6 +41,8 @@ export default class DropboxBackups extends Plugin {
dbx: unknown;
dbxAuth: unknown;

backupsQueue: number[] = [];

icons = {
cloudSlash: `
<path d="M8.4,4.7L5.6,7.5l22,22c0,0-0.1,0-0.1,0l8.7,8.7l0-0.1L74.1,76h-0.1l4,4h0.1l14,14l2.8-2.8L83.6,79.9 c9.1-0.7,16.4-8.4,16.4-17.7c0-9.1-6.9-16.7-15.7-17.7c0-0.4,0-0.8,0-1.1c0-14.5-11.8-26.3-26.3-26.3c-9.8,0-18.7,5.5-23.2,14 L8.4,4.7z M58,21.1c12.3,0,22.3,10,22.3,22.3c0,0.8-0.1,1.7-0.2,2.8l-0.3,2.2h2.4c7.6,0,13.7,6.2,13.7,13.8 C96,69.8,89.8,76,82.2,76h-2.5l-42-41.9C41.4,26.2,49.3,21.1,58,21.1L58,21.1z M22.4,30.1c-5.3,1.7-9.3,6.6-9.7,12.4 C5.2,45.3,0,52.5,0,60.7C0,71.3,8.7,80,19.3,80h52.9l-4-4H19.3C10.9,76,4,69.1,4,60.7c0-6.9,4.6-12.9,11.3-14.8l1.5-0.4l-0.1-2 c0-5.2,4-9.5,9.1-9.9L22.4,30.1z"/>
Expand Down Expand Up @@ -73,6 +77,12 @@ export default class DropboxBackups extends Plugin {

async backup(): Promise<void> {
const now = Date.now();
let status = "complete";

this.backupsQueue.push(now);
if (this.backupsQueue.length > 1) {
this.backupsQueue = [now];
}

const year = moment(new Date(now)).format("YYYY");
const month = moment(new Date(now)).format("MM");
Expand All @@ -97,6 +107,10 @@ export default class DropboxBackups extends Plugin {
if (fileList.length > 0) {
let counter = 0;
for (const file of fileList) {
if (!this.backupsQueue.includes(now)) {
status = "canceled";
break;
}
if (this.app.vault.adapter.exists(file.path)) {
const fileContents = this.couldBeBinary(file.extension)
? await this.app.vault.adapter.readBinary(file.path)
Expand Down Expand Up @@ -125,7 +139,7 @@ export default class DropboxBackups extends Plugin {
}
}

console.log(`Backup to ${pathPrefix} complete!`);
console.log(`Backup to ${pathPrefix} ${status}!`);

if (!Platform.isMobile && this.dropboxBackupsRibbonIcon) {
this.dropboxBackupsRibbonIcon.setAttribute(
Expand All @@ -137,6 +151,11 @@ export default class DropboxBackups extends Plugin {
this.dropboxBackupsRibbonIcon,
"dropbox-backups-upload-complete"
);

const indexOfNow = this.backupsQueue.indexOf(now);
if (indexOfNow > -1) {
this.backupsQueue.splice(indexOfNow, 1);
}
}

async setupAuth() {
Expand Down Expand Up @@ -186,7 +205,8 @@ export default class DropboxBackups extends Plugin {
params.code
);

const accessTokenResponseResult = accessTokenResponse?.result as accessTokenStore;
const accessTokenResponseResult =
accessTokenResponse?.result as accessTokenStore;
this.dropboxBackupsTokenStore = accessTokenResponseResult;
await this.app.vault.adapter.write(
this.dropboxBackupsTokenStorePath,
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "obsidian-dropbox-backups",
"name": "Dropbox Backups",
"version": "0.13.1",
"version": "0.14.0",
"minAppVersion": "0.9.12",
"description": "Automated backups to Dropbox for your enjoyment.",
"author": "ryanpcmcquen",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obsidian-dropbox-backups",
"version": "0.13.1",
"version": "0.14.0",
"description": "Automated backups to Dropbox for your enjoyment.",
"main": "main.js",
"repository": "https://github.com/ryanpcmcquen/obsidian-dropbox-backups",
Expand Down
1 change: 1 addition & 0 deletions versions.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"0.14.0": "0.9.12",
"0.13.1": "0.9.12",
"0.13.0": "0.9.12",
"0.12.4": "0.9.12",
Expand Down

0 comments on commit b6b7ec5

Please sign in to comment.