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

fix: fix regressions caused by forEach to for..of migration #884

Merged
merged 2 commits into from
Jan 31, 2025
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
4 changes: 2 additions & 2 deletions src/background/services/monetization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export class MonetizationService {
this.canTryPayment(connected, state)
) {
for (const session of sessionsArr) {
if (!sessions.get(session.id)) return;
if (!sessions.get(session.id)) continue;
const source = replacedSessions.has(session.id)
? 'request-id-reused'
: 'new-link';
Expand Down Expand Up @@ -181,7 +181,7 @@ export class MonetizationService {
const { requestId } = p;

const session = sessions.get(requestId);
if (!session) return;
if (!session) continue;

if (p.intent === 'remove') {
needsAdjustAmount = true;
Expand Down
2 changes: 1 addition & 1 deletion src/content/services/frameManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export class FrameManager {
private onWholeDocumentObserved(records: MutationRecord[]) {
for (const record of records) {
if (record.type === 'childList') {
for (const node of record.addedNodes) {
for (const node of record.removedNodes) {
this.check('removed', node);
}
}
Expand Down
53 changes: 29 additions & 24 deletions src/content/services/monetizationLinkManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
MonetizationEventPayload,
ResumeMonetizationPayload,
StartMonetizationPayload,
StartMonetizationPayloadEntry,
StopMonetizationPayload,
StopMonetizationPayloadEntry,
} from '@/shared/messages';
Expand Down Expand Up @@ -358,31 +359,24 @@ export class MonetizationLinkManager extends EventEmitter {
};

private async onWholeDocumentObserved(records: MutationRecord[]) {
const stopMonetizationPayload: StopMonetizationPayload = [];

for (const record of records) {
if (record.type === 'childList') {
for (const node of record.removedNodes) {
if (!(node instanceof HTMLLinkElement)) return;
if (!this.monetizationLinks.has(node)) return;
const payloadEntry = this.onRemovedLink(node);
stopMonetizationPayload.push(payloadEntry);
}
}
}

const childListRecords = records.filter((e) => e.type === 'childList');
const removedNodes = childListRecords.flatMap((e) => [...e.removedNodes]);
const allRemovedLinkTags = removedNodes.map((node) =>
this.onRemovedNode(node),
);
const stopMonetizationPayload: StopMonetizationPayload = allRemovedLinkTags
.filter(isNotNull)
.flat();
await this.sendStopMonetization(stopMonetizationPayload);

if (this.isTopFrame) {
const addedNodes = records
.filter((e) => e.type === 'childList')
.flatMap((e) => [...e.addedNodes]);
const addedNodes = childListRecords.flatMap((e) => [...e.addedNodes]);
const allAddedLinkTags = await Promise.all(
addedNodes.map((node) => this.onAddedNode(node)),
);
const startMonetizationPayload = allAddedLinkTags
.filter(isNotNull)
.map(({ details }) => details);
.flat();

void this.sendStartMonetization(startMonetizationPayload);
}
Expand Down Expand Up @@ -492,24 +486,35 @@ export class MonetizationLinkManager extends EventEmitter {
void this.sendStartMonetization(startMonetizationPayload);
}

private async onAddedNode(node: Node) {
private async onAddedNode(
node: Node,
): Promise<StartMonetizationPayload | null> {
if (node instanceof HTMLElement) {
this.dispatchOnMonetizationAttrChangedEvent(node);
}

if (node instanceof HTMLLinkElement) {
return await this.onAddedLink(node);
const payloadEntry = await this.onAddedLink(node);
return payloadEntry ? [payloadEntry] : null;
}
return null;
}

private async onAddedLink(link: HTMLLinkElement) {
private onRemovedNode(node: Node): StopMonetizationPayload | null {
if (node instanceof HTMLLinkElement) {
return [this.onRemovedLink(node)];
}
return null;
sidvishnoi marked this conversation as resolved.
Show resolved Hide resolved
}

private async onAddedLink(
link: HTMLLinkElement,
): Promise<StartMonetizationPayloadEntry | null> {
this.observeLinkAttrs(link);
const res = await this.checkLink(link);
if (res) {
this.monetizationLinks.set(link, res.details);
}
return res;
if (!res) return null;
this.monetizationLinks.set(link, res.details);
return res.details;
}

private onRemovedLink(link: HTMLLinkElement): StopMonetizationPayloadEntry {
Expand Down