fix: refresh favorite identity from market bridge
This commit is contained in:
@@ -138,6 +138,7 @@ export interface CreateMarketControllerOptions {
|
||||
|
||||
const AUDIENCE_PROFILE_FIELD_SELECTION_STORAGE_KEY =
|
||||
"sces:audience-profile:selectedHeaders";
|
||||
const SERIALIZED_MARKET_ROWS_ATTRIBUTE = "data-sces-market-rows";
|
||||
|
||||
export function createMarketController(options: CreateMarketControllerOptions) {
|
||||
const marketApiClient = createMarketApiClient();
|
||||
@@ -226,6 +227,7 @@ export function createMarketController(options: CreateMarketControllerOptions) {
|
||||
let activeSort: MarketSortState | undefined;
|
||||
let isDisposed = false;
|
||||
let lastKnownPageSignature = "";
|
||||
let lastKnownFavoriteIdentitySignature = readFavoriteIdentitySignature(options.document);
|
||||
let needsResync = false;
|
||||
let scheduledSyncTimeoutId: number | null = null;
|
||||
let activeSyncPromise: Promise<void> | null = null;
|
||||
@@ -268,6 +270,9 @@ export function createMarketController(options: CreateMarketControllerOptions) {
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
const nextFavoriteIdentitySignature = readFavoriteIdentitySignature(options.document);
|
||||
const favoriteIdentityChanged =
|
||||
nextFavoriteIdentitySignature !== lastKnownFavoriteIdentitySignature;
|
||||
|
||||
const toolbarNeedsRemount =
|
||||
!toolbar || !isPluginToolbarMounted(toolbar.root, options.document);
|
||||
@@ -291,7 +296,8 @@ export function createMarketController(options: CreateMarketControllerOptions) {
|
||||
!needsToolbarRemount &&
|
||||
!selectionControlsMissing &&
|
||||
!favoriteControlsMissing &&
|
||||
!needsUnavailableFavoritesRecovery
|
||||
!needsUnavailableFavoritesRecovery &&
|
||||
!favoriteIdentityChanged
|
||||
) {
|
||||
return;
|
||||
}
|
||||
@@ -303,6 +309,10 @@ export function createMarketController(options: CreateMarketControllerOptions) {
|
||||
if (favoriteControlsMissing || needsUnavailableFavoritesRecovery) {
|
||||
needsFavoritesRefresh = true;
|
||||
}
|
||||
if (favoriteIdentityChanged) {
|
||||
lastKnownFavoriteIdentitySignature = nextFavoriteIdentitySignature;
|
||||
needsFavoritesRefresh = true;
|
||||
}
|
||||
scheduleSync();
|
||||
});
|
||||
const observationRoot = options.document.body ?? options.document.documentElement;
|
||||
@@ -1870,10 +1880,25 @@ export function createMarketController(options: CreateMarketControllerOptions) {
|
||||
return;
|
||||
}
|
||||
|
||||
const documentElement = options.document.documentElement;
|
||||
if (observationRoot === documentElement) {
|
||||
observer.observe(observationRoot, {
|
||||
attributeFilter: [SERIALIZED_MARKET_ROWS_ATTRIBUTE],
|
||||
attributes: true,
|
||||
childList: true,
|
||||
subtree: true
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
observer.observe(observationRoot, {
|
||||
childList: true,
|
||||
subtree: true
|
||||
});
|
||||
observer.observe(documentElement, {
|
||||
attributeFilter: [SERIALIZED_MARKET_ROWS_ATTRIBUTE],
|
||||
attributes: true
|
||||
});
|
||||
}
|
||||
|
||||
function runSyncCycle(): Promise<void> {
|
||||
@@ -1929,6 +1954,10 @@ export function createMarketController(options: CreateMarketControllerOptions) {
|
||||
|
||||
}
|
||||
|
||||
function readFavoriteIdentitySignature(document: Document): string {
|
||||
return document.documentElement.getAttribute(SERIALIZED_MARKET_ROWS_ATTRIBUTE) ?? "";
|
||||
}
|
||||
|
||||
function readNativeToolbarActionCount(document: Document): number {
|
||||
return Array.from(document.querySelectorAll("button, a, [role='button']")).filter(
|
||||
(element) => {
|
||||
|
||||
@@ -4658,8 +4658,8 @@ describe("market-content-entry", () => {
|
||||
});
|
||||
|
||||
test("preserves a late serialized core user id through favorite batch import", async () => {
|
||||
document.body.innerHTML = buildRealMarketFixtureWithoutAuthorIds([
|
||||
{ authorName: "达人 A", price21To60s: "¥11,000" }
|
||||
document.body.innerHTML = buildRealMarketFixture([
|
||||
{ authorId: "111", authorName: "达人 A", price21To60s: "¥11,000" }
|
||||
]);
|
||||
const repository = createTestFavoritesRepository();
|
||||
const observer = createMutationObserverFactory();
|
||||
@@ -4687,16 +4687,8 @@ describe("market-content-entry", () => {
|
||||
{ authorId: "111", authorName: "达人 A", coreUserId: "core-111" }
|
||||
])
|
||||
);
|
||||
document.documentElement.setAttribute("data-test-page-index", "2");
|
||||
observer.trigger();
|
||||
await waitForCondition(
|
||||
() => {
|
||||
const favoriteButton = document.querySelector(
|
||||
'[data-sces-favorite-row-action="button"]'
|
||||
);
|
||||
return favoriteButton instanceof HTMLButtonElement && !favoriteButton.disabled;
|
||||
}
|
||||
);
|
||||
await flushWithTimers();
|
||||
|
||||
click('[data-sces-favorite-row-action="button"]');
|
||||
click('[data-sces-favorite-row-picker="create-folder"]');
|
||||
|
||||
Reference in New Issue
Block a user