50 lines
1.2 KiB
JavaScript
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;
|