uhm/src/app.js

53 lines
1.3 KiB
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;
}
2024-05-05 19:04:45 +00:00
// starts app
function dispatch(input, actions) {
Object.entries(actions).forEach(([name, action]) => {
if (typeof action === "function") {
actions[name] = (...args) => {
// update date from action return
Object.assign(data, action(input, ...args));
// call update
update();
};
}
});
update();
}
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) {
2024-05-05 19:04:45 +00:00
dispatch(data, actions);
2024-05-05 00:34:27 +00:00
}
2024-05-05 19:04:45 +00:00
return {state}
2024-05-05 13:07:17 +00:00
}