From c599a304c8882afb5e57610cde324f4fbbf6a74e Mon Sep 17 00:00:00 2001 From: syuilo Date: Sun, 12 Dec 2021 06:21:48 +0900 Subject: [PATCH] refactor --- .../processors/db/export-custom-emojis.ts | 55 +++++++------------ .../src/queue/processors/db/export-notes.ts | 46 ++++++---------- 2 files changed, 37 insertions(+), 64 deletions(-) diff --git a/packages/backend/src/queue/processors/db/export-custom-emojis.ts b/packages/backend/src/queue/processors/db/export-custom-emojis.ts index ed0ad249a5..aa13c76638 100644 --- a/packages/backend/src/queue/processors/db/export-custom-emojis.ts +++ b/packages/backend/src/queue/processors/db/export-custom-emojis.ts @@ -39,16 +39,20 @@ export async function exportCustomEmojis(job: Bull.Job, done: () => void): Promi const metaStream = fs.createWriteStream(metaPath, { flags: 'a' }); - await new Promise((res, rej) => { - metaStream.write('[', err => { - if (err) { - logger.error(err); - rej(err); - } else { - res(); - } + const writeMeta = (text: string): Promise => { + return new Promise((res, rej) => { + metaStream.write(text, err => { + if (err) { + logger.error(err); + rej(err); + } else { + res(); + } + }); }); - }); + }; + + await writeMeta('['); const customEmojis = await Emojis.find({ where: { @@ -72,34 +76,17 @@ export async function exportCustomEmojis(job: Bull.Job, done: () => void): Promi logger.error(e); } - await new Promise((res, rej) => { - const content = JSON.stringify({ - id: exportId, - downloaded: downloaded, - emoji: emoji, - }); - const isFirst = customEmojis.indexOf(emoji) === 0; - metaStream.write(isFirst ? content : ',\n' + content, err => { - if (err) { - logger.error(err); - rej(err); - } else { - res(); - } - }); + const content = JSON.stringify({ + id: exportId, + downloaded: downloaded, + emoji: emoji, }); + const isFirst = customEmojis.indexOf(emoji) === 0; + + await writeMeta(isFirst ? content : ',\n' + content); } - await new Promise((res, rej) => { - metaStream.write(']', err => { - if (err) { - logger.error(err); - rej(err); - } else { - res(); - } - }); - }); + await writeMeta(']'); metaStream.end(); diff --git a/packages/backend/src/queue/processors/db/export-notes.ts b/packages/backend/src/queue/processors/db/export-notes.ts index 7220455edc..e64e763513 100644 --- a/packages/backend/src/queue/processors/db/export-notes.ts +++ b/packages/backend/src/queue/processors/db/export-notes.ts @@ -34,16 +34,20 @@ export async function exportNotes(job: Bull.Job, done: any): Prom const stream = fs.createWriteStream(path, { flags: 'a' }); - await new Promise((res, rej) => { - stream.write('[', err => { - if (err) { - logger.error(err); - rej(err); - } else { - res(); - } + const write = (text: string): Promise => { + return new Promise((res, rej) => { + stream.write(text, err => { + if (err) { + logger.error(err); + rej(err); + } else { + res(); + } + }); }); - }); + }; + + await write('['); let exportedNotesCount = 0; let cursor: Note['id'] | null = null; @@ -73,17 +77,8 @@ export async function exportNotes(job: Bull.Job, done: any): Prom poll = await Polls.findOneOrFail({ noteId: note.id }); } const content = JSON.stringify(serialize(note, poll)); - await new Promise((res, rej) => { - const isFirst = exportedNotesCount === 0; - stream.write(isFirst ? content : ',\n' + content, err => { - if (err) { - logger.error(err); - rej(err); - } else { - res(); - } - }); - }); + const isFirst = exportedNotesCount === 0; + await write(isFirst ? content : ',\n' + content); exportedNotesCount++; } @@ -94,16 +89,7 @@ export async function exportNotes(job: Bull.Job, done: any): Prom job.progress(exportedNotesCount / total); } - await new Promise((res, rej) => { - stream.write(']', err => { - if (err) { - logger.error(err); - rej(err); - } else { - res(); - } - }); - }); + await write(']'); stream.end(); logger.succ(`Exported to: ${path}`);