@protorians/widgets
Version:
Create your web user interfaces with widgets
18 lines (17 loc) • 546 B
JavaScript
export function useStateful(composite, dependencies) {
let initial = composite(dependencies);
function update() {
const computed = composite(dependencies);
if (computed !== initial) {
initial.element.replaceWith(computed.clientElement);
initial = computed;
}
}
Object.entries(dependencies).forEach(([key, state]) => {
state.effect((current) => {
dependencies = { ...dependencies, [key]: current };
update();
});
});
return initial;
}