stackchain-dashboard/frontend/conversation-action-hydrator.js
timmy f094438dab
All checks were successful
CI / lint (pull_request) Successful in 1m53s
CI / build-release (pull_request) Successful in 6s
CI / release-candidate (pull_request) Has been skipped
Load comment actions on conversation demand
2026-08-14 17:00:31 +00:00

50 lines
1.2 KiB
JavaScript

function createConversationActionHydrator({ load, activate }) {
let actions = null;
let pending = null;
const wiredRoots = new WeakSet();
function ensure() {
if (actions) return Promise.resolve(actions);
if (!pending) {
pending = load().then(() => {
actions = activate();
return actions;
}).catch(error => {
pending = null;
throw error;
});
}
return pending;
}
function show({ root, state, paint, wire, retry }) {
if (actions) {
retry.hidden = true;
if (!wiredRoots.has(root)) {
wire(actions);
wiredRoots.add(root);
}
paint(state, actions);
return Promise.resolve(true);
}
paint(state, null);
retry.hidden = true;
return ensure().then(controller => {
if (!wiredRoots.has(root)) {
wire(controller);
wiredRoots.add(root);
}
paint(state, controller);
return true;
}).catch(() => {
retry.hidden = false;
return false;
});
}
return { show, ready: () => Boolean(actions) };
}
if (typeof module !== 'undefined' && module.exports) module.exports = createConversationActionHydrator;