Skip to content

Commit

Permalink
feat: Add intent to background & split amount (#307)
Browse files Browse the repository at this point in the history
* Add intent to background

* Split amount

* Udate rate of pay when stopping sessions

* Solve bug

* Prevent race conditions on stop and start
  • Loading branch information
dianafulga authored Jun 11, 2024
1 parent 5ba9ca2 commit d091c53
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 15 deletions.
42 changes: 32 additions & 10 deletions src/background/services/monetization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -32,9 +32,9 @@ export class MonetizationService {
sender: Runtime.MessageSender
) {
const {
connected,
enabled,
rateOfPay: rate,
rateOfPay,
connected,
walletAddress: connectedWallet
} = await this.storage.get([
'enabled',
Expand All @@ -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
}
Expand All @@ -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,
Expand All @@ -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()
Expand All @@ -96,7 +105,7 @@ export class MonetizationService {
}
}

stopPaymentSession(
async stopPaymentSession(
payload: StopMonetizationPayload[],
sender: Runtime.MessageSender
) {
Expand All @@ -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)
})
}

Expand All @@ -130,7 +152,7 @@ export class MonetizationService {
payload.forEach((p) => {
const { requestId } = p

this.sessions[tabId].get(requestId)?.resume()
sessions.get(requestId)?.resume()
})
}

Expand Down
3 changes: 3 additions & 0 deletions src/background/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
10 changes: 5 additions & 5 deletions src/content/services/monetizationTagManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export class MonetizationTagManager extends EventEmitter {
this.sendStopMonetization(stopMonetizationTags)
}

private onWholeDocumentObserved(records: MutationRecord[]) {
private async onWholeDocumentObserved(records: MutationRecord[]) {
const startMonetizationTagsPromises: Promise<StartMonetizationPayload | null>[] =
[]
const stopMonetizationTags: StopMonetizationPayload[] = []
Expand All @@ -121,7 +121,7 @@ export class MonetizationTagManager extends EventEmitter {
}
}

this.sendStopMonetization(stopMonetizationTags)
await this.sendStopMonetization(stopMonetizationTags)

if (this.isTopFrame) {
for (const record of records) {
Expand Down Expand Up @@ -216,8 +216,8 @@ export class MonetizationTagManager extends EventEmitter {
}
}

await this.sendStopMonetization(stopMonetizationTags)
this.sendStartMonetization(startMonetizationTags)
this.sendStopMonetization(stopMonetizationTags)
}

private async checkAdded(node: Node) {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d091c53

Please sign in to comment.