Skip to content

Commit

Permalink
fix: preview: don't spam "Recently Closed Windows" (#301)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbnuqw committed Feb 17, 2024
1 parent c471302 commit 86f8c97
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
33 changes: 28 additions & 5 deletions src/services/tabs.preview.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { NOID } from 'src/defaults'
import { ADDON_HOST, NOID } from 'src/defaults'
import { InstanceType } from 'src/types'
import { Sidebar } from './sidebar'
import { Tabs } from './tabs.fg'
import { Styles } from './styles'
Expand Down Expand Up @@ -30,6 +31,7 @@ let currentWinX = 0
let currentWinOffsetY = 0
let currentWinOffsetX = 0
let deadOnArrival = false
let listening = false

export async function showPreviewPopup(tabId: ID, y?: number) {
const tab = Tabs.byId[tabId]
Expand All @@ -45,6 +47,8 @@ export async function showPreviewPopup(tabId: ID, y?: number) {

if (currentWinWidth === 0 || currentWinHeight === 0) return

if (!listening) setupPopupDisconnectionListener()

currentWinOffsetY = 0
currentWinOffsetX = 0

Expand Down Expand Up @@ -157,8 +161,7 @@ export function updatePreviewPopup(tabId: ID) {
if (IPC.state.previewConnection) {
IPC.sendToPreview('updatePreview', tabId, tab.title, tab.url, !!tab.discarded)
} else {
if (state.winId !== NOID) browser.windows.remove(state.winId)
state.winId = NOID
closePreviewPopup()
}
}

Expand All @@ -168,8 +171,10 @@ export function closePreviewPopup() {
return
}

if (state.winId !== NOID) browser.windows.remove(state.winId)
state.winId = NOID
if (state.winId !== NOID) {
browser.windows.remove(state.winId)
state.winId = NOID
}
}

function getPopupX() {
Expand All @@ -187,6 +192,24 @@ function getPopupX() {
}
}

export function setupPopupDisconnectionListener() {
const checkPart = ADDON_HOST.slice(50, 52) + '/p'

IPC.onDisconnected(InstanceType.preview, async () => {
if (!Settings.state.previewTabs) return

const recentlyClosedItems = await browser.sessions.getRecentlyClosed({ maxResults: 3 })
for (const rc of recentlyClosedItems) {
if (!rc.window?.sessionId) continue
if (rc.window.tabs?.length === 1) {
const url = rc.window.tabs[0].url
if (url.startsWith(checkPart, 50)) browser.sessions.forgetClosedWindow(rc.window.sessionId)
}
}
})
listening = true
}

export async function showPreviewInline(tabId: ID) {
if (state.tabId === tabId) return

Expand Down
6 changes: 6 additions & 0 deletions src/types/web-ext.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,12 @@ declare namespace browser {
function getWindowValue<T>(id: ID, key: string): Promise<T>
function setTabValue<T>(tabId: ID, key: string, value: T): Promise<void>
function getTabValue<T>(tabId: ID, key: string): Promise<T>
function forgetClosedWindow(sessionId: string): Promise<void>
function forgetClosedTab(windowId: number, sessionId: string): Promise<void>

type SessionChangeListener = () => void

const onChanged: EventTarget<SessionChangeListener>
}

/**
Expand Down

0 comments on commit 86f8c97

Please sign in to comment.