@@ -25,6 +25,7 @@ class SpacesService {
2525 constructor ( ) {
2626 this . tabHistoryUrlMap = { } ;
2727 this . closedWindowIds = { } ;
28+ this . boundsUpdateTimers = { } ;
2829 this . sessionUpdateTimers = { } ;
2930 this . historyQueue = [ ] ;
3031 this . eventQueueCount = 0 ;
@@ -405,6 +406,50 @@ class SpacesService {
405406 return null ;
406407 }
407408
409+ /**
410+ * Captures and stores window bounds for a window with debouncing.
411+ * This is called when window bounds change to ensure we have current bounds
412+ * without excessive database writes during rapid resize/move operations.
413+ *
414+ * @param {number } windowId - The ID of the window to capture bounds for
415+ * @param {Object } bounds - The window bounds object with left, top, width, height
416+ * @returns {Promise<void> }
417+ */
418+ async captureWindowBounds ( windowId , bounds ) {
419+ await this . ensureInitialized ( ) ;
420+
421+ const session = await this . getSessionByWindowId ( windowId ) ;
422+ if ( session && session . id ) {
423+ // Update bounds in memory immediately for responsiveness
424+ session . windowBounds = {
425+ left : bounds . left ,
426+ top : bounds . top ,
427+ width : bounds . width ,
428+ height : bounds . height
429+ } ;
430+
431+ if ( debug ) {
432+ // eslint-disable-next-line no-console
433+ console . log ( `Captured window bounds for session ${ session . id } :` , session . windowBounds ) ;
434+ }
435+
436+ // Debounce database writes to avoid excessive I/O during rapid resize/move
437+ clearTimeout ( this . boundsUpdateTimers [ windowId ] ) ;
438+ this . boundsUpdateTimers [ windowId ] = setTimeout ( async ( ) => {
439+ try {
440+ // Save bounds to database after debounce period
441+ await this . _updateSessionSync ( session ) ;
442+ if ( debug ) {
443+ // eslint-disable-next-line no-console
444+ console . log ( `Saved window bounds to database for session ${ session . id } ` ) ;
445+ }
446+ } catch ( error ) {
447+ console . error ( `Error saving bounds for session ${ session . id } :` , error ) ;
448+ }
449+ } , 1000 ) ; // 1 second debounce - adjust as needed
450+ }
451+ }
452+
408453 // event listener functions for window and tab events
409454 // (events are received and screened first in background.js)
410455 // -----------------------------------------------------------------------------------------
@@ -530,6 +575,7 @@ class SpacesService {
530575 }
531576
532577 this . closedWindowIds [ windowId ] = true ;
578+ clearTimeout ( this . boundsUpdateTimers [ windowId ] ) ;
533579 clearTimeout ( this . sessionUpdateTimers [ windowId ] ) ;
534580 }
535581
@@ -538,7 +584,7 @@ class SpacesService {
538584 // if this is a saved session then just remove the windowId reference
539585 if ( session . id ) {
540586 session . windowId = false ;
541- // Persist the cleared windowId to database with sync
587+ // Persist the window to database with sync
542588 await this . _updateSessionSync ( session ) ;
543589
544590 // else if it is temporary session then remove the session from the cache
@@ -856,11 +902,12 @@ class SpacesService {
856902 * @param {string } sessionName - The name for the new session
857903 * @param {Array<Object> } tabs - Array of tab objects containing URL and other tab properties
858904 * @param {number|false } windowId - The window ID to associate with this session, or false for no association
905+ * @param {WindowBounds } [windowBounds] - Optional window bounds to save with the session
859906 * @returns {Promise<Session|null> } Promise that resolves to:
860907 * - Session object with id property if successfully created
861908 * - null if session creation failed, no tabs were provided, or attempted on already saved session
862909 */
863- async saveNewSession ( sessionName , tabs , windowId ) {
910+ async saveNewSession ( sessionName , tabs , windowId , windowBounds ) {
864911 await this . ensureInitialized ( ) ;
865912
866913 if ( ! tabs ) {
@@ -909,6 +956,11 @@ class SpacesService {
909956 session . sessionHash = sessionHash ;
910957 session . tabs = tabs ;
911958 session . lastAccess = new Date ( ) ;
959+
960+ // Add window bounds if provided
961+ if ( windowBounds ) {
962+ session . windowBounds = windowBounds ;
963+ }
912964
913965 // save session to db - this should only be called on temporary sessions (id: false)
914966 try {
0 commit comments