refactor: use Deno.read/writeTextFile
parent
3e2eede47c
commit
cd4aa53c7d
10
src/state.ts
10
src/state.ts
|
|
@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue