Skip to content
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
6 changes: 6 additions & 0 deletions src/background/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import {
EventsService,
Heartbeat,
Deduplicator,
PaymentSession,
PaymentManager,
} from './services';
import { createLogger, type Logger, type RootLogger } from '@/shared/logger';
import { LOG_LEVEL } from '@/shared/defines';
Expand Down Expand Up @@ -59,6 +61,8 @@ export interface Cradle {
tabState: TabState;
windowState: WindowState;
heartbeat: Heartbeat;
PaymentSession: typeof PaymentSession;
PaymentManager: typeof PaymentManager;
}

export const configureContainer = () => {
Expand Down Expand Up @@ -134,6 +138,8 @@ export const configureContainer = () => {
logger: logger.getLogger('window-state'),
})),
heartbeat: asClass(Heartbeat).singleton(),
PaymentSession: asValue(PaymentSession),
PaymentManager: asValue(PaymentManager),
});

return container;
Expand Down
2 changes: 2 additions & 0 deletions src/background/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ export { SendToPort } from './sendToPort';
export { EventsService } from './events';
export { Deduplicator } from './deduplicator';
export { Heartbeat } from './heartbeat';
export { PaymentManager } from './paymentManager';
export { PaymentSession } from './paymentSession';
10 changes: 8 additions & 2 deletions src/background/services/monetization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import type {
StartMonetizationPayload,
StopMonetizationPayload,
} from '@/shared/messages';
import { PaymentManager } from './paymentManager';
import { getSender, getTabId } from '@/background/utils';
import { OUTGOING_PAYMENT_POLLING_MAX_DURATION } from '@/background/config';
import {
Expand All @@ -29,6 +28,8 @@ export class MonetizationService {
private tabState: Cradle['tabState'];
private windowState: Cradle['windowState'];
private message: Cradle['message'];
private PaymentSession: Cradle['PaymentSession'];
private PaymentManager: Cradle['PaymentManager'];

constructor({
logger,
Expand All @@ -41,6 +42,8 @@ export class MonetizationService {
tabState,
windowState,
message,
PaymentSession,
PaymentManager,
}: Cradle) {
Object.assign(this, {
logger,
Expand All @@ -53,6 +56,8 @@ export class MonetizationService {
tabState,
windowState,
message,
PaymentSession,
PaymentManager,
});

this.registerEventListeners();
Expand Down Expand Up @@ -93,7 +98,7 @@ export class MonetizationService {
let paymentManager = this.tabState.paymentManagers.get(tabId);
if (!paymentManager) {
const url = removeQueryParams(this.tabState.url.get(tabId) || fullUrl!);
paymentManager = new PaymentManager(
paymentManager = new this.PaymentManager(
tabId,
url,
connectedWallet,
Expand All @@ -109,6 +114,7 @@ export class MonetizationService {
),
rootLogger: this.rootLogger,
message: this.message,
PaymentSession: this.PaymentSession,
},
);
this.tabState.paymentManagers.set(tabId, paymentManager);
Expand Down
11 changes: 5 additions & 6 deletions src/background/services/paymentManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type {
WalletAddress,
} from '@interledger/open-payments';
import type { Cradle as Cradle_ } from '@/background/container';
import { PaymentSession } from './paymentSession';
import type { PaymentSession } from './paymentSession';
import {
MIN_PAYMENT_WAIT,
OUTGOING_PAYMENT_POLLING_MAX_ATTEMPTS,
Expand All @@ -17,7 +17,6 @@ import {
sleep,
Timeout,
} from '@/shared/helpers';
import { isOutOfBalanceError } from './openPayments';

type Cradle = Pick<
Cradle_,
Expand All @@ -29,6 +28,7 @@ type Cradle = Pick<
| 'events'
| 'tabState'
| 'message'
| 'PaymentSession'
>;

/** Payable amount to increase by {@linkcode Interval.units} every {@linkcode Interval.duration}ms. */
Expand Down Expand Up @@ -159,7 +159,6 @@ export class PaymentManager {
this.tabId,
this.sender,
this.rootLogger,
PaymentSession,
this.deps,
);
this.streams.set(frameId, stream);
Expand Down Expand Up @@ -202,6 +201,7 @@ export class PaymentManager {
payableSessions: PaymentSession[],
signal?: AbortSignal,
) {
const { isOutOfBalanceError } = await import('./openPayments');
Comment thread
sidvishnoi marked this conversation as resolved.
const outgoingPayments = new Map<string, OutgoingPayment | null>(
payableSessions.map((session, i) => [
session.id,
Expand Down Expand Up @@ -497,8 +497,7 @@ export class PaymentStream {
private readonly tabId: TabId,
private sender: WalletAddress,
private rootLogger: Cradle['rootLogger'],
private PaymentSessionConstructor: typeof PaymentSession,
private deps: ConstructorParameters<typeof PaymentSession>[6],
private deps: Cradle,
) {
this.iter = this.sessionsIter(this);
}
Expand All @@ -510,7 +509,7 @@ export class PaymentStream {
session.enable(); // if was disabled earlier
return session;
}
session = new this.PaymentSessionConstructor(
session = new this.deps.PaymentSession(
receiver,
sessionId,
this.tabId,
Expand Down