Skip to content

Instantly share code, notes, and snippets.

@derekmc
Last active March 28, 2025 17:18
Show Gist options
  • Save derekmc/ff24e85b6fea9693ad61741f45d4d460 to your computer and use it in GitHub Desktop.
Save derekmc/ff24e85b6fea9693ad61741f45d4d460 to your computer and use it in GitHub Desktop.
Global Structured Variable Wrappers
Each different wrapper contains different variables based on
their use in the app's lifecycle:
data - Global saved state
temp - Global unsaved state, may be reset during runtime
ref - Runtime objects, not reset unlike temp
env - Runtime constants, not modified
data0, temp0 - Default initial values for data and temp
This project is an example of one possible way to
implement a minimal application structure that
is still standardized and easy to debug.
==== js/main.js ====
let data = {}
let temp = {}
let ref = {}
let env = {
"tempTimeout": 15 * 1000,
"dataKey": "__APP_DATA__",
"saveInterval": 5 * 1000,
}
let data0 = {
}
let temp0 = {
}
window.addEventListener('load', main)
function load(){
data = localStorage.getItem(read(env.dataKey))
}
function save(){
localStorage.setItem(write(env.dataKey), data)
}
function main(){
load()
setInterval(save, env.saveInterval)
init()
resize()
draw()
}
function init(){
for(let k of data0){
if(!data.hasOwnProperty(k))
data[k] = copy(data0[k]) }
}
function delay(f, t){
let name = "delay-callback:" + f
if(ref.hasOwnProperty(name)){
clearTimeout(ref[name])}
ref[name] = setTimeout(f, t)
}
function tempReset(){
temp = copy(temp0)
}
function resize(){
}
function id(x){
return document.getElementById(x)
}
function copy(x){
return JSON.parse(JSON.stringify(x))
}
function read(x){
return JSON.parse(x)
}
function write(x){
return JSON.stringify(x)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment