From 695b02caaa3677d62da3d1441d9af49965eacac6 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Wed, 26 May 2021 18:43:11 +0200 Subject: [PATCH] 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. --- commands.js | 3 --- components/app.js | 29 +++++++++--------------- components/connect-form.js | 17 +++++--------- store.js | 46 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 62 insertions(+), 33 deletions(-) create mode 100644 store.js diff --git a/commands.js b/commands.js index 7dad527..0459ac6 100644 --- a/commands.js +++ b/commands.js @@ -132,9 +132,6 @@ export default { "quit": { description: "Quit", execute: (app, args) => { - if (window.localStorage) { - localStorage.removeItem("autoconnect"); - } app.close({ name: SERVER_BUFFER }); }, }, diff --git a/components/app.js b/components/app.js index 7e47fe1..1a10e85 100644 --- a/components/app.js +++ b/components/app.js @@ -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: diff --git a/components/connect-form.js b/components/connect-form.js index 831129e..9104896 100644 --- a/components/connect-form.js +++ b/components/connect-form.js @@ -74,17 +74,6 @@ export default class ConnectForm extends Component { } render() { - var rememberMe = null; - if (window.localStorage) { - rememberMe = html` - -

- `; - } - return html`

Connect to IRC

@@ -101,7 +90,11 @@ export default class ConnectForm extends Component {

- ${rememberMe} + +

Advanced options diff --git a/store.js b/store.js new file mode 100644 index 0000000..4258e0b --- /dev/null +++ b/store.js @@ -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)); + }, +};