From cd4aa53c7d0886cd1f414158edb86bb5f0d1e497 Mon Sep 17 00:00:00 2001 From: spacemeowx2 Date: Mon, 24 Oct 2022 20:47:25 +0800 Subject: [PATCH] refactor: use Deno.read/writeTextFile --- src/state.ts | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/state.ts b/src/state.ts index e60af2e..fd611de 100644 --- a/src/state.ts +++ b/src/state.ts @@ -34,17 +34,15 @@ export class FileStateBackend implements StateBackend { constructor(private path: string) {} async read(): Promise { - const decoder = new TextDecoder(); - const data = await Deno.readFile(this.path); - const json = JSON.parse(decoder.decode(data)); + const data = await Deno.readTextFile(this.path); + const json = JSON.parse(data); return json; } async write(newState: State): Promise { - const encoder = new TextEncoder(); - const data = encoder.encode(JSON.stringify(newState, undefined, 2)); + const data = JSON.stringify(newState, undefined, 2); const swapPath = `${this.path}.swap`; - await Deno.writeFile(swapPath, data); + await Deno.writeTextFile(swapPath, data); await Deno.rename(swapPath, this.path); } }