@@ -12,6 +12,7 @@ import { spacesService } from './spacesService.js';
1212import * as common from '../common.js' ;
1313/** @typedef {common.SessionPresence } SessionPresence */
1414/** @typedef {common.Space } Space */
15+ /** @typedef {common.Window } Window */
1516/** @typedef {import('./dbService.js').WindowBounds } WindowBounds */
1617
1718// eslint-disable-next-line no-unused-vars, no-var
@@ -276,7 +277,11 @@ async function processMessage(request, sender) {
276277 case 'requestSpaceFromWindowId' :
277278 windowId = cleanParameter ( request . windowId ) ;
278279 if ( windowId ) {
279- return requestSpaceFromWindowId ( windowId ) ;
280+ let matchByTabs = undefined ;
281+ if ( request . matchByTabs ) {
282+ matchByTabs = cleanParameter ( request . matchByTabs ) ;
283+ }
284+ return requestSpaceFromWindowId ( windowId , matchByTabs ) ;
280285 }
281286 return undefined ;
282287
@@ -786,9 +791,11 @@ async function requestCurrentSpace() {
786791
787792/**
788793 * @param {number } windowId
794+ * @param {boolean|undefined } matchByTabs - Whether to match the space by tabs if matching by
795+ * windowId fails. If undefined, the default is to match by windowId only.
789796 * @returns {Promise<Space|false> }
790797 */
791- async function requestSpaceFromWindowId ( windowId ) {
798+ async function requestSpaceFromWindowId ( windowId , matchByTabs ) {
792799 // first check for an existing session matching this windowId
793800 const session = await dbService . fetchSessionByWindowId ( windowId ) ;
794801
@@ -802,11 +809,37 @@ async function requestSpaceFromWindowId(windowId) {
802809 history : session . history ,
803810 } ;
804811 return space ;
805-
806- // otherwise build a space object out of the actual window
807812 } else {
808813 try {
814+ /** @type {Window } */
809815 const window = await chrome . windows . get ( windowId , { populate : true } ) ;
816+
817+ if ( matchByTabs ) {
818+ console . log ( `matchByTabs=true` ) ;
819+ const allSpaces = await requestAllSpaces ( ) ;
820+ // If any space in the database has the exact same tabs in the exact same order as
821+ // the currently-open window, then we assume the window got out of sync (due to a
822+ // Chrome restart or other factors). Update the database with the new window id and
823+ // return it.
824+ for ( const space of allSpaces ) {
825+ if (
826+ space . tabs . length === window . tabs . length &&
827+ space . tabs . every ( ( tab , index ) => tab . url === window . tabs [ index ] . url )
828+ ) {
829+ // Update the database object.
830+ const dbSession = await dbService . fetchSessionById ( space . sessionId ) ;
831+ dbSession . windowId = windowId ;
832+ await dbService . updateSession ( dbSession ) ;
833+
834+ // Update the space object and return it.
835+ space . windowId = windowId ;
836+ console . log ( `matchByTabs: Found a session and updated it.` ) ;
837+ return space ;
838+ }
839+ }
840+ }
841+
842+ // Otherwise build a space object out of the actual window.
810843 /** @type {Space } */
811844 const space = {
812845 sessionId : false ,
@@ -1273,9 +1306,9 @@ function calculateSessionBounds(displayBounds, sessionBounds) {
12731306}
12741307
12751308/**
1276- * Ensures the parameter is a number.
1309+ * Ensures the parameter is a number or boolean .
12771310 * @param {string|number } param - The parameter to clean.
1278- * @returns {number } - The cleaned parameter.
1311+ * @returns {number|boolean } - The cleaned parameter.
12791312 */
12801313function cleanParameter ( param ) {
12811314 if ( typeof param === 'number' ) {
0 commit comments