uhm/src/app.js

39 lines
874 B
JavaScript
Raw Normal View History

2024-05-05 13:07:17 +00:00
export default function app(opts) {
2024-05-05 00:34:27 +00:00
// initial setup
2024-05-05 13:07:17 +00:00
let data = check(opts.state, {});
let view = check(opts.view, () => null);
let actions = check(opts.actions, {});
let mount = opts.mount || "body";
// check set or default
function check(value, type) {
return typeof value === typeof type ? value : type;
}
2024-05-05 00:34:27 +00:00
// state helper
const state = (state) => {
if(typeof state === "object") {
data = {...data, ...state};
}
// update ui
update();
2024-05-05 00:34:27 +00:00
// return current state
return data;
}
const update = () => {
2024-05-05 13:07:17 +00:00
document.querySelector(mount).replaceChildren(view(data, actions));
2024-05-05 00:34:27 +00:00
}
// mount view
2024-05-05 13:07:17 +00:00
if (opts.view && mount) {
update();
2024-05-05 00:34:27 +00:00
}
return {
state,
actions,
2024-05-05 13:07:17 +00:00
}
}