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 2 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
27 changes: 19 additions & 8 deletions src/background/services/monetization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export class MonetizationService {
const {
enabled,
rateOfPay,
connected,
walletAddress: connectedWallet
} = await this.storage.get([
'enabled',
Expand All @@ -49,16 +50,17 @@ export class MonetizationService {
return
}
const { tabId, frameId } = getSender(sender)
const sessions = this.sessions[tabId]

if (this.sessions[tabId] == null) {
if (sessions == null) {
this.sessions[tabId] = new Map()
}

const sessionsCount = this.sessions[tabId].size + payload.length
const sessionsCount = sessions.size + payload.length
dianafulga marked this conversation as resolved.
Show resolved Hide resolved
const rate = computeRate(rateOfPay, sessionsCount)

// Adjust rate of payment for existing sessions
this.sessions[tabId].forEach((session) => {
sessions.forEach((session) => {
session.adjustSessionAmount(rate)
})

Expand All @@ -76,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 @@ -103,7 +105,7 @@ export class MonetizationService {
}
}

stopPaymentSession(
async stopPaymentSession(
payload: StopMonetizationPayload[],
sender: Runtime.MessageSender
) {
Expand All @@ -118,12 +120,21 @@ export class MonetizationService {
payload.forEach((p) => {
const { requestId } = p

this.sessions[tabId].get(requestId)?.stop()
sessions.get(requestId)?.stop()

if (p.remove) {
this.sessions[tabId].delete(requestId)
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)
})
}

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

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

Expand Down
2 changes: 1 addition & 1 deletion src/content/services/monetizationTagManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,8 @@ export class MonetizationTagManager extends EventEmitter {
}
}

this.sendStartMonetization(startMonetizationTags)
this.sendStopMonetization(stopMonetizationTags)
this.sendStartMonetization(startMonetizationTags)
dianafulga marked this conversation as resolved.
Show resolved Hide resolved
}

private async checkAdded(node: Node) {
Expand Down
Loading