From d9a750f7f08c8fc37775bd20fbbd34ddf555776e Mon Sep 17 00:00:00 2001
From: Seth Trowbridge <seth111@gmail.com>
Date: Sun, 23 Feb 2025 21:30:44 -0500
Subject: [PATCH] deparate dev code include

---
 dev_server.ts | 16 +++-------------
 src/dev.js    | 32 ++++++++++++++++++++++++++++++++
 2 files changed, 35 insertions(+), 13 deletions(-)
 create mode 100644 src/dev.js

diff --git a/dev_server.ts b/dev_server.ts
index 4250307..0f426c2 100644
--- a/dev_server.ts
+++ b/dev_server.ts
@@ -3,32 +3,22 @@ import { contentType } from "jsr:@std/media-types";
 // Parse the port from the command-line arguments, defaulting to 8000
 const port = parseInt(Deno.args[0] || "8000", 10);
 const sockets: WebSocket[] = [];
-const connect = `<script>
-const ws = new WebSocket('ws://localhost:${port}/ws');
-ws.addEventListener('message', (event) => {
-  if (event.data === 'reload') {
-    window.location.reload();
-  }
-});
-ws.addEventListener('error', console.error);
-ws.addEventListener('close', console.warn);
-</script>`;
-
 const bundle = await fetch(import.meta.resolve("./bundle.js")).then(r=>r.text());
+const devinc = await fetch(import.meta.resolve("./src/dev.js")).then(r=>r.text());
 
 let html: string;
 try {
-  html = Deno.readTextFileSync("./index.html").replace("<head>", "<head>"+connect);
+  html = Deno.readTextFileSync("./index.html").replace("</head>", `<script>${devinc}</script></head>`);
 } catch (_) {
   html = `
 <!DOCTYPE html>
 <html lang="en">
     <head>
-      ${connect}
         <meta charset="UTF-8">
         <meta name="viewport" content="width=device-width, initial-scale=1.0">
         <style> * {margin: 0;padding: 0;box-sizing: border-box;}html, body {height: 100%;width: 100%;font-family: Arial, sans-serif;line-height: 1.6;}body {-webkit-font-smoothing: antialiased;-moz-osx-font-smoothing: grayscale;}img, video {max-width: 100%;height: auto;}a {text-decoration: none;color: inherit;}ul, ol {list-style: none;}button, input, textarea {font-family: inherit;font-size: inherit;line-height: inherit;border: none;background: none;padding: 0;margin: 0;outline: none;}table {border-collapse: collapse;width: 100%;}</style>
         <script>${bundle}</script>
+        <script>${devinc}</script>
     </head>
     <body></body>
 </html>`;
diff --git a/src/dev.js b/src/dev.js
new file mode 100644
index 0000000..c228d00
--- /dev/null
+++ b/src/dev.js
@@ -0,0 +1,32 @@
+// added in devmode to index.html
+new WebSocket('ws://'+window.location.host+'/ws').addEventListener('message',e=>e.data==='reload'&&window.location.reload());
+
+vanX.Store =(obj, key)=>
+{
+  let checkInit = JSON.stringify(obj);
+  let checkStore = localStorage.getItem(key+"check");
+  localStorage.setItem(key+"check", checkInit);
+  
+  let recallJSON;
+  if(checkInit == checkStore)
+  {
+    let recallText = localStorage.getItem(key);
+    try
+    {
+      recallJSON = JSON.parse(recallText) || obj;
+    }
+    catch(e)
+    {
+      recallJSON = obj;
+    }
+  }
+  else
+  {
+    
+    recallJSON = obj;
+  }
+
+  const store = vanX.reactive( recallJSON );
+  van.derive(() => localStorage.setItem(key, JSON.stringify(vanX.compact(store))));
+  return store;
+}  
\ No newline at end of file