Skip to content

Commit

Permalink
Move active search rows to PlexClientState
Browse files Browse the repository at this point in the history
Remove PlexClientState's dependency on PlexUI by moving PlexUI's active search
rows to PlexClientState. Where they belong is debatable, but the distinction
between the classes is already fuzzy at best.

Reduces the number of circular dependencies from 2 to 1.
  • Loading branch information
danrahn committed Mar 10, 2024
1 parent fb3d3ea commit 8119454
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 25 deletions.
30 changes: 22 additions & 8 deletions Client/Script/PlexClientState.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { addWindowResizedListener } from './WindowResizeEventHandler.js';
import { BulkActionType } from './BulkActionCommon.js';
import { ClientMovieData } from './ClientDataExtensions.js';
import { ClientSettings } from './ClientSettings.js';
import { PlexUI } from './PlexUI.js';
import { ServerCommands } from './Commands.js';

/** @typedef {!import('../../Shared/PlexTypes').MarkerDataMap} MarkerDataMap */
Expand Down Expand Up @@ -53,7 +52,9 @@ class PlexClientStateManager {
/** @type {{[sectionId: number]: { items: ShowMap|MovieMap, searchTokens: SearchTokenMaps }}} */
#sections = {};
/** @type {ShowData[]|ClientMovieData[]} */
activeSearchUnfiltered = [];
#activeSearchUnfiltered = [];
/** @type {ShowResultRow[]|MovieResultRow[]} */
#activeSearchFiltered = [];
/** @type {ShowResultRow} */
#activeShow;
/** @type {SeasonResultRow} */
Expand All @@ -80,7 +81,7 @@ class PlexClientStateManager {
if (Instance.#activeSectionType === SectionType.Movie) {
/** @type {MovieResultRow} */
let searchRow;
for (searchRow of PlexUI.getActiveSearchRows()) {
for (searchRow of Instance.#activeSearchFiltered) {
searchRow.updateMarkerBreakdown();
}
}
Expand Down Expand Up @@ -112,7 +113,20 @@ class PlexClientStateManager {

/** @returns The list of shows that match the current search. */
getUnfilteredSearchResults() {
return this.activeSearchUnfiltered;
return this.#activeSearchUnfiltered;
}

/**
* Retrieve all result rows in the search result list. */
getActiveSearchRows() {
return this.#activeSearchFiltered;
}

/**
* Set the currently active search rows.
* @param {{ShowResultRow[]|MovieResultRow[]}} rows */
setActiveSearchRows(rows) {
this.#activeSearchFiltered = rows;
}

/**
Expand Down Expand Up @@ -425,7 +439,7 @@ class PlexClientStateManager {
return this.#defaultSort(a, b);
});

this.activeSearchUnfiltered = resultArr;
this.#activeSearchUnfiltered = resultArr;
}

/**
Expand Down Expand Up @@ -507,7 +521,7 @@ class PlexClientStateManager {
// Update any visible movies
/** @type {MovieResultRow} */
let searchRow;
for (searchRow of PlexUI.getActiveSearchRows()) {
for (searchRow of this.#activeSearchFiltered) {
const metadataId = searchRow.mediaItem().metadataId;
activeIds.add(metadataId);
const newItems = unpurged.get(metadataId);
Expand All @@ -531,7 +545,7 @@ class PlexClientStateManager {
// Update non-active shows (as they're all cached for quick search results)
/** @type {ShowResultRow} */
let searchRow;
for (searchRow of PlexUI.getActiveSearchRows()) {
for (searchRow of this.#activeSearchFiltered) {
activeIds.add(searchRow.mediaItem().metadataId);
if (unpurged.get(searchRow.mediaItem().metadataId)) {
Log.verbose(`Updating search result show row ${searchRow.show().title} after purge update.`);
Expand Down Expand Up @@ -625,7 +639,7 @@ class PlexClientStateManager {

const affectedShows = Object.keys(markers).length;
Log.assert(affectedShows <= 1, `Bulk actions should target a single show, found ${affectedShows}`);
for (const searchRow of PlexUI.getActiveSearchRows()) {
for (const searchRow of this.#activeSearchFiltered) {
if (markers[searchRow.show().metadataId]) {
return this.updateNonActiveBreakdown(searchRow, []);
}
Expand Down
27 changes: 12 additions & 15 deletions Client/Script/PlexUI.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,6 @@ class PlexUIManager {
* @type {number} */
#searchTimer;

/** @type {ShowResultRow[]|MovieResultRow[]} */
#activeSearch = [];

#lastSort = {
by : SortConditions.Alphabetical,
order : SortOrder.Ascending
Expand Down Expand Up @@ -285,12 +282,6 @@ class PlexUIManager {
this.showSections(uiSections);
}

/**
* Retrieve all ShowResultRows in the search result list. */
getActiveSearchRows() {
return this.#activeSearch;
}

async #libraryChanged() {
this.#searchContainer.classList.add('hidden');
this.#lastSearch = null;
Expand Down Expand Up @@ -436,7 +427,8 @@ class PlexUIManager {
return;
}

this.#activeSearch = [];
/** @type {MovieResultRow[]} */
const filteredResults = [];
const rowsLimit = 250; // Most systems should still be fine with this. Even 1000 might not be horrible, but play it safe.
let nonFiltered = 0;
let nextFilterIndex = 0;
Expand All @@ -445,7 +437,7 @@ class PlexUIManager {
if (!FilterSettings.shouldFilter(movie.markerBreakdown())) {
++nonFiltered;
const newRow = new MovieResultRow(movie);
this.#activeSearch.push(newRow);
filteredResults.push(newRow);
movieList.appendChild(newRow.buildRow());
}

Expand All @@ -465,7 +457,7 @@ class PlexUIManager {
for (const movie of searchResults.slice(nextFilterIndex)) {
if (!FilterSettings.shouldFilter(movie.markerBreakdown())) {
const newRow = new MovieResultRow(movie);
this.#activeSearch.push(newRow);
filteredResults.push(newRow);
movieList.appendChild(newRow.buildRow());
}
}
Expand All @@ -480,6 +472,8 @@ class PlexUIManager {
text,
{ click : loadTheRest }));
}

PlexClientState.setActiveSearchRows(filteredResults);
}

#searchShows(forFilterReapply=false) {
Expand All @@ -501,18 +495,21 @@ class PlexUIManager {
return;
}

this.#activeSearch = [];
/** @type {ShowResultRow[]} */
const filteredResults = [];
for (const show of searchResults) {
if (!FilterSettings.shouldFilter(show.markerBreakdown())) {
const newRow = new ShowResultRow(show);
this.#activeSearch.push(newRow);
filteredResults.push(newRow);
showList.appendChild(newRow.buildRow());
}
}

if (this.#activeSearch.length === 0) {
if (filteredResults.length === 0) {
showList.appendChild(this.noResultsBecauseOfFilterRow());
}

PlexClientState.setActiveSearchRows(filteredResults);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions Client/Script/ResultRow.js
Original file line number Diff line number Diff line change
Expand Up @@ -1712,7 +1712,7 @@ class MovieResultRow extends BaseItemResultRow {
* @param {boolean} hide Whether to hide or show all marker tables. */
showHideMarkerTables(hide) {
/** @type {MovieResultRow[]} */
const movies = PlexUI.getActiveSearchRows();
const movies = PlexClientState.getActiveSearchRows();
if (!hide) {
// Check how many requests for markers we'll have to make if we try
// to expand everything. If it's greater than 100, ignore the bulk request.
Expand All @@ -1730,7 +1730,7 @@ class MovieResultRow extends BaseItemResultRow {
}
}

return BaseItemResultRow.ShowHideMarkerTables(hide, PlexUI.getActiveSearchRows());
return BaseItemResultRow.ShowHideMarkerTables(hide, PlexClientState.getActiveSearchRows());
}


Expand Down

0 comments on commit 8119454

Please sign in to comment.