okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-027-web: the served web chat page for 0.2.75d9f47922

main
3.3 KiB66 linesraw
1// serve/web/chatstore.js - the saved chats, kept in this browser (IndexedDB, database "strata", store "chats", one record per
2// chat). Ported from PR #1787 by Scotty-Hudson. A chat can be several megabytes and localStorage holds about 5 MB for the whole
3// page, so the chats live here and only small keys (which chat is open, the settings) stay in localStorage. Nothing goes to the
4// server: the server has no endpoint for chats and never sees a title.
5//
6// Where the browser will not keep it (a private window, a blocked or full store) the list lives in memory for the visit, and
7// onProblem(kind) tells the page once: "memory" (no store), "quota" (full), "failed" (a write the browser refused).
8"use strict";
9(function (root) {
10  function createChatStore({indexedDB: idb = root.indexedDB, onProblem = () => {}} = {}) {
11    let db = null, told = false;
12    const problem = (kind) => { if (!told) { told = true; onProblem(kind); } };
13
14    function open() {
15      return new Promise((resolve) => {
16        let r;
17        try { r = idb.open("strata", 1); } catch (e) { resolve(null); return; }       // private mode: memory only
18        r.onupgradeneeded = () => {
19          const d = r.result;
20          if (!d.objectStoreNames.contains("chats")) d.createObjectStore("chats", {keyPath: "id"});
21        };
22        // a database already at version 1 does not run onupgradeneeded, so the store may not be there
23        r.onsuccess = () => resolve(r.result.objectStoreNames.contains("chats") ? r.result : null);
24        r.onerror = () => resolve(null);
25        r.onblocked = () => resolve(null);          // another tab holds an older version of the database
26      });
27    }
28    // true when the store is there; false: memory only (and the page has been told)
29    async function start() {
30      db = await open();
31      if (!db) problem("memory");
32      return !!db;
33    }
34    function all() {
35      return new Promise((resolve) => {
36        if (!db) { resolve([]); return; }
37        // the result is on the request, not on the store: reading it at oncomplete gives nothing
38        const req = db.transaction("chats", "readonly").objectStore("chats").getAll();
39        req.onsuccess = () => resolve(req.result || []);
40        req.onerror = () => resolve([]);
41        req.transaction.onabort = () => resolve([]);        // an aborted read fires abort, not error: do not hang
42      });
43    }
44    // a request that fails because the storage is full aborts the transaction, so it fires abort rather than error
45    function watch(t) {
46      const warn = () => problem(t.error && t.error.name === "QuotaExceededError" ? "quota" : "failed");
47      t.onerror = warn; t.onabort = warn;
48    }
49    function put(chat) {                                  // one record per chat: not the whole list for one message
50      if (!db) return;
51      const t = db.transaction("chats", "readwrite");
52      t.objectStore("chats").put(chat);
53      watch(t);
54    }
55    function remove(id) {
56      if (!db) return;
57      const t = db.transaction("chats", "readwrite");
58      t.objectStore("chats").delete(id);
59      watch(t);
60    }
61    return {start, all, put, remove, get usable() { return !!db; }};
62  }
63  const api = {createChatStore};
64  root.StrataChatStore = api;
65  if (typeof module !== "undefined" && module.exports) module.exports = api;
66})(typeof globalThis !== "undefined" ? globalThis : this);