TypeScript, in layers, with a strict linter
The client was one main.js, one preload.js, three files in lib/ and one renderer
script. It is now a typed application whose imports point inward: domain (models,
ports, errors) knows nothing about Electron, Node or Python; application orchestrates
it through those ports; infrastructure holds the adapters — the Python CLI, HTTP, the
filesystem, Electron itself — and main, preload and renderer sit on top as hosts.
STRUCTURE.md is the map, and the deliverable as much as the code is: every layer, every
pattern in use (ports and adapters, repository vs gateway, service, DTO and mapper,
composition root, controller and router, single flight, observer streams, state store
with unidirectional flow, passive view, coded error hierarchy, frozen constant tables,
untrusted-data readers) and the naming rules — files, classes, and a verb vocabulary
for methods where find/require/read/list/apply/render/handle each state a contract.
Two properties fell out of the move, and they are why it was worth doing:
- The catalog can be driven with no window and no Electron at all. The smoke test
assembles the same services against the same ports in a plain Node process; it used
to be a script that reimplemented the bridge.
- The window never receives a filesystem path. A title crosses the bridge without
one, and launching is asked for by name, resolved in the main process from the
store's own state. Verified with a fake launcher: an unknown name answers false, a
native title resolves to its menu entry, a hosted one to its catalog URL.
Types are mandatory, including where inference would manage: explicit return,
parameter and property types, strict plus noUncheckedIndexedAccess,
exactOptionalPropertyTypes, noImplicitOverride and noPropertyAccessFromIndexSignature,
typescript-eslint strictTypeChecked and stylisticTypeChecked, exhaustive switches, no
any, no non-null assertions, and no casts on foreign data — engine stdout and the
registry go through readers that turn unknown into typed values. naming-convention
enforces the patterns rather than trusting them.
Two rule conflicts had to be decided rather than papered over. typedef and
no-inferrable-types disagree about `fallback: string = ''`: the annotation wins, since a
signature states its types. erasableSyntaxOnly is off, because it forbids constructor
parameter properties, which are how dependencies are declared here.
The preload and the renderer are bundled by esbuild into one file each: a sandboxed
preload may not require its own modules, and a module script over file:// is blocked by
the page's own origin rules. tsc compiles the rest. The package ships build/** and
package.json — 111 entries, no sources, no toolchain.
New targets: build, typecheck, lint, lint-fix, and check — typecheck, lint, then both
test suites, cheapest failure first. Every script that runs the app builds first, so a
stale bundle cannot be tested.
Nothing about the window changed: same side menu, same categories, same switcher, same
two languages. make check is clean, both test suites pass with one store and with two,
the packaged 1.3.0 bundle drives the real store, and the window was photographed before
and after — the two are the same picture.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
import type { BridgeApi } from '../shared/contracts/BridgeApi'
|
||||
import { requireBridge } from './BridgeAccess'
|
||||
import { CatalogController } from './controllers/CatalogController'
|
||||
import { EngineStreamController } from './controllers/EngineStreamController'
|
||||
import { PreferencesController } from './controllers/PreferencesController'
|
||||
import { StoreController } from './controllers/StoreController'
|
||||
import { AppStore, type AppState } from './state/AppStore'
|
||||
import { buildCategorySections, resolveFilter, type CategoryFilter } from './state/CategoryFilter'
|
||||
import { CatalogGridView } from './views/CatalogGridView'
|
||||
import { GameCardView } from './views/GameCardView'
|
||||
import { GateView } from './views/GateView'
|
||||
import { LogDrawerView } from './views/LogDrawerView'
|
||||
import { SideMenuView } from './views/SideMenuView'
|
||||
import { TopBarView } from './views/TopBarView'
|
||||
|
||||
/**
|
||||
* The window, assembled.
|
||||
*
|
||||
* The flow is one direction only: a control calls a controller, the controller calls
|
||||
* the bridge and writes to the store, and the store re-renders every view. No view
|
||||
* reads another view, and nothing but the store decides what is on screen.
|
||||
*/
|
||||
export class RendererApplication {
|
||||
private readonly store = new AppStore()
|
||||
private readonly bridge: BridgeApi
|
||||
private readonly log: LogDrawerView
|
||||
private readonly gate: GateView
|
||||
private readonly grid: CatalogGridView
|
||||
private readonly topBar: TopBarView
|
||||
private readonly sideMenu: SideMenuView
|
||||
private readonly catalog: CatalogController
|
||||
private readonly stores: StoreController
|
||||
private readonly preferences: PreferencesController
|
||||
private readonly streams: EngineStreamController
|
||||
|
||||
public constructor (bridge: BridgeApi = requireBridge()) {
|
||||
this.bridge = bridge
|
||||
|
||||
this.log = new LogDrawerView({
|
||||
onOpenFolder: (directory: string): void => { void this.bridge.openFolder(directory) }
|
||||
})
|
||||
this.gate = new GateView((url: string): void => { void this.bridge.openUrl(url) })
|
||||
this.catalog = new CatalogController(this.bridge, this.store, this.log)
|
||||
this.stores = new StoreController(this.bridge, this.store, this.gate, this.log, this.catalog)
|
||||
this.preferences = new PreferencesController(this.bridge, this.store)
|
||||
this.streams = new EngineStreamController(this.bridge, this.store, this.log)
|
||||
|
||||
this.grid = new CatalogGridView(new GameCardView({
|
||||
onInstall: (name: string): void => { void this.catalog.syncGames([name]) },
|
||||
onLaunch: (name: string): void => { void this.catalog.launchGame(name) },
|
||||
onRemove: (name: string): void => { void this.catalog.removeGame(name) }
|
||||
}))
|
||||
this.topBar = new TopBarView({
|
||||
onToggleNavigation: (): void => { void this.preferences.toggleNavigation() }
|
||||
})
|
||||
this.sideMenu = new SideMenuView({
|
||||
onSelectStore: (home: string): void => { void this.stores.selectStore(home) },
|
||||
onAddStore: (): void => { void this.stores.offerStores() },
|
||||
onSyncAll: (): void => { void this.catalog.syncGames([]) },
|
||||
onRefresh: (): void => { void this.catalog.refresh() },
|
||||
onSelectCategory: (filter: CategoryFilter): void => { this.store.applyFilter(filter) },
|
||||
onSelectLocale: (locale: string): void => { void this.preferences.selectLocale(locale) }
|
||||
})
|
||||
|
||||
this.store.subscribe((state: AppState): void => { this.render(state) })
|
||||
this.streams.subscribe()
|
||||
}
|
||||
|
||||
/** Decides what the window is showing, then hands over to the views. */
|
||||
public async start (): Promise<void> {
|
||||
this.store.applyAppState(await this.bridge.readState())
|
||||
const state = this.store.readState()
|
||||
|
||||
if (state.pythonVersion === null) {
|
||||
this.stores.showMissingPythonGate()
|
||||
return
|
||||
}
|
||||
if (state.currentStore === null) {
|
||||
await this.stores.offerStores()
|
||||
return
|
||||
}
|
||||
if (state.engine !== null && !state.engine.supported) {
|
||||
this.stores.showOutdatedEngineGate()
|
||||
return
|
||||
}
|
||||
this.gate.hide()
|
||||
await this.catalog.refresh()
|
||||
}
|
||||
|
||||
private render (state: AppState): void {
|
||||
// The filter is corrected before anything is drawn from it, so the menu and the
|
||||
// grid can never disagree about which category is active.
|
||||
const corrected = resolveFilter(state.filter, buildCategorySections(state.games, state.messages))
|
||||
if (corrected !== state.filter) {
|
||||
this.store.applyFilter(corrected)
|
||||
return
|
||||
}
|
||||
|
||||
document.body.classList.toggle('nav-closed', !state.navigationOpen)
|
||||
this.topBar.render(state)
|
||||
this.sideMenu.render(state)
|
||||
this.log.render(state)
|
||||
if (this.gate.visible) this.grid.hide()
|
||||
else this.grid.render(state)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user