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
14 changes: 9 additions & 5 deletions packages/jsx/src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@ import { createElement } from './createElement.js';
import { setCurrentApp } from './runtime.js';

/**
* Shared inline unmount helper — used by both the dev-server-backed path
* and the fallback path so cleanup logic stays synchronized.
* Unmount a list of apps. Swallow any errors thrown by `unmount()` but log
* a single error message for observability. Exported only for tests.
*/
function _unmountApps(apps: Array<{ unmount?: () => void }>): void {
export function unmountApps(apps: Array<{ unmount?: () => void }>): void {
apps.forEach((app) => {
if (typeof app.unmount === 'function') {
try { app.unmount(); } catch {}
try {
app.unmount();
} catch (err) {
console.error('[jsx] Error during unmount():', err);
}
}
});
}
Expand Down Expand Up @@ -162,7 +166,7 @@ export async function render(
// dev-server unavailable — use local helper for cleanup
const apps = (globalThis as any).__termuijs_apps;
if (Array.isArray(apps)) {
_unmountApps(apps);
unmountApps(apps);
(globalThis as any).__termuijs_apps = [];
}
});
Expand Down
26 changes: 26 additions & 0 deletions packages/jsx/src/unmountHelpers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { describe, it, expect, vi } from 'vitest';
import { unmountApps } from './render.js';

describe('unmountApps', () => {
it('logs errors, completes cleanup, and does not rethrow', () => {
const err = new Error('boom');
const throwing = { unmount: vi.fn(() => { throw err; }) };
const called = { unmount: vi.fn(() => { /* ok */ }) };

const spy = vi.spyOn(console, 'error').mockImplementation(() => {});

try {
// Should not throw
expect(() => unmountApps([throwing, called])).not.toThrow();

// Ensure the second unmount still ran
expect(called.unmount).toHaveBeenCalledTimes(1);

// Ensure we logged the error exactly once with the expected message
expect(spy).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenCalledWith('[jsx] Error during unmount():', err);
} finally {
spy.mockRestore();
}
});
Comment thread
coderabbitai[bot] marked this conversation as resolved.
});
Loading