Commit 9312e9f5 authored by Romain Casati's avatar Romain Casati
Browse files

GUI: state saved in storage.

parent adb510f7
import { PromiseDelegate } from "promise-delegate";
import { CheckpointsManager } from "@basthon/checkpoints";
import { CheckpointsManager, Storage } from "@basthon/checkpoints";
import { KernelBase } from "@basthon/kernel-base";
import { KernelLoader } from "@basthon/kernel-loader";
......@@ -30,6 +30,7 @@ export class GUIBase {
protected _contentFilename: string = "content.txt";
protected _urlKey: string = "content";
private _extensions: Map<string, (() => Promise<void>)> = new Map();
protected _stateStorage: Storage<boolean>;
/* console errors redirected to notification system */
private _console_error: (message: string) => void = console.error;
......@@ -43,6 +44,8 @@ export class GUIBase {
if (!options.noCheckpointsInit)
this._checkpoints = new CheckpointsManager<string>(
`${options.uiName}.${options.language}`, this._maxCheckpoints);
/* globale states (not language specific) */
this._stateStorage = new Storage(`${options.uiName}.states`);
}
/**
......@@ -224,8 +227,23 @@ export class GUIBase {
kernel.addEventListener("eval.error", () => {
this.validateBackup();
});
/* instanciate the storage */
await this._stateStorage.ready();
}
/**
* Get state from strorage.
*/
protected async getState(state: string, def?: any): Promise<any> {
return await this._stateStorage.get(state) ?? def;
}
/**
* Set state in storage.
*/
protected async setState(state: string, value: any) {
await this._stateStorage.set(state, value);
}
/**
* Load content at startup.
*/
......@@ -361,7 +379,7 @@ export class GUIBase {
/**
* Get mode as a string (dark/light).
*/
public theme(): "dark" | "light" | undefined { return; }
public async theme(): Promise<"dark" | "light" | undefined> { return; }
/**
* Restart the kernel.
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment