diff --git a/src/background/services/monetization.ts b/src/background/services/monetization.ts index 84aa9218..0e9dd4c4 100644 --- a/src/background/services/monetization.ts +++ b/src/background/services/monetization.ts @@ -8,7 +8,7 @@ import { } from '@/shared/messages' import { PaymentSession } from './paymentSession' import { emitToggleWM } from '../lib/messages' -import { getCurrentActiveTab, getSender, getTabId } from '../utils' +import { computeRate, getCurrentActiveTab, getSender, getTabId } from '../utils' import { EventsService } from './events' export class MonetizationService { @@ -32,9 +32,9 @@ export class MonetizationService { sender: Runtime.MessageSender ) { const { - connected, enabled, - rateOfPay: rate, + rateOfPay, + connected, walletAddress: connectedWallet } = await this.storage.get([ 'enabled', @@ -43,9 +43,9 @@ export class MonetizationService { 'walletAddress' ]) - if (!rate || !connectedWallet) { + if (!rateOfPay || !connectedWallet) { this.logger.error( - `Did not find rate of pay or connect wallet information. Received rate=${rate}, wallet=${connectedWallet}. Payment session will not be initialized.` + `Did not find rate of pay or connect wallet information. Received rate=${rateOfPay}, wallet=${connectedWallet}. Payment session will not be initialized.` ) return } @@ -55,9 +55,18 @@ export class MonetizationService { this.sessions[tabId] = new Map() } + const sessions = this.sessions[tabId] + const sessionsCount = sessions.size + payload.length + const rate = computeRate(rateOfPay, sessionsCount) + + // Adjust rate of payment for existing sessions + sessions.forEach((session) => { + session.adjustSessionAmount(rate) + }) + + // Initialize new sessions payload.forEach((p) => { const { requestId, walletAddress: receiver } = p - // TODO: Split monetization amount (needs batching) const session = new PaymentSession( receiver, @@ -69,7 +78,7 @@ export class MonetizationService { this.openPaymentsService ) - this.sessions[tabId].set(requestId, session) + sessions.set(requestId, session) if (connected === true && enabled === true) { void session.start() @@ -96,7 +105,7 @@ export class MonetizationService { } } - stopPaymentSession( + async stopPaymentSession( payload: StopMonetizationPayload[], sender: Runtime.MessageSender ) { @@ -111,7 +120,20 @@ export class MonetizationService { payload.forEach((p) => { const { requestId } = p - this.sessions[tabId].get(requestId)?.stop() + sessions.get(requestId)?.stop() + + if (p.remove) { + sessions.delete(requestId) + } + }) + + const { rateOfPay } = await this.storage.get(['rateOfPay']) + if (!rateOfPay) return + + const rate = computeRate(rateOfPay, sessions.size) + // Adjust rate of payment for existing sessions + sessions.forEach((session) => { + session.adjustSessionAmount(rate) }) } @@ -130,7 +152,7 @@ export class MonetizationService { payload.forEach((p) => { const { requestId } = p - this.sessions[tabId].get(requestId)?.resume() + sessions.get(requestId)?.resume() }) } diff --git a/src/background/utils.ts b/src/background/utils.ts index 646bc032..32efb9b4 100644 --- a/src/background/utils.ts +++ b/src/background/utils.ts @@ -85,3 +85,6 @@ export const getSender = (sender: Runtime.MessageSender) => { const frameId = notNullOrUndef(sender.frameId, 'sender.frameId') return { tabId, frameId } } + +export const computeRate = (rate: string, sessionsCount: number) => + (+rate / sessionsCount).toString() diff --git a/src/content/services/monetizationTagManager.ts b/src/content/services/monetizationTagManager.ts index d8fa06df..0fa0838c 100644 --- a/src/content/services/monetizationTagManager.ts +++ b/src/content/services/monetizationTagManager.ts @@ -106,7 +106,7 @@ export class MonetizationTagManager extends EventEmitter { this.sendStopMonetization(stopMonetizationTags) } - private onWholeDocumentObserved(records: MutationRecord[]) { + private async onWholeDocumentObserved(records: MutationRecord[]) { const startMonetizationTagsPromises: Promise[] = [] const stopMonetizationTags: StopMonetizationPayload[] = [] @@ -121,7 +121,7 @@ export class MonetizationTagManager extends EventEmitter { } } - this.sendStopMonetization(stopMonetizationTags) + await this.sendStopMonetization(stopMonetizationTags) if (this.isTopFrame) { for (const record of records) { @@ -216,8 +216,8 @@ export class MonetizationTagManager extends EventEmitter { } } + await this.sendStopMonetization(stopMonetizationTags) this.sendStartMonetization(startMonetizationTags) - this.sendStopMonetization(stopMonetizationTags) } private async checkAdded(node: Node) { @@ -442,10 +442,10 @@ export class MonetizationTagManager extends EventEmitter { } } - private sendStopMonetization(tags: StopMonetizationPayload[]) { + private async sendStopMonetization(tags: StopMonetizationPayload[]) { if (!tags.length) return - stopMonetization(tags) + await stopMonetization(tags) // Check if tab still monetized let validTagsCount = 0