diff --git a/src/app.ts b/src/app.ts index 73a7cd2..eb4dd70 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,6 +1,6 @@ import { getBulletToken, getGToken, loginManually } from "./iksm.ts"; import { MultiProgressBar, Mutex } from "../deps.ts"; -import { DEFAULT_STATE, State } from "./state.ts"; +import { DEFAULT_STATE, FileStateBackend, State, StateBackend } from "./state.ts"; import { checkToken, getBankaraBattleHistories, @@ -25,6 +25,7 @@ export type Opts = { noProgress: boolean; monitor: boolean; cache?: Cache; + stateBackend?: StateBackend; }; export const DEFAULT_OPTS: Opts = { @@ -157,22 +158,18 @@ type Progress = { export class App { state: State = DEFAULT_STATE; + stateBackend: StateBackend; constructor(public opts: Opts) { + this.stateBackend = opts.stateBackend ?? new FileStateBackend(opts.profilePath); } async writeState(newState: State) { this.state = newState; - const encoder = new TextEncoder(); - const data = encoder.encode(JSON.stringify(this.state, undefined, 2)); - const swapPath = `${this.opts.profilePath}.swap`; - await Deno.writeFile(swapPath, data); - await Deno.rename(swapPath, this.opts.profilePath); + await this.stateBackend.write(newState); } async readState() { - const decoder = new TextDecoder(); try { - const data = await Deno.readFile(this.opts.profilePath); - const json = JSON.parse(decoder.decode(data)); + const json = await this.stateBackend.read(); this.state = { ...DEFAULT_STATE, ...json, diff --git a/src/state.ts b/src/state.ts index 414e33b..e60af2e 100644 --- a/src/state.ts +++ b/src/state.ts @@ -24,3 +24,27 @@ export const DEFAULT_STATE: State = { fileExportPath: "./export", monitorInterval: 500, }; + +export type StateBackend = { + read: () => Promise; + write: (newState: State) => Promise; +}; + +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)); + return json; + } + + async write(newState: State): Promise { + const encoder = new TextEncoder(); + const data = encoder.encode(JSON.stringify(newState, undefined, 2)); + const swapPath = `${this.path}.swap`; + await Deno.writeFile(swapPath, data); + await Deno.rename(swapPath, this.path); + } +}