1- import {
2- activeWindowAsync ,
3- subscribeActiveWindow ,
4- unsubscribeAllActiveWindow ,
5- } from '@miniben90/x-win'
61import { db } from './db/client'
72import { windowActivities , validateNewWindowActivity } from './db/schema'
83import { getAppSettings } from './settings'
94import { hasScreenRecordingPermission } from './permissions'
105import { ipcMain } from 'electron'
116import { logger } from './logger'
127
8+ // Lazy-load x-win module with detailed error reporting
9+ let xWinModule : any = null
10+ let xWinLoadError : Error | null = null
11+
12+ async function loadXWinModule ( ) {
13+ if ( xWinModule ) return xWinModule
14+ if ( xWinLoadError ) throw xWinLoadError
15+
16+ try {
17+ console . log ( '=== ATTEMPTING TO LOAD @miniben90/x-win ===' )
18+ console . log ( 'Process platform:' , process . platform )
19+ console . log ( 'Process arch:' , process . arch )
20+ console . log ( 'Process versions:' , JSON . stringify ( process . versions , null , 2 ) )
21+ console . log ( '__dirname:' , __dirname )
22+ console . log ( 'process.cwd():' , process . cwd ( ) )
23+ console . log ( 'app.isPackaged:' , require ( 'electron' ) . app . isPackaged )
24+
25+ xWinModule = await import ( '@miniben90/x-win' )
26+ console . log ( '=== @miniben90/x-win LOADED SUCCESSFULLY ===' )
27+ return xWinModule
28+ } catch ( error ) {
29+ console . error ( '=== FAILED TO LOAD @miniben90/x-win ===' )
30+ console . error ( 'Error name:' , error instanceof Error ? error . name : 'Unknown' )
31+ console . error ( 'Error message:' , error instanceof Error ? error . message : String ( error ) )
32+ console . error ( 'Error stack:' , error instanceof Error ? error . stack : 'No stack' )
33+ console . error ( 'Full error:' , JSON . stringify ( error , Object . getOwnPropertyNames ( error ) , 2 ) )
34+
35+ xWinLoadError = error instanceof Error ? error : new Error ( String ( error ) )
36+ throw xWinLoadError
37+ }
38+ }
39+
1340interface WindowInfo {
1441 id : number
1542 title : string
@@ -114,9 +141,17 @@ export async function startActivityTracking(): Promise<void> {
114141
115142 logger . info ( 'Starting activity tracking...' )
116143
144+ let xWin
145+ try {
146+ xWin = await loadXWinModule ( )
147+ } catch ( error ) {
148+ logger . error ( 'Cannot start activity tracking - x-win module failed to load:' , error )
149+ return
150+ }
151+
117152 try {
118153 // Get initial window state
119- const initialWindow = await activeWindowAsync ( )
154+ const initialWindow = await xWin . activeWindowAsync ( )
120155 if ( initialWindow ) {
121156 lastWindowInfo = initialWindow as WindowInfo
122157 currentActivityStartTime = new Date ( )
@@ -128,7 +163,7 @@ export async function startActivityTracking(): Promise<void> {
128163 }
129164
130165 // Subscribe to window changes
131- subscriptionId = subscribeActiveWindow ( async ( error , windowInfo ) => {
166+ subscriptionId = xWin . subscribeActiveWindow ( async ( error , windowInfo ) => {
132167 if ( error ) {
133168 logger . error ( 'Error in window subscription:' , error )
134169 return
@@ -254,8 +289,8 @@ export async function stopActivityTracking(): Promise<void> {
254289 await saveCurrentActivityIfNeeded ( )
255290
256291 // Unsubscribe from window changes
257- if ( subscriptionId !== null ) {
258- unsubscribeAllActiveWindow ( )
292+ if ( subscriptionId !== null && xWinModule ) {
293+ xWinModule . unsubscribeAllActiveWindow ( )
259294 subscriptionId = null
260295 }
261296
0 commit comments