Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
342ca8c5fc | |||
869b406323 | |||
cec0803aff |
10
graph/index.html
Normal file
10
graph/index.html
Normal file
@ -0,0 +1,10 @@
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Document</title>
|
||||
</head>
|
||||
<body>
|
||||
<script src="./storage.js"></script>
|
||||
</body>
|
||||
</html>
|
69
graph/storage.js
Normal file
69
graph/storage.js
Normal file
@ -0,0 +1,69 @@
|
||||
/** @typedef {{Save:(key:string, data:object)=>void, Load:(key:string)=>object, ID:string, List:Record<string, object>, Dump:()=>void}} SaveLoad<T=object> */
|
||||
|
||||
/** @type {Record<string, SaveLoad>} */
|
||||
const Master = {};
|
||||
const Prefix = "SL:";
|
||||
const Delimiter = "_"
|
||||
/** @type {(keyOuter:string)=>SaveLoad} */
|
||||
function SaveLoad(keyOuter)
|
||||
{
|
||||
/** @type {SaveLoad} */
|
||||
const sl = {
|
||||
ID: keyOuter,
|
||||
Save(key, data)
|
||||
{
|
||||
localStorage.setItem(Prefix+keyOuter+Delimiter+key, JSON.stringify(data));
|
||||
},
|
||||
Load(key)
|
||||
{
|
||||
const obj = JSON.parse( localStorage.getItem(Prefix+keyOuter+Delimiter+key) || "{}" );
|
||||
this.List[key] = obj;
|
||||
return obj;
|
||||
},
|
||||
List:{},
|
||||
Dump(){this.List = {};}
|
||||
};
|
||||
|
||||
Master[keyOuter] = sl;
|
||||
return sl;
|
||||
}
|
||||
|
||||
/** @type {()=>void} */
|
||||
function MassLoad()
|
||||
{
|
||||
let loaded = 0;
|
||||
let scanned = 0;
|
||||
for (let i = 0; i < localStorage.length; i++) {
|
||||
|
||||
scanned++;
|
||||
|
||||
const fullKey = localStorage.key(i) || "";
|
||||
|
||||
if(fullKey.substring(0, Prefix.length) == Prefix)
|
||||
{
|
||||
const delim = fullKey.indexOf(Delimiter);
|
||||
const typeKey = fullKey.substring(Prefix.length, delim);
|
||||
const type = Master[typeKey];
|
||||
if(type)
|
||||
{
|
||||
const actualKey = fullKey.substring(delim+1);
|
||||
type.Load(actualKey) && loaded++;
|
||||
}
|
||||
}
|
||||
}
|
||||
console.log("mass load complete. loaded:", loaded, "scanned:", scanned);
|
||||
}
|
||||
|
||||
const Node = SaveLoad("Node");
|
||||
const Link = SaveLoad("Link");
|
||||
MassLoad();
|
||||
console.log(Node.List);
|
||||
console.log(Link.List);
|
||||
|
||||
|
||||
|
||||
Node.Save("n1", {name:"n1"});
|
||||
Node.Save("n2", {name:"n2"});
|
||||
Link.Save("l1", ["n1", "n2", {time:123}]);
|
||||
|
||||
Node.Save("n1", {name:"n1", updated:true});
|
39
graph/walker.js
Normal file
39
graph/walker.js
Normal file
@ -0,0 +1,39 @@
|
||||
|
||||
function Walk(obj)
|
||||
{
|
||||
const set = new Set();
|
||||
function Step(obj, path="")
|
||||
{
|
||||
if(obj !== null && typeof obj === "object")
|
||||
{
|
||||
if(set.has(obj))
|
||||
{
|
||||
console.log("ive been to", obj, "quitting");
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
set.add(obj);
|
||||
console.log("ive never seen", obj, "here are its fields:");
|
||||
console.log("-----------------");
|
||||
for(let key in obj)
|
||||
{
|
||||
const val = obj[key];
|
||||
console.log(" >stepping into", path+"."+key);
|
||||
Step(val, path+"."+key);
|
||||
}
|
||||
console.log("-----------------");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
console.log(obj, "is a leaf");
|
||||
}
|
||||
}
|
||||
Step(obj);
|
||||
}
|
||||
|
||||
|
||||
const root = {hey:"1", deep:{basement:{closet:{item1:"one", item2:"two"}}}, arr:["a", "b"]};
|
||||
root.circ = root;
|
||||
Walk(root);
|
Loading…
Reference in New Issue
Block a user