Skip to content

Commit

Permalink
Address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
raducristianpopa committed Jun 7, 2024
1 parent afff06a commit 4b237b0
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 32 deletions.
6 changes: 3 additions & 3 deletions src/background/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import {
StorageService,
MonetizationService,
Background,
TabEvents
TabEvents,
EventsService,
Deduplicator
} from './services'
import { createLogger, Logger } from '@/shared/logger'
import { LOG_LEVEL } from '@/shared/defines'
import { EventsService } from './services/events'
import { Deduplicator } from './services/deduplicator'

interface Cradle {
logger: Logger
Expand Down
14 changes: 7 additions & 7 deletions src/background/services/deduplicator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,25 @@ interface CacheEntry {

interface DedupeOptions {
cacheFnArgs: boolean
wait: number
}

export class Deduplicator {
private cache: Map<string, CacheEntry> = new Map()
private readonly duration = 5000

constructor(private logger: Logger) {}

dedupe<T extends AsyncFn<any>>(
fn: T,
options: DedupeOptions = { cacheFnArgs: false }
{ cacheFnArgs = false, wait = 5000 }: Partial<DedupeOptions> = {}
): T {
return ((...args: Parameters<T>): ReturnType<T> => {
const key = this.generateCacheKey(fn, args, options.cacheFnArgs)
const key = this.generateCacheKey(fn, args, cacheFnArgs)
const entry = this.cache.get(key)

if (entry) {
this.logger.debug(
`Deduping function=${fn.name}, ${options.cacheFnArgs ? 'args=' + JSON.stringify(args) : 'without args'}`
`Deduping function=${fn.name}, ${cacheFnArgs ? 'args=' + JSON.stringify(args) : 'without args'}`
)
return entry.promise as ReturnType<T>
}
Expand All @@ -42,7 +42,7 @@ export class Deduplicator {
.catch((err) => {
throw err
})
.finally(() => this.scheduleCacheClear(key))
.finally(() => this.scheduleCacheClear(key, wait))

return promise as ReturnType<T>
}) as unknown as T
Expand All @@ -60,14 +60,14 @@ export class Deduplicator {
return key
}

private scheduleCacheClear(key: string): void {
private scheduleCacheClear(key: string, wait: number): void {
setTimeout(() => {
this.logger.debug(`Attempting to remove key=${key} from cache.`)
const entry = this.cache.get(key)
if (entry) {
this.logger.debug(`Removing key=${key} from cache.`)
this.cache.delete(key)
}
}, this.duration)
}, wait)
}
}
20 changes: 8 additions & 12 deletions src/background/services/openPayments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -524,22 +524,18 @@ export class OpenPaymentsService {
}

async rotateToken() {
const rotate = this.deduplicator.dedupe(this.client!.token.rotate, {
cacheFnArgs: false
})
const token = await rotate({
const rotate = this.deduplicator.dedupe(this.client!.token.rotate)
const newToken = await rotate({
url: this.token.manage,
accessToken: this.token.value
})
const token = {
value: newToken.access_token.value,
manage: newToken.access_token.manage
}
await this.storage.set({
token: {
value: token.access_token.value,
manage: token.access_token.manage
}
token
})
this.token = {
value: token.access_token.value,
manage: token.access_token.manage
}
this.token = token
}
}
9 changes: 5 additions & 4 deletions src/background/services/paymentSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,23 +120,24 @@ export class PaymentSession {

await this.setIncomingPaymentUrl()

while (this.active) {
let quote: Quote | undefined
let outgoingPayment: OutgoingPayment | undefined
let quote: Quote | undefined
let outgoingPayment: OutgoingPayment | undefined

while (this.active) {
try {
// Quote can be removed once the Test Wallet upgrades to alpha-10.
// We will be able to create an outgoing payment with an incoming payment,
// making the quoting unnecessary through OP.
//
// Note: Under the hood, Rafiki is still quoting.
if (!quote) {
if (!quote) {
quote = await this.openPaymentsService.createQuote({
walletAddress: this.sender,
receiver: this.incomingPaymentUrl,
amount: this.amount
})
}

outgoingPayment = await this.openPaymentsService.createOutgoingPayment({
walletAddress: this.sender,
quoteId: quote.id
Expand Down
7 changes: 1 addition & 6 deletions src/popup/components/ui/RadioGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,7 @@ export const Radio = ({

<label htmlFor={inputId} className="group flex items-center">
<span
className={`inline-block h-6 w-6 rounded-full border-2 border-base
peer-checked:group-[]:border-blue-500
peer-checked:group-[]:bg-primary
peer-checked:group-[]:ring-4
peer-checked:group-[]:ring-inset
peer-checked:group-[]:ring-white`}
className={`inline-block h-6 w-6 rounded-full border-2 border-base peer-checked:group-[]:border-blue-500 peer-checked:group-[]:bg-primary peer-checked:group-[]:ring-4 peer-checked:group-[]:ring-inset peer-checked:group-[]:ring-white`}
/>
{label ? (
<p className="ms-2 text-base leading-6 text-medium">{label}</p>
Expand Down

0 comments on commit 4b237b0

Please sign in to comment.