Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions frontend/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,28 @@ <h1 class="boot-title">LockNote</h1>
<div class="boot-track"><div class="boot-bar"></div></div>
</div>
</div>
<script>
// 首屏静态页绘制后立即显示窗口,不等待主界面 JS 加载。
(function () {
var emitted = false;
function emitReady() {
if (emitted) return;
if (!window.runtime || typeof window.runtime.EventsEmit !== 'function') {
setTimeout(emitReady, 16);
return;
}
try {
window.runtime.EventsEmit('frontend:ready');
emitted = true;
} catch (error) {
setTimeout(emitReady, 16);
}
}
requestAnimationFrame(function () {
requestAnimationFrame(emitReady);
});
})();
</script>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
Expand Down
38 changes: 27 additions & 11 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// https://github.com/JackyZhang8/locknote
// 一个简单、可靠、离线优先的桌面加密笔记软件。
// A simple, reliable, offline-first encrypted note-taking desktop app.
import { useEffect, useState } from 'react';
import { lazy, Suspense, useEffect, useState } from 'react';
import { useStore } from './store';
import { LockScreen } from './components/LockScreen';
import { MainLayout } from './components/MainLayout';
import { OnboardingScreen } from './components/OnboardingScreen';
import { SetupScreen } from './components/SetupScreen';
import { StartupProgressScreen } from './components/StartupProgressScreen';
Expand All @@ -13,6 +12,9 @@ import { EventsEmit, EventsOn } from '../wailsjs/runtime/runtime';
import { flushEditor } from './editorFlush';
import { formatMessage, useI18n } from './i18n';

const loadMainLayout = () => import('./components/MainLayout');
const MainLayout = lazy(() => loadMainLayout().then((m) => ({ default: m.MainLayout })));

// Extend Window interface for Wails runtime
declare global {
interface Window {
Expand Down Expand Up @@ -62,6 +64,7 @@ async function waitForCoreReady(
onStatus: ((message: string) => void) | undefined,
messages: { preparingData: string; failed: string },
): Promise<void> {
let preparationMessageShown = false;
while (true) {
const ready = await App.IsCoreReady();
if (ready) return;
Expand All @@ -70,8 +73,11 @@ async function waitForCoreReady(
onStatus?.(formatMessage(messages.failed, { error: startupError }));
throw new Error(startupError);
}
onStatus?.(messages.preparingData);
await new Promise(resolve => setTimeout(resolve, 300));
if (!preparationMessageShown) {
onStatus?.(messages.preparingData);
preparationMessageShown = true;
}
await new Promise(resolve => setTimeout(resolve, 50));
}
}

Expand Down Expand Up @@ -108,6 +114,12 @@ function AppRoot() {
const [startupProgress, setStartupProgress] = useState(8);
const [showOnboarding, setShowOnboarding] = useState(false);

useEffect(() => {
if (!loading) {
void loadMainLayout();
}
}, [loading]);

useEffect(() => {
const suppressDefaultContextMenu = (event: MouseEvent) => {
event.preventDefault();
Expand Down Expand Up @@ -141,14 +153,14 @@ function AppRoot() {
setFirstRun(firstRun);
setShowOnboarding(firstRun);

const unlocked = await App.IsUnlocked();
setUnlocked(unlocked);

setStartupProgress(84);
const version = await App.GetVersion();
const [unlocked, version, dataDir] = await Promise.all([
App.IsUnlocked(),
App.GetVersion(),
App.GetDataDir(),
]);
setUnlocked(unlocked);
setVersion(version);

const dataDir = await App.GetDataDir();
setDataDir(dataDir);

if (firstRun) {
Expand Down Expand Up @@ -321,7 +333,11 @@ function AppRoot() {
return <LockScreen />;
}

return <MainLayout />;
return (
<Suspense fallback={<div className="h-screen bg-background" />}>
<MainLayout />
</Suspense>
);
}

export default AppRoot;
31 changes: 30 additions & 1 deletion frontend/src/appStartup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,24 @@ import assert from 'node:assert/strict';
import { readFileSync } from 'node:fs';

const source = readFileSync('src/App.tsx', 'utf8');
const htmlSource = readFileSync('index.html', 'utf8');
const onboardingSource = readFileSync('src/components/OnboardingScreen.tsx', 'utf8');
const startupProgressSource = readFileSync('src/components/StartupProgressScreen.tsx', 'utf8');

test('index.html emits frontend readiness after the boot splash gets its first frames', () => {
const splashStart = htmlSource.indexOf('<div id="boot-splash"');
const rootStart = htmlSource.indexOf('<div id="root"></div>');
const readyScriptStart = htmlSource.indexOf('<script>', splashStart);
const readyScript = htmlSource.slice(readyScriptStart, rootStart);

assert.equal(splashStart >= 0 && readyScriptStart > splashStart && rootStart > readyScriptStart, true);
assert.equal(/requestAnimationFrame\(function \(\) \{\s*requestAnimationFrame\(emitReady\);\s*\}\);/.test(readyScript), true);
assert.equal(/window\.runtime\.EventsEmit\('frontend:ready'\)/.test(readyScript), true);
assert.equal(/typeof window\.runtime\.EventsEmit !== 'function'[\s\S]*setTimeout\(emitReady, 16\)/.test(readyScript), true);
assert.equal(/var emitted = false;[\s\S]*if \(emitted\) return;[\s\S]*emitted = true;/.test(readyScript), true);
assert.equal(/try \{[\s\S]*\} catch \(error\) \{[\s\S]*setTimeout\(emitReady, 16\);/.test(readyScript), true);
});

test('App waits for Wails runtime without falling through to the first-run screen on Windows cold start', () => {
assert.equal(/async function waitForWailsRuntime/.test(source), true);
assert.equal(/while \(!isWailsRuntimeReady\(\)\) \{\s*await new Promise\(resolve => setTimeout\(resolve, 100\)\);/.test(source), true);
Expand All @@ -22,9 +37,23 @@ test('App waits for background core initialization before calling first-run APIs
assert.equal(/App\.GetStartupError/.test(source), true);
assert.equal(source.indexOf('await waitForCoreReady(setStartupMessage, t.startup);') < source.indexOf('const firstRun = await App.IsFirstRun();'), true);
assert.equal(/while \(true\)/.test(coreReadySource), true);
assert.equal(/await new Promise\(resolve => setTimeout\(resolve, 300\)\)/.test(coreReadySource), true);
assert.equal(/await new Promise\(resolve => setTimeout\(resolve, 50\)\)/.test(coreReadySource), true);
assert.equal(/let preparationMessageShown = false/.test(coreReadySource), true);
assert.equal(/if \(!preparationMessageShown\) \{\s*onStatus\?\.\(messages\.preparingData\);\s*preparationMessageShown = true;\s*\}/.test(coreReadySource), true);
assert.equal(/if \(startupError\) \{[\s\S]*onStatus\?\.\(formatMessage\(messages\.failed, \{ error: startupError \}\)\);[\s\S]*throw new Error\(startupError\);/.test(coreReadySource), true);
assert.equal((coreReadySource.match(/throw new Error/g) || []).length, 1);
const metadataPromiseSource = source.match(/const \[unlocked, version, dataDir\] = await Promise\.all\(\[([\s\S]*?)\]\);/)?.[1] ?? '';
assert.equal(/App\.IsUnlocked\(\)/.test(metadataPromiseSource), true);
assert.equal(/App\.GetVersion\(\)/.test(metadataPromiseSource), true);
assert.equal(/App\.GetDataDir\(\)/.test(metadataPromiseSource), true);
});

test('App lazy-loads MainLayout and prefetches it after startup', () => {
assert.equal(/const loadMainLayout = \(\) => import\('\.\/components\/MainLayout'\);/.test(source), true);
assert.equal(/const MainLayout = lazy\(\(\) => loadMainLayout\(\)\.then\(\(m\) => \(\{ default: m\.MainLayout \}\)\)\);/.test(source), true);
assert.equal(/import\s+\{\s*MainLayout\s*\}\s+from\s+['"]\.\/components\/MainLayout['"]/.test(source), false);
assert.equal(/<Suspense fallback=\{<div className="h-screen bg-background" \/>}>\s*<MainLayout \/>/.test(source), true);
assert.equal(/if \(!loading\) \{\s*void loadMainLayout\(\);\s*\}/.test(source), true);
});

test('App shows a cold-start message while the Windows runtime is still warming up', () => {
Expand Down
Loading