From 163d8119c109c42e64ab37b01dec131f2cb5bf61 Mon Sep 17 00:00:00 2001 From: Volpeon Date: Tue, 19 Oct 2021 19:25:03 +0200 Subject: Code improvements, support a manual post queue --- src/services/dedupe.ts | 65 -------------------------------------------------- 1 file changed, 65 deletions(-) delete mode 100644 src/services/dedupe.ts (limited to 'src/services/dedupe.ts') diff --git a/src/services/dedupe.ts b/src/services/dedupe.ts deleted file mode 100644 index 2eeb5ee..0000000 --- a/src/services/dedupe.ts +++ /dev/null @@ -1,65 +0,0 @@ -import fs from "fs/promises"; -import path from "path"; -import * as f from "fp-ts"; -import * as t from "io-ts"; - -export const E926DedupeEntryC = t.type({ - provider: t.literal("e926"), - id: t.number, -}); - -export const DedupeEntryC = E926DedupeEntryC; - -export type E926DedupeEntry = t.TypeOf; - -export type DedupeEntry = t.TypeOf; - -export class Dedupe { - private entries: DedupeEntry[] = []; - - private readonly filePath: string; - - private isLoaded = false; - - constructor(private max: number, filename: string) { - this.filePath = path.join(process.cwd(), filename); - } - - private async load() { - if (this.isLoaded) { - return; - } - - try { - await fs.stat(this.filePath); - } catch { - await this.save(); - } - - const fileContent = await fs.readFile(this.filePath, "utf8"); - const entries = t.array(DedupeEntryC).decode(fileContent); - - if (f.either.isRight(entries)) { - this.entries = entries.right; - } - } - - private async save() { - await fs.writeFile(this.filePath, JSON.stringify(this.entries ?? []), "utf8"); - } - - async check(entry: DedupeEntry) { - await this.load(); - - const has = !!this.entries.find((e) => e.provider === entry.provider && e.id === entry.id); - - if (!has) { - this.entries.push(entry); - await this.save(); - } - - return has; - } -} - -export default new Dedupe(50, "dedupe.json"); -- cgit v1.2.3-70-g09d2