Define Data interface for DataService
This commit is contained in:
+11
-4
@@ -1,15 +1,22 @@
|
|||||||
|
interface Data {
|
||||||
|
limboMode: boolean
|
||||||
|
}
|
||||||
|
|
||||||
export default class DataService {
|
export default class DataService {
|
||||||
private static file: Bun.BunFile | undefined;
|
private static file: Bun.BunFile | undefined;
|
||||||
private static _data: Record<string, unknown> = {};
|
private static _data: Data = {
|
||||||
|
// default values (will be overwritten)
|
||||||
|
limboMode: false
|
||||||
|
};
|
||||||
|
|
||||||
static async load() {
|
static async load() {
|
||||||
this.file = Bun.file("./data.json");
|
this.file = Bun.file("./data.json");
|
||||||
if (!(await this.file.exists()))
|
if (!(await this.file.exists()))
|
||||||
await Bun.write(this.file, JSON.stringify({}, null, 2));
|
await Bun.write(this.file, JSON.stringify(this._data, null, 2));
|
||||||
this._data = await this.file.json();
|
this._data = { ...this._data, ...(await this.file.json()) };
|
||||||
}
|
}
|
||||||
|
|
||||||
static set data(data: Record<string, unknown>) {
|
static set data(data: Data) {
|
||||||
if (!this.file) throw new Error("DataService is not loaded. Please call DataService.load()");
|
if (!this.file) throw new Error("DataService is not loaded. Please call DataService.load()");
|
||||||
this._data = data;
|
this._data = data;
|
||||||
void Bun.write(this.file, JSON.stringify(this._data, null, 2));
|
void Bun.write(this.file, JSON.stringify(this._data, null, 2));
|
||||||
|
|||||||
Reference in New Issue
Block a user