41 lines
1.4 KiB
JavaScript
41 lines
1.4 KiB
JavaScript
async function loadWorkspace({ document, window = null, createLoader = createFeatureLoader }) {
|
|
let cameOnline = false;
|
|
let replayed = false;
|
|
const captureOnline = () => { cameOnline = true; };
|
|
window?.addEventListener('online', captureOnline);
|
|
const status = document.querySelector('#my-work-action-status');
|
|
const url = document.querySelector(
|
|
'meta[name="stackchain-feature-today-timer"]'
|
|
)?.content || '';
|
|
const loader = createLoader({
|
|
document,
|
|
urls: { 'today-timer': url },
|
|
});
|
|
if (status) status.textContent = 'Starting workspace…';
|
|
try {
|
|
await loader.load('today-timer');
|
|
if (status) status.textContent = '';
|
|
return {
|
|
replayOnline(callback) {
|
|
if (replayed) return;
|
|
replayed = true;
|
|
window?.removeEventListener('online', captureOnline);
|
|
if (cameOnline) callback();
|
|
},
|
|
};
|
|
} catch (error) {
|
|
window?.removeEventListener('online', captureOnline);
|
|
if (window?.location?.reload) {
|
|
if (cameOnline) window.location.reload();
|
|
else window.addEventListener('online', () => window.location.reload(), { once: true });
|
|
}
|
|
if (status) {
|
|
status.textContent = window
|
|
? 'Workspace could not load. Reconnect to retry automatically, or reload now.'
|
|
: 'Workspace could not load. Check your connection, then reload to retry.';
|
|
}
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
if (typeof module !== 'undefined' && module.exports) module.exports = loadWorkspace; |