From 3eb5dc6f9de174d49913061083833ad699855ee4 Mon Sep 17 00:00:00 2001 From: Kuba Orlik Date: Sun, 21 Nov 2021 23:00:55 +0100 Subject: [PATCH] Improve marking performance --- sidebar/stolen-data-cluster.tsx | 71 ++++++++++++++++++++++----------- stolen-data-entry.ts | 26 +++++++++--- util.ts | 1 - 3 files changed, 69 insertions(+), 29 deletions(-) diff --git a/sidebar/stolen-data-cluster.tsx b/sidebar/stolen-data-cluster.tsx index 9fefac4..35521c2 100644 --- a/sidebar/stolen-data-cluster.tsx +++ b/sidebar/stolen-data-cluster.tsx @@ -1,8 +1,13 @@ import React from "react"; import { getMemory } from "../memory"; -import { MergedStolenDataEntry, Sources } from "../stolen-data-entry"; +import { RequestCluster } from "../request-cluster"; +import { + MergedStolenDataEntry, + Sources, + StolenDataEntry, +} from "../stolen-data-entry"; -import { hyphenate } from "../util"; +import { hyphenate, useEmitter } from "../util"; const MAX_STRING_VALUE_LENGTH = 100; @@ -51,6 +56,7 @@ function StolenDataValue({ entry: MergedStolenDataEntry; prefixKey?: string; }) { + const [version] = useEmitter(entry); const value = entry.getParsedValues(prefixKey)[0]; let body = null; if (!value) { @@ -64,6 +70,7 @@ function StolenDataValue({ ? "2px solid red" : "2px solid transparent", }} + data-version={version} > {content.slice(0, MAX_STRING_VALUE_LENGTH)}{" "} {content.length > MAX_STRING_VALUE_LENGTH ? "(...)" : ""} @@ -92,6 +99,38 @@ const icons: Record = { header: "H", }; +function StolenDataRow({ + entry, + cluster, +}: { + entry: MergedStolenDataEntry; + cluster: RequestCluster; +}) { + const [version] = useEmitter(entry); + return ( + + entry.toggleMark("")} + > + {entry.getNames().map(hyphenate).join(", ")} + + {entry.getSources().map((source) => icons[source])} + + + + + ); +} + export default function StolenDataCluster({ origin, shorthost, @@ -130,27 +169,13 @@ export default function StolenDataCluster({ {cluster .getStolenData({ minValueLength, cookiesOnly, cookiesOrOriginOnly }) .map((entry) => ( - - entry.toggleMark("")} - > - {entry.getNames().map(hyphenate).join(", ")} - - {entry.getSources().map((source) => icons[source])} - - - - + ))} diff --git a/stolen-data-entry.ts b/stolen-data-entry.ts index 503ad60..ac5ce6d 100644 --- a/stolen-data-entry.ts +++ b/stolen-data-entry.ts @@ -1,7 +1,7 @@ import { TCModel } from "@iabtcf/core"; +import { EventEmitter } from "events"; import ExtendedRequest, { HAREntry } from "./extended-request"; import Mark from "./mark"; -import { getMemory } from "./memory"; import { getshorthost, isJSONObject, @@ -29,7 +29,7 @@ const id = (function* id() { } })(); -export class StolenDataEntry { +export class StolenDataEntry extends EventEmitter { public isIAB = false; public iab: TCModel | null = null; public id: number; @@ -47,6 +47,7 @@ export class StolenDataEntry { // // console.log(this.iab); // this.isIAB = true; // } catch (e) {} + super(); this.id = id.next().value as number; this.classification = this.classify(); } @@ -127,7 +128,7 @@ export class StolenDataEntry { addMark(key: string) { this.marks.push(new Mark(this, key)); - getMemory().emit("change", true); // to trigger rerender + this.emit("change"); } hasMark(key?: string) { @@ -140,7 +141,7 @@ export class StolenDataEntry { removeMark(key: string) { this.marks = this.marks.filter((mark) => mark.key != key); - getMemory().emit("change", true); // to trigger rerender + this.emit("change"); } toggleMark(key: string) { @@ -194,8 +195,9 @@ export class StolenDataEntry { } } -export class MergedStolenDataEntry { +export class MergedStolenDataEntry extends EventEmitter { constructor(public entries: StolenDataEntry[]) { + super(); const all_marks = unique( entries.map((entry) => entry.marks).reduce(reduceConcat, []) ); @@ -205,6 +207,20 @@ export class MergedStolenDataEntry { // getMemory().emit("change"); // to trigger render } + on(event: string, listener: () => void) { + for (const entry of this.entries) { + entry.on(event, listener); + } + return this; + } + + removeListener(event: string, listener: () => void) { + for (const entry of this.entries) { + entry.removeListener(event, listener); + } + return this; + } + hasValue(value: string) { return this.entries.some((entry) => entry.value === value); } diff --git a/util.ts b/util.ts index 2b5340c..bbb69e3 100644 --- a/util.ts +++ b/util.ts @@ -1,6 +1,5 @@ import { EventEmitter } from "events"; import { Dispatch, SetStateAction, useEffect, useState } from "react"; -import Memory from "./memory"; export type Unpromisify = T extends Promise ? R : T; export type Unarray = T extends Array ? R : T;