Introduce store helper

Responsible for serializing/deserializing data to be saved in
localStorage. Add a prefix to all localStorage entries to avoid
conflicts with other webapps. Stop guarding against localStorage
not existing, browsers can just implement a dumb interface to
disable it.
This commit is contained in:
Simon Ser 2021-05-26 18:43:11 +02:00
parent 12a38ace90
commit 695b02caaa
4 changed files with 62 additions and 33 deletions

View file

@ -132,9 +132,6 @@ export default {
"quit": {
description: "Quit",
execute: (app, args) => {
if (window.localStorage) {
localStorage.removeItem("autoconnect");
}
app.close({ name: SERVER_BUFFER });
},
},

View file

@ -16,6 +16,7 @@ import { strip as stripANSI } from "../lib/ansi.js";
import { SERVER_BUFFER, BufferType, ReceiptType, NetworkStatus, Unread } from "../state.js";
import commands from "../commands.js";
import { setup as setupKeybindings } from "../keybindings.js";
import * as store from "../store.js";
const configPromise = fetch("../config.json")
.then((resp) => {
@ -193,8 +194,8 @@ export default class App extends Component {
this.saveReceipts = debounce(this.saveReceipts.bind(this), 500);
if (window.localStorage && localStorage.getItem("autoconnect")) {
var connectParams = JSON.parse(localStorage.getItem("autoconnect"));
var connectParams = store.autoconnect.load();
if (connectParams) {
this.state.connectParams = {
...this.state.connectParams,
...connectParams,
@ -202,10 +203,7 @@ export default class App extends Component {
};
}
if (window.localStorage && localStorage.getItem("receipts")) {
var obj = JSON.parse(localStorage.getItem("receipts"));
this.receipts = new Map(Object.entries(obj));
}
this.receipts = store.receipts.load();
configPromise.then((config) => {
this.handleConfig(config);
@ -376,10 +374,7 @@ export default class App extends Component {
}
saveReceipts() {
if (window.localStorage) {
var obj = Object.fromEntries(this.receipts);
localStorage.setItem("receipts", JSON.stringify(obj));
}
store.receipts.put(this.receipts);
}
getReceipt(target, type) {
@ -808,12 +803,10 @@ export default class App extends Component {
handleConnectSubmit(connectParams) {
this.setState({ error: null });
if (window.localStorage) {
if (connectParams.autoconnect) {
localStorage.setItem("autoconnect", JSON.stringify(connectParams));
} else {
localStorage.removeItem("autoconnect");
}
if (connectParams.autoconnect) {
store.autoconnect.put(connectParams);
} else {
store.autoconnect.put(null);
}
this.connect(connectParams);
@ -887,8 +880,8 @@ export default class App extends Component {
}
// TODO: only clear local storage if this network is stored there
if (buf.network == 1 && window.localStorage) {
localStorage.removeItem("autoconnect");
if (buf.network == 1) {
store.autoconnect.put(null);
}
break;
case BufferType.CHANNEL:

View file

@ -74,17 +74,6 @@ export default class ConnectForm extends Component {
}
render() {
var rememberMe = null;
if (window.localStorage) {
rememberMe = html`
<label>
<input type="checkbox" name="rememberMe" checked=${this.state.rememberMe} disabled=${this.props.disabled}/>
Remember me
</label>
<br/><br/>
`;
}
return html`
<form onChange=${this.handleChange} onSubmit=${this.handleSubmit}>
<h2>Connect to IRC</h2>
@ -101,7 +90,11 @@ export default class ConnectForm extends Component {
</label>
<br/><br/>
${rememberMe}
<label>
<input type="checkbox" name="rememberMe" checked=${this.state.rememberMe} disabled=${this.props.disabled}/>
Remember me
</label>
<br/><br/>
<details>
<summary>Advanced options</summary>

46
store.js Normal file
View file

@ -0,0 +1,46 @@
const PREFIX = "gamja_";
function getItem(k) {
k = PREFIX + k;
}
function setItem(k, v) {
k = PREFIX + k;
}
class Item {
constructor(k) {
this.k = PREFIX + k;
}
load() {
var v = localStorage.getItem(this.k);
if (!v) {
return null;
}
return JSON.parse(v);
}
put(v) {
if (v) {
localStorage.setItem(this.k, JSON.stringify(v));
} else {
localStorage.removeItem(this.k);
}
}
}
export const autoconnect = new Item("autoconnect");
const rawReceipts = new Item("receipts");
export const receipts = {
load() {
var v = rawReceipts.load();
return new Map(Object.entries(v || {}));
},
put(m) {
rawReceipts.put(Object.fromEntries(m));
},
};