LifeMate — Personal, Family & Learning Companion

fix/sms-approval-gates
chore/phase1-repository-cleanup deploy/stable-web-apk deployment-static design/mobile-accessibility-v2 design/mobile-navigation-review docs/child-education-access-decision docs/offline-data-rescue-options docs/owner-guide-and-delivery-plan docs/phase1-policy-options docs/public-review-live-2026-10-15 feat/aparat-learning-library feat/child-device-safety feat/child-identity-schema feat/flutter-mobile-navigation feat/learning-content-operations feat/local-signing-preflight feat/online-family-stage0 feat/personal-encrypted-recovery feat/private-planner-series-api feat/public-review-page feat/standalone-weekly-planner feat/weekly-planner-android final-feature-1/iran-family-school fix/auth-abuse-expiry-safety fix/auth-expiry-after-lock fix/backend-request-safety fix/lifeguide-bootstrap-naming-ci fix/lifeguide-pages-qa-base-preflight fix/lifeguide-qa-pages-env-deployment fix/local-task-persistence fix/managed-child-session-boundary fix/personal-jalali-dates fix/personal-notification-controls fix/personal-upgrade-delivery-guard fix/sms-approval-gates fix/study-duration-display infra/cloudflare-neon-migration infra/lifeguide-cloudflare-qa infra/lifeguide-github-pages-qa infra/lifeguide-supabase-qa main phase-1/foundation-completion phase-2/product-foundation-identity phase-3/planner-school-family-core phase-4/ai-learning-wellbeing phase-5/hardening-observability-release test/android-reminder-acceptance test/personal-notification-settings test/personal-recovery-acceptance test/role-entry-apk
js 105 lines 4.2 KB
Raw
1 {{flutter_js}}
2 {{flutter_build_config}}
3
4 const localBase = new URL('.', document.baseURI);
5 const engineConfig = {
6 canvasKitBaseUrl: new URL('canvaskit/', localBase).href,
7 fontFallbackBaseUrl: new URL('assets/fonts/fallback/', localBase).href,
8 };
9 const showFailure = () => {
10 const status = document.getElementById('startup-status');
11 if (status) status.textContent =
12 'لایف‌گاید بارگذاری نشد. اتصال را بررسی کنید و صفحه را دوباره باز کنید.';
13 };
14
15 // A document must own the origin-wide lock before Flutter can read credentials
16 // or personal caches. Keep the callback pending for the entire document lifetime.
17 // Browser teardown/crash releases the lock; backgrounding must not release it.
18 let instanceOwned = false;
19 let documentInvalidated = false;
20 let requestInFlight = false;
21 Object.defineProperty(window, 'lifeguideInstanceOwned', {get: () => instanceOwned});
22
23 window.addEventListener('pagehide', () => {
24 documentInvalidated = true;
25 instanceOwned = false;
26 });
27 window.addEventListener('pageshow', (event) => {
28 // A cached engine must never resume with a stale ownership assertion. Keep
29 // the old lock held until this document is replaced by a fresh bootstrap.
30 if (event.persisted || documentInvalidated) window.location.reload();
31 });
32
33 function showInstanceNotice(unsupported) {
34 const status = document.getElementById('startup-status');
35 if (!status) return;
36 status.setAttribute('role', 'alert');
37 status.textContent = unsupported
38 ? 'این مرورگر از اجرای امن لایف‌گاید پشتیبانی نمی‌کند. از یک مرورگر به‌روز و سازگار استفاده کنید.'
39 : 'لایف‌گاید در پنجره دیگری باز است. برای ادامه، آن پنجره را ببندید و دوباره تلاش کنید.';
40 let retry = document.getElementById('startup-retry');
41 if (!retry) {
42 retry = document.createElement('button');
43 retry.id = 'startup-retry';
44 retry.type = 'button';
45 retry.textContent = 'تلاش دوباره';
46 retry.style.cssText = 'display:block;margin:16px auto;padding:12px 24px;font:inherit;cursor:pointer';
47 retry.addEventListener('click', startInstance);
48 status.after(retry);
49 }
50 }
51
52 // Retire the legacy generated worker, which could serve stale runtime settings.
53 async function prepareWorker() {
54 if (!('serviceWorker' in navigator)) return;
55 try {
56 const registrations = await navigator.serviceWorker.getRegistrations();
57 for (const registration of registrations) {
58 const script = (registration.active || registration.waiting || registration.installing)?.scriptURL;
59 if (script && new URL(script).pathname.endsWith('/flutter_service_worker.js')) {
60 await registration.unregister();
61 }
62 }
63 await navigator.serviceWorker.register(new URL('lifeguide-sw.js', localBase), {updateViaCache: 'none'});
64 } catch (_) { /* Network-only startup still works when worker setup fails. */ }
65 }
66
67 async function startInstance() {
68 if (requestInFlight) return;
69 if (documentInvalidated) { window.location.reload(); return; }
70 if (!window.isSecureContext || !navigator.locks?.request) {
71 showInstanceNotice(true);
72 return;
73 }
74 requestInFlight = true;
75 document.getElementById('startup-retry')?.remove();
76 try {
77 await navigator.locks.request('lifeguide.active-instance.v1',
78 {mode: 'exclusive', ifAvailable: true}, async (lock) => {
79 if (!lock) { showInstanceNotice(false); return; }
80 if (documentInvalidated) return;
81 instanceOwned = true;
82 prepareWorker();
83 _flutter.loader.load({
84 config: engineConfig,
85 onEntrypointLoaded: async (initializer) => {
86 try {
87 if (!instanceOwned) return;
88 const runner = await initializer.initializeEngine(engineConfig);
89 if (!instanceOwned) return;
90 await runner.runApp();
91 document.getElementById('startup-status')?.remove();
92 } catch (_) { showFailure(); }
93 },
94 }).catch(showFailure);
95 await new Promise(() => {});
96 });
97 } catch (_) {
98 instanceOwned = false;
99 showInstanceNotice(true);
100 } finally {
101 requestInFlight = false;
102 }
103 }
104
105 startInstance();