Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add intent to background & split amount #307

Merged
merged 6 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
dianafulga marked this conversation as resolved.
Show resolved Hide resolved

// Check if tab still monetized
let validTagsCount = 0
Expand Down
Loading