refactor: use Deno.read/writeTextFile

main
spacemeowx2 2022-10-24 20:47:25 +08:00
parent 3e2eede47c
commit cd4aa53c7d
1 changed files with 4 additions and 6 deletions

View File

@ -34,17 +34,15 @@ export class FileStateBackend implements StateBackend {
constructor(private path: string) {} constructor(private path: string) {}
async read(): Promise<State> { async read(): Promise<State> {
const decoder = new TextDecoder(); const data = await Deno.readTextFile(this.path);
const data = await Deno.readFile(this.path); const json = JSON.parse(data);
const json = JSON.parse(decoder.decode(data));
return json; return json;
} }
async write(newState: State): Promise<void> { async write(newState: State): Promise<void> {
const encoder = new TextEncoder(); const data = JSON.stringify(newState, undefined, 2);
const data = encoder.encode(JSON.stringify(newState, undefined, 2));
const swapPath = `${this.path}.swap`; const swapPath = `${this.path}.swap`;
await Deno.writeFile(swapPath, data); await Deno.writeTextFile(swapPath, data);
await Deno.rename(swapPath, this.path); await Deno.rename(swapPath, this.path);
} }
} }