<commit_before>import {debounce} from 'debounce'
import bookmarks from './api/chrome/bookmarks.js'
import {
	persistSession,
} from './api/index.js'
import {
	getBookmarkFolderId,
} from './api/chrome/bookmarks'
import openSavedSessionTracker from './api/openSavedSessionTracker'
import status from './ui/status/index.js'

(async function () {
	await bookmarks.createSeshyFolder()
}())

const debouncedTabChangeListener = debounce(tabChangeListener, 2000)

// Listeners must be at the top-level: https://developer.chrome.com/docs/extensions/mv2/background_migration/#listeners
chrome.tabs.onUpdated.addListener((_, changeInfo, tab) => debouncedTabChangeListener(onUpdatedListener(tab, changeInfo)))
chrome.tabs.onRemoved.addListener((_, removeInfo) => debouncedTabChangeListener(onRemovedListener(removeInfo)))
chrome.windows.onRemoved.addListener(
	openSavedSessionTracker.removeClosedWindowId,
)
chrome.storage.onChanged.addListener(openSavedSessionTracker.removeStaleWindowIds)
chrome.windows.onFocusChanged.addListener(setActionIcon)

async function tabChangeListener (promise) {
	await promise
}

async function onRemovedListener (removeInfo) {
	console.debug('tab removed', removeInfo)
	if (removeInfo.isWindowClosing) return

	const bookmarkFolderId = await getBookmarkFolderId(removeInfo.windowId)
	const isSavedSession = !!bookmarkFolderId<commit_msg>Fix sessions disappearing<commit_after>import {debounce} from 'debounce'
import bookmarks from './api/chrome/bookmarks.js'
import {
	persistSession,
} from './api/index.js'
import {
	getBookmarkFolderId,
} from './api/chrome/bookmarks'
import openSavedSessionTracker from './api/openSavedSessionTracker'
import status from './ui/status/index.js'

(async function () {
	await bookmarks.createSeshyFolder()
}())

const debouncedTabChangeListener = debounce(tabChangeListener, 2000)

// Listeners must be at the top-level: https://developer.chrome.com/docs/extensions/mv2/background_migration/#listeners
chrome.runtime.onStartup.addListener(openSavedSessionTracker.removeStaleWindowIds)
chrome.storage.onChanged.addListener(openSavedSessionTracker.removeStaleWindowIds) // TODO: Is this triggered by the onRemoved listener?
chrome.tabs.onUpdated.addListener((_, changeInfo, tab) => debouncedTabChangeListener(onUpdatedListener(tab, changeInfo)))
chrome.tabs.onRemoved.addListener((_, removeInfo) => debouncedTabChangeListener(onRemovedListener(removeInfo)))
chrome.windows.onRemoved.addListener(
	openSavedSessionTracker.removeClosedWindowId,
)
chrome.windows.onFocusChanged.addListener(setActionIcon)

async function tabChangeListener (promise) {
	await promise
}

async function onRemovedListener (removeInfo) {
	console.debug('tab removed', removeInfo)
	if (removeInfo.isWindowClosing) return

	const bookmarkFolderId = await getBookmarkFolderId(removeInfo.windowId)
	const isSavedSession = !!bookmarkFolderId
{'fraction_non_alphanumeric': 0.10456605088881143, 'fraction_numerical': 0.003485535029627048, 'mean_word_length': 7.516320474777448, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4959263', 'n_tokens_mistral': 857, 'n_tokens_neox': 841, 'n_words': 158}