23 lines
740 B
JavaScript
23 lines
740 B
JavaScript
function createSrSessionPreparation({ ensureCurrentUser, allowsCrmActivities, sendRequest, reportProgress, appendLog, onReady = async () => {} }) {
|
|
let pending = null;
|
|
function ensure() {
|
|
if (pending) return pending;
|
|
pending = (async () => {
|
|
try {
|
|
await ensureCurrentUser();
|
|
if (!allowsCrmActivities()) return;
|
|
reportProgress("running");
|
|
const result = await sendRequest();
|
|
appendLog(result?.debugRequests || []);
|
|
if (!result?.ok) throw new Error(result?.error || "Session initialization failed.");
|
|
reportProgress("complete");
|
|
await onReady();
|
|
} catch {
|
|
reportProgress("error");
|
|
}
|
|
})();
|
|
return pending;
|
|
}
|
|
return { ensure };
|
|
}
|