forked from tools/josh
1
0
Fork 0

Move entries to localstorage where applicable

Fixes #135.
This commit is contained in:
Florine W. Dekker 2020-08-10 20:23:04 +02:00
parent 22c60dbbbd
commit 72826745b6
Signed by: FWDekker
GPG Key ID: B1B567AF58D6EE0F
3 changed files with 64 additions and 62 deletions

View File

@ -1,6 +1,6 @@
{
"name": "fwdekker.com",
"version": "0.37.3",
"version": "0.37.4",
"description": "The source code of [my personal website](https://fwdekker.com/).",
"author": "Felix W. Dekker",
"browser": "dist/bundle.js",

View File

@ -59,6 +59,6 @@
});
}
</script>
<script type="module" src="bundle.js"></script>
<script type="module" src="bundle.js?v=%%VERSION_NUMBER%%"></script>
</body>
</html>

View File

@ -13,9 +13,11 @@ export class Persistence {
* Removes all persistent storage.
*/
static reset(): void {
sessionStorage.clear();
localStorage.clear();
Cookies.remove("env");
localStorage.removeItem("//files");
localStorage.removeItem("//history");
localStorage.removeItem("//version");
sessionStorage.removeItem("//env");
sessionStorage.removeItem("//has-updated");
Cookies.remove("poweroff");
}
@ -29,7 +31,7 @@ export class Persistence {
* failed.
*/
static getFileSystem(): FileSystem {
const fileString = localStorage.getItem("files");
const fileString = localStorage.getItem("//files");
if (fileString !== null) {
try {
const parsedFiles = Node.deserialize(fileString);
@ -38,7 +40,7 @@ export class Persistence {
else
console.warn("'files' cookie contains non-directory.");
} catch (error) {
console.warn("Failed to deserialize 'files' cookie.", error);
console.warn("Failed to deserialize 'files' storage.", error);
}
}
@ -51,7 +53,7 @@ export class Persistence {
* @param fileSystem the file system to persist
*/
static setFileSystem(fileSystem: FileSystem): void {
localStorage.setItem("files", JSON.stringify(fileSystem.root));
localStorage.setItem("//files", JSON.stringify(fileSystem.root));
}
/**
@ -59,9 +61,9 @@ export class Persistence {
*/
static getHistory(): InputHistory {
try {
return new InputHistory(JSON.parse(localStorage.getItem("history") ?? "[]"));
return new InputHistory(JSON.parse(localStorage.getItem("//history") ?? "[]"));
} catch (error) {
console.warn("Failed to deserialize 'history' cookie.", error);
console.warn("Failed to deserialize 'history' storage.", error);
return new InputHistory();
}
}
@ -72,7 +74,54 @@ export class Persistence {
* @param history the history to persist
*/
static setHistory(history: InputHistory): void {
localStorage.setItem("history", JSON.stringify(history.entries));
localStorage.setItem("//history", JSON.stringify(history.entries));
}
/**
* Returns the version of the scripts that were used the last time the user visited the website.
*/
static getVersion(): string {
return localStorage.getItem("//version") ?? "%%VERSION_NUMBER%%";
}
/**
* Sets the version of the scripts that were used the last time the user visited the website.
*
* @param version the version of the scripts that were used the last time the user visited the website
*/
static setVersion(version: string) {
localStorage.setItem("//version", version);
}
///
/// Short-term storage
///
/**
* Returns `true` if and only if the server is "turned off".
*/
static getPoweroff(): boolean {
try {
return JSON.parse(Cookies.get("poweroff") ?? "false");
} catch (error) {
console.warn("Failed to deserialize 'poweroff' cookie.", error);
return false;
}
}
/**
* Stores whether the server is "turned off".
*
* @param value whether the server is "turned off"
*/
static setPoweroff(value: boolean): void {
Cookies.set("poweroff", "" + value, {
expires: new Date(new Date().setSeconds(new Date().getSeconds() + 30)),
path: "/",
secure: true,
sameSite: "lax"
});
}
/**
@ -82,7 +131,7 @@ export class Persistence {
* @param userList the list of users used to validate the `user` environment variable
*/
static getEnvironment(userList: UserList): Environment {
const environmentString = Cookies.get("env") ?? "{}";
const environmentString = sessionStorage.getItem("//env") ?? "{}";
let environment: Environment;
try {
@ -119,54 +168,7 @@ export class Persistence {
* @param environment the environment to persist
*/
static setEnvironment(environment: Environment): void {
Cookies.set("env", environment.variables, {path: "/", secure: true, sameSite: "lax"});
}
/**
* Returns the version of the scripts that were used the last time the user visited the website.
*/
static getVersion(): string {
return localStorage.getItem("version") ?? "%%VERSION_NUMBER%%";
}
/**
* Sets the version of the scripts that were used the last time the user visited the website.
*
* @param version the version of the scripts that were used the last time the user visited the website
*/
static setVersion(version: string) {
localStorage.setItem("version", version);
}
///
/// Short-term storage
///
/**
* Returns `true` if and only if the server is "turned off".
*/
static getPoweroff(): boolean {
try {
return JSON.parse(Cookies.get("poweroff") ?? "false");
} catch (error) {
console.warn("Failed to deserialize 'poweroff' cookie.", error);
return false;
}
}
/**
* Stores whether the server is "turned off".
*
* @param value whether the server is "turned off"
*/
static setPoweroff(value: boolean): void {
Cookies.set("poweroff", "" + value, {
expires: new Date(new Date().setSeconds(new Date().getSeconds() + 30)),
path: "/",
secure: true,
sameSite: "lax"
});
sessionStorage.setItem("//env", JSON.stringify(environment.variables));
}
/**
@ -174,7 +176,7 @@ export class Persistence {
*/
static getWasUpdated(): boolean {
try {
return JSON.parse(sessionStorage.getItem("has-updated") ?? "false");
return JSON.parse(sessionStorage.getItem("//has-updated") ?? "false");
} catch (error) {
console.warn("Failed to deserialize 'poweroff' cookie.", error);
return false;
@ -187,6 +189,6 @@ export class Persistence {
* @param value whether the terminal was updated in this session
*/
static setWasUpdated(value: boolean): void {
sessionStorage.setItem("has-updated", "" + value);
sessionStorage.setItem("//has-updated", "" + value);
}
}