Skip to content

Commit

Permalink
fix(background): don't clear overpaying on page refresh
Browse files Browse the repository at this point in the history
Co-authored-by: Radu-Cristian Popa <[email protected]>
  • Loading branch information
sidvishnoi and raducristianpopa committed Jul 30, 2024
1 parent dbc9a2b commit 59bd9e5
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/background/services/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ export class Background {

bindTabHandlers() {
this.browser.tabs.onRemoved.addListener(this.tabEvents.clearTabSessions)
this.browser.tabs.onUpdated.addListener(this.tabEvents.clearTabSessions)
this.browser.tabs.onUpdated.addListener(this.tabEvents.clearTabSessions, {
properties: ['url', 'discarded', 'status', 'isArticle']
})
this.browser.tabs.onCreated.addListener(this.tabEvents.onCreatedTab)
this.browser.tabs.onActivated.addListener(this.tabEvents.onActivatedTab)
}
Expand Down
10 changes: 8 additions & 2 deletions src/background/services/monetization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,10 @@ export class MonetizationService {
emitToggleWM({ enabled: !enabled })
}

clearTabSessions(tabId: number) {
clearTabSessions(
tabId: number,
{ clearOverpaying }: { clearOverpaying: boolean }
) {
this.logger.debug(`Attempting to clear sessions for tab ${tabId}.`)
const sessions = this.tabState.getSessions(tabId)

Expand All @@ -228,7 +231,10 @@ export class MonetizationService {
session.stop()
}

this.tabState.clearByTabId(tabId)
this.tabState.clearSessionsByTabId(tabId)
if (clearOverpaying) {
this.tabState.clearOverpayingByTabId(tabId)
}

this.logger.debug(`Cleared ${sessions.size} sessions for tab ${tabId}.`)
}
Expand Down
4 changes: 3 additions & 1 deletion src/background/services/tabEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,13 @@ export class TabEvents {
tabId: TabId,
changeInfo: Tabs.OnUpdatedChangeInfoType | Tabs.OnRemovedRemoveInfoType
) => {
// console.log('clearTabSessions', changeInfo)
if (
('status' in changeInfo && changeInfo.status === 'loading') ||
'isWindowClosing' in changeInfo
) {
this.monetizationService.clearTabSessions(tabId)
const clearOverpaying = !('isWindowClosing' in changeInfo) // TODO: verify this condition
this.monetizationService.clearTabSessions(tabId, { clearOverpaying })
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/background/services/tabState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,11 @@ export class TabState {
return [...this.sessions.keys()]
}

clearByTabId(tabId: TabId) {
clearOverpayingByTabId(tabId: TabId) {
this.state.delete(tabId)
}

clearSessionsByTabId(tabId: TabId) {
this.sessions.delete(tabId)
}
}

0 comments on commit 59bd9e5

Please sign in to comment.