Appearance
Lifecycle Events
Provider는 선택적으로 lifecycle 인터페이스를 구현하여 초기화, 윈도우 연결, 정리 시점에 동작을 정의할 수 있다. 모든 lifecycle 메서드는 등록 순서대로 호출된다.
실행 순서
resolveAll() → 모든 provider 인스턴스 생성
initAll() → OnInit.onInit() 호출
windows.open('main') → 프레임워크가 ipc.windows 선언대로 메인 창 생성
└ @OnWindow('created') 훅 → load
→ IPC 핸들러 등록
창 이벤트마다 → @OnWindow(stage) 훅 (loaded·shown·focused·blurred·closing·closed)
windows.open(kind) → 서브 창이 열릴 때마다 같은 단계가 다시 돈다
disposeAll() → OnDispose.onDispose() 호출 (메인 창 closed 시, 서브 창 cascade close 후)각 단계에서 에러가 발생하면 onError 콜백이 호출되고, 나머지 provider의 lifecycle은 계속 실행된다. 창은 provider가 만들지 않는다 — shared의 ipc.windows에 선언하면 프레임워크의 WindowRegistry가 만든다.
OnInit
윈도우 생성 전에 호출된다. 설정 로드, 데이터베이스 연결 등 초기화 작업에 사용한다.
ts
import { Injectable, type OnInit } from '@repo/electron-ipc';
@Injectable()
class DatabaseService implements OnInit {
private db!: Database;
onInit() {
this.db = new Database('./app.db');
}
}@OnWindow(stage, { kind? })
provider 메서드를 창 수명 단계에 건다. 메인 창뿐 아니라 서브 창이 열릴 때마다 불리므로, 특정 창에만 반응하려면 kind를 준다. 한 클래스에 여러 개 둘 수 있다.
단계는 Electron 창 이벤트에 1:1로 대응한다.
| 단계 | Electron | 언제 |
|---|---|---|
created | 생성자 직후 | 로드 전. 리스너 걸기, 창 참조 보관 |
loaded | did-finish-load | 렌더러가 살아 있음. 초기 데이터 push |
shown | show | 실제로 화면에 보임 |
focused / blurred | focus / blur | 활성 창 추적 |
closing | close | 취소 가능 — 훅이 false를 돌려주면 창이 닫히지 않는다 |
closed | closed | 정리 |
ts
import { Injectable, OnWindow } from '@repo/electron-ipc';
import type { BrowserWindow } from 'electron';
@Injectable()
class TrayService {
@OnWindow('created', { kind: 'main' })
attachMain(window: BrowserWindow) {
// 트레이 아이콘 클릭 시 메인 창 토글
}
@OnWindow('closing')
confirmClose(window: BrowserWindow, kind: RegisteredWindowKind) {
return !hasUnsavedChanges(kind); // false면 닫기 취소
}
}- 훅은 동기다.
closing은 Electron이 동기preventDefault만 받으므로 async 훅의 결과는 무시된다 - 훅 예외는 provider별로 격리되어
onError('Svc.method(stage)', error)로 보고된 뒤 다음 훅으로 계속 간다 kind는ipc.windows의 키다. 앱이Register에 창 선언을 등록하면'main' | 'settings'처럼 좁혀진다- 훅이 돈 뒤(취소된
closing제외) 같은 단계가 렌더러에도service.window.onStageChanged로 알려진다 (WindowContract)
OnDispose
메인 창 closed 이벤트 시 호출된다. 서브 창은 그 전에 프레임워크가 모두 닫는다. 리소스 정리에 사용한다.
ts
import { Injectable, type OnDispose } from '@repo/electron-ipc';
@Injectable()
class DatabaseService implements OnDispose {
onDispose() {
this.db.close();
}
}다음 단계
- EventSource — 메인 → 렌더러 이벤트 push
- Guards — IPC 요청 검증