1010import { dbService } from './dbService.js' ;
1111import { spacesService } from './spacesService.js' ;
1212import * as common from '../common.js' ;
13+ /** @typedef {common.SessionPresence } SessionPresence */
1314/** @typedef {common.Space } Space */
1415/** @typedef {import('./dbService.js').WindowBounds } WindowBounds */
1516
@@ -41,17 +42,17 @@ async function rediscoverWindowByUrl(storageKey, htmlFilename) {
4142
4243 // If not in storage or window doesn't exist, search for window by URL
4344 const targetUrl = chrome . runtime . getURL ( htmlFilename ) ;
44- const allWindows = await chrome . windows . getAll ( { populate : true } ) ;
45-
45+ const allWindows = await chrome . windows . getAll ( { populate : true } ) ;
46+
4647 for ( const window of allWindows ) {
4748 for ( const tab of window . tabs ) {
4849 if ( tab . url && tab . url . startsWith ( targetUrl ) ) {
49- await chrome . storage . local . set ( { [ storageKey ] : window . id } ) ;
50+ await chrome . storage . local . set ( { [ storageKey ] : window . id } ) ;
5051 return window . id ;
5152 }
5253 }
5354 }
54-
55+
5556 return false ;
5657}
5758
@@ -166,19 +167,19 @@ export function initializeServiceWorker() {
166167 await closePopupWindow ( ) ;
167168 }
168169 }
169-
170+
170171 spacesService . handleWindowFocussed ( windowId ) ;
171172 } ) ;
172173
173174 // Listen for window bounds changes (resize/move) with debouncing
174175 chrome . windows . onBoundsChanged . addListener ( async ( window ) => {
175176 if ( checkInternalSpacesWindows ( window . id , false ) ) return ;
176-
177+
177178 // Capture bounds - await ensures proper event ordering and timer management
178179 await spacesService . captureWindowBounds ( window . id , {
179180 left : window . left ,
180181 top : window . top ,
181- width : window . width ,
182+ width : window . width ,
182183 height : window . height
183184 } ) ;
184185 } ) ;
@@ -196,7 +197,7 @@ export function initializeServiceWorker() {
196197 try {
197198 // Ensure spacesService is initialized before processing any message
198199 await spacesService . ensureInitialized ( ) ;
199-
200+
200201 const response = await processMessage ( request , sender ) ;
201202 if ( response !== undefined ) {
202203 sendResponse ( response ) ;
@@ -206,7 +207,7 @@ export function initializeServiceWorker() {
206207 sendResponse ( false ) ;
207208 }
208209 } ) ( ) ;
209-
210+
210211 // We must return true synchronously to keep the message port open
211212 // for our async sendResponse() calls
212213 return true ;
@@ -361,7 +362,7 @@ async function processMessage(request, sender) {
361362 if ( ! windowId ) {
362363 return false ;
363364 }
364-
365+
365366 try {
366367 const window = await chrome . windows . get ( windowId ) ;
367368 // Capture bounds before programmatically closing the window
@@ -755,7 +756,11 @@ function checkInternalSpacesWindows(windowId, windowClosed) {
755756 */
756757async function requestSessionPresence ( sessionName ) {
757758 const session = await dbService . fetchSessionByName ( sessionName ) ;
758- return { exists : ! ! session , isOpen : ! ! session && ! ! session . windowId } ;
759+ return {
760+ exists : ! ! session ,
761+ isOpen : ! ! session && ! ! session . windowId ,
762+ sessionName : session ?. name || false ,
763+ } ;
759764}
760765
761766/**
@@ -798,7 +803,7 @@ async function requestSpaceFromWindowId(windowId) {
798803 } ;
799804 return space ;
800805
801- // otherwise build a space object out of the actual window
806+ // otherwise build a space object out of the actual window
802807 } else {
803808 try {
804809 const window = await chrome . windows . get ( windowId , { populate : true } ) ;
@@ -827,11 +832,11 @@ async function requestSpaceFromWindowId(windowId) {
827832 */
828833async function requestSpaceFromSessionId ( sessionId ) {
829834 const session = await dbService . fetchSessionById ( sessionId ) ;
830-
835+
831836 if ( ! session ) {
832837 return null ;
833838 }
834-
839+
835840 return {
836841 sessionId : session . id ,
837842 windowId : session . windowId ,
@@ -893,12 +898,12 @@ async function handleLoadSession(sessionId, tabUrl) {
893898 await focusOrLoadTabInWindow ( newWindow , tabUrl ) ;
894899 }
895900
896- /* session.tabs.forEach(function (curTab) {
897- chrome.tabs.create({windowId: newWindow.id, url: curTab.url, pinned: curTab.pinned, active: false});
898- });
901+ /* session.tabs.forEach(function (curTab) {
902+ chrome.tabs.create({windowId: newWindow.id, url: curTab.url, pinned: curTab.pinned, active: false});
903+ });
899904
900- const tabs = await chrome.tabs.query({windowId: newWindow.id, index: 0});
901- chrome.tabs.remove(tabs[0].id); */
905+ const tabs = await chrome.tabs.query({windowId: newWindow.id, index: 0});
906+ chrome.tabs.remove(tabs[0].id); */
902907 }
903908}
904909
@@ -945,11 +950,11 @@ async function handleSaveNewSession(windowId, sessionName, deleteOld) {
945950 sessionName ,
946951 curWindow . tabs ,
947952 curWindow . id ,
948- {
949- left : curWindow . left ,
950- top : curWindow . top ,
951- width : curWindow . width ,
952- height : curWindow . height
953+ {
954+ left : curWindow . left ,
955+ top : curWindow . top ,
956+ width : curWindow . width ,
957+ height : curWindow . height
953958 } ,
954959 ) ;
955960 return result ?? false ;
@@ -977,7 +982,7 @@ async function handleRestoreFromBackup(space, deleteOld) {
977982 ) ;
978983 return null ;
979984 }
980-
985+
981986 // if we choose to overwrite, delete the existing session
982987 await handleDeleteSession ( existingSession . id ) ;
983988 }
@@ -1024,24 +1029,22 @@ async function handleImportNewSession(urlList) {
10241029async function handleUpdateSessionName ( sessionId , sessionName , deleteOld ) {
10251030 // check to make sure session name doesn't already exist
10261031 const existingSession = await dbService . fetchSessionByName ( sessionName ) ;
1027- // Fix: allow renaming when only capitalization is changed
1028- if ( existingSession && existingSession . id === sessionId ) {
1029- return spacesService . updateSessionName ( sessionId , sessionName ) ?? false ;
1030- }
10311032
1032- // if session with same name already exist, then prompt to override the existing session
1033- if ( existingSession ) {
1033+ // If a different session with same name already exists, then prompt to
1034+ // override the existing session.
1035+ // TESTME: Only delete if session ids differ.
1036+ if ( existingSession && existingSession . id !== sessionId ) {
10341037 if ( ! deleteOld ) {
10351038 console . error (
10361039 `handleUpdateSessionName: Session with name "${ sessionName } " already exists and deleteOld was not true.`
10371040 ) ;
10381041 return false ;
10391042 }
1040-
1043+
10411044 // if we choose to override, then delete the existing session
10421045 await handleDeleteSession ( existingSession . id ) ;
10431046 }
1044-
1047+
10451048 return spacesService . updateSessionName ( sessionId , sessionName ) ?? false ;
10461049}
10471050
@@ -1188,7 +1191,7 @@ async function handleMoveTabToSession(tabId, sessionId) {
11881191 if ( ! session ) {
11891192 return false ;
11901193 }
1191-
1194+
11921195 // if session is currently open then move it directly
11931196 if ( session . windowId ) {
11941197 moveTabToWindow ( tab , session . windowId ) ;
@@ -1345,7 +1348,7 @@ async function getTargetDisplayWorkArea() {
13451348 const activeDisplay = displays . find ( display => {
13461349 const d = display . workArea ;
13471350 return windowCenterX >= d . left && windowCenterX < ( d . left + d . width ) &&
1348- windowCenterY >= d . top && windowCenterY < ( d . top + d . height ) ;
1351+ windowCenterY >= d . top && windowCenterY < ( d . top + d . height ) ;
13491352 } ) ;
13501353 if ( activeDisplay ) {
13511354 targetDisplay = activeDisplay ;
@@ -1402,5 +1405,6 @@ export {
14021405 getEffectiveTabUrl ,
14031406 getTargetDisplayWorkArea ,
14041407 handleLoadSession ,
1408+ handleUpdateSessionName ,
14051409 requestAllSpaces ,
14061410} ;
0 commit comments