* readd overwritten commit * Remove duplicated code * Fix status load in and failed state update * Late sidebar open bugs fixed * Change ideMessenger post parameters * debug pause * Don't use global, fix table creation bug * Creating new branch for config-related issues here * cleanup
50 lines
1.2 KiB
TypeScript
50 lines
1.2 KiB
TypeScript
import fs from "node:fs";
|
|
import { getGlobalContextFilePath } from "./paths";
|
|
import { IndexingProgressUpdate } from "..";
|
|
|
|
export type GlobalContextType = {
|
|
indexingPaused: boolean;
|
|
};
|
|
|
|
/**
|
|
* A way to persist global state
|
|
*/
|
|
export class GlobalContext {
|
|
update<T extends keyof GlobalContextType>(
|
|
key: T,
|
|
value: GlobalContextType[T],
|
|
) {
|
|
if (!fs.existsSync(getGlobalContextFilePath())) {
|
|
fs.writeFileSync(
|
|
getGlobalContextFilePath(),
|
|
JSON.stringify(
|
|
{
|
|
[key]: value,
|
|
},
|
|
null,
|
|
2,
|
|
),
|
|
);
|
|
} else {
|
|
const data = fs.readFileSync(getGlobalContextFilePath(), "utf-8");
|
|
const parsed = JSON.parse(data);
|
|
parsed[key] = value;
|
|
fs.writeFileSync(
|
|
getGlobalContextFilePath(),
|
|
JSON.stringify(parsed, null, 2),
|
|
);
|
|
}
|
|
}
|
|
|
|
get<T extends keyof GlobalContextType>(
|
|
key: T,
|
|
): GlobalContextType[T] | undefined {
|
|
if (!fs.existsSync(getGlobalContextFilePath())) {
|
|
return undefined;
|
|
}
|
|
|
|
const data = fs.readFileSync(getGlobalContextFilePath(), "utf-8");
|
|
const parsed = JSON.parse(data);
|
|
return parsed[key];
|
|
}
|
|
}
|