refactor: use Map for heads

Signed-off-by: Varun Patil <radialapps@gmail.com>
pull/900/head
Varun Patil 2023-10-28 01:31:04 -07:00
parent 18b8a5404f
commit a151d9cbd4
2 changed files with 18 additions and 18 deletions

View File

@ -145,7 +145,7 @@ export default defineComponent({
props: { props: {
heads: { heads: {
type: Object as PropType<{ [dayid: number]: IHeadRow }>, type: Map as PropType<Map<number, IHeadRow>>,
required: true, required: true,
}, },
/** List of rows for multi selection */ /** List of rows for multi selection */
@ -587,7 +587,7 @@ export default defineComponent({
// Update heads // Update heads
for (const dayid of updatedDays) { for (const dayid of updatedDays) {
this.updateHeadSelected(this.heads[dayid]); this.updateHeadSelected(this.heads.get(dayid)!);
} }
this.$forceUpdate(); this.$forceUpdate();
@ -612,7 +612,7 @@ export default defineComponent({
} }
if (!noUpdate) { if (!noUpdate) {
this.updateHeadSelected(this.heads[photo.dayid]); this.updateHeadSelected(this.heads.get(photo.dayid)!);
this.$forceUpdate(); this.$forceUpdate();
} }
}, },
@ -669,7 +669,7 @@ export default defineComponent({
}); });
behind.forEach((p) => this.selectPhoto(p, true, true)); behind.forEach((p) => this.selectPhoto(p, true, true));
updateDaySet.forEach((d) => this.updateHeadSelected(this.heads[d])); updateDaySet.forEach((dayid) => this.updateHeadSelected(this.heads.get(dayid)!));
this.$forceUpdate(); this.$forceUpdate();
} }
}, },
@ -713,7 +713,7 @@ export default defineComponent({
const heads = new Set<IHeadRow>(); const heads = new Set<IHeadRow>();
photos.forEach((photo: IPhoto) => { photos.forEach((photo: IPhoto) => {
photo.flag &= ~this.c.FLAG_SELECTED; photo.flag &= ~this.c.FLAG_SELECTED;
heads.add(this.heads[photo.dayid]); heads.add(this.heads.get(photo.dayid)!);
this.selection.deleteBy(photo); this.selection.deleteBy(photo);
this.selectionChanged(); this.selectionChanged();
}); });

View File

@ -151,7 +151,7 @@ export default defineComponent({
/** Computed number of columns */ /** Computed number of columns */
numCols: 0, numCols: 0,
/** Header rows for dayId key */ /** Header rows for dayId key */
heads: {} as { [dayid: number]: IHeadRow }, heads: new Map<number, IHeadRow>(),
/** Current list (days response) was loaded from cache */ /** Current list (days response) was loaded from cache */
daysIsCache: false, daysIsCache: false,
@ -273,7 +273,7 @@ export default defineComponent({
if (isNaN(dayid) || !key) return; if (isNaN(dayid) || !key) return;
// Get day // Get day
const day = this.heads[dayid]?.day; const day = this.heads.get(dayid)?.day;
if (day && !day.detail) { if (day && !day.detail) {
const state = this.state; const state = this.state;
await this.fetchDay(dayid, true); await this.fetchDay(dayid, true);
@ -334,7 +334,7 @@ export default defineComponent({
this.loading = 0; this.loading = 0;
this.list = []; this.list = [];
this.dtmContent = false; this.dtmContent = false;
this.heads = {}; this.heads = new Map();
this.currentStart = 0; this.currentStart = 0;
this.currentEnd = 0; this.currentEnd = 0;
this.state = Math.random(); this.state = Math.random();
@ -739,7 +739,7 @@ export default defineComponent({
if (!data || !this.state) return; if (!data || !this.state) return;
const list: typeof this.list = []; const list: typeof this.list = [];
const heads: typeof this.heads = {}; const heads: typeof this.heads = new Map();
// Store the preloads in a separate map. // Store the preloads in a separate map.
// This is required since otherwise the inner detail objects // This is required since otherwise the inner detail objects
@ -789,14 +789,14 @@ export default defineComponent({
} }
// Add header to list // Add header to list
heads[day.dayid] = head; heads.set(day.dayid, head);
list.push(head); list.push(head);
// Dummy rows for placeholders // Dummy rows for placeholders
let nrows = Math.ceil(day.count / this.numCols); let nrows = Math.ceil(day.count / this.numCols);
// Check if already loaded - we can learn // Check if already loaded - we can learn
const prevRows = this.heads[day.dayid]?.day?.rows; const prevRows = this.heads.get(day.dayid)?.day?.rows;
nrows = prevRows?.length || nrows; nrows = prevRows?.length || nrows;
// Add rows // Add rows
@ -856,7 +856,7 @@ export default defineComponent({
// If any day in the fetch list has local images we need to fetch // If any day in the fetch list has local images we need to fetch
// the remote hidden images for the merging to happen correctly // the remote hidden images for the merging to happen correctly
if (this.routeHasNative) { if (this.routeHasNative) {
if (dayIds.some((id) => this.heads[id]?.day?.haslocal)) { if (dayIds.some((id) => this.heads.get(id)?.day?.haslocal)) {
query[DaysFilterType.HIDDEN] = '1'; query[DaysFilterType.HIDDEN] = '1';
} }
} }
@ -869,7 +869,7 @@ export default defineComponent({
if (!now && this.loadedDays.has(dayId)) return; if (!now && this.loadedDays.has(dayId)) return;
// Get head to ensure the day exists / is valid // Get head to ensure the day exists / is valid
const head = this.heads[dayId]; const head = this.heads.get(dayId);
if (!head) return; if (!head) return;
// Do this in advance to prevent duplicate requests // Do this in advance to prevent duplicate requests
@ -910,7 +910,7 @@ export default defineComponent({
// If the queue has gotten large enough, just expire immediately // If the queue has gotten large enough, just expire immediately
// This is to prevent a large number of requests from being queued // This is to prevent a large number of requests from being queued
now ||= this.fetchDayQueue.length >= 16; now ||= this.fetchDayQueue.length >= 16;
now ||= this.fetchDayQueue.reduce((sum, dayId) => sum + this.heads[dayId]?.day?.count ?? 0, 0) > 256; now ||= this.fetchDayQueue.reduce((sum, dayId) => sum + (this.heads.get(dayId)?.day?.count ?? 0), 0) > 256;
// Process immediately // Process immediately
if (now) return await this.fetchDayExpire(); if (now) return await this.fetchDayExpire();
@ -972,7 +972,7 @@ export default defineComponent({
nativex.processFreshServerDay(dayId, photos); nativex.processFreshServerDay(dayId, photos);
// Only process days that have local images further // Only process days that have local images further
return this.heads[dayId]?.day?.haslocal; return this.heads.get(dayId)?.day?.haslocal;
}) })
.map(async ([dayId, photos]) => { .map(async ([dayId, photos]) => {
nativex.mergeDay(photos, await nativex.getLocalDay(dayId)); nativex.mergeDay(photos, await nativex.getLocalDay(dayId));
@ -986,7 +986,7 @@ export default defineComponent({
utils.removeHiddenPhotos(photos); utils.removeHiddenPhotos(photos);
// Check if the response has any delta // Check if the response has any delta
const head = this.heads[dayId]; const head = this.heads.get(dayId);
if (head?.day?.detail?.length === photos.length) { if (head?.day?.detail?.length === photos.length) {
// Goes over the day and checks each photo including // Goes over the day and checks each photo including
// the order with the current list. If anything changes, // the order with the current list. If anything changes,
@ -1032,7 +1032,7 @@ export default defineComponent({
processDay(dayId: number, data: IPhoto[]) { processDay(dayId: number, data: IPhoto[]) {
if (!data || !this.state) return; if (!data || !this.state) return;
const head = this.heads[dayId]; const head = this.heads.get(dayId);
if (!head) return; if (!head) return;
const day = head.day; const day = head.day;
@ -1208,7 +1208,7 @@ export default defineComponent({
removedRows.push(...this.list.splice(headIdx, 1)); removedRows.push(...this.list.splice(headIdx, 1));
rowIdx = headIdx - 1; rowIdx = headIdx - 1;
headRemoved = true; headRemoved = true;
delete this.heads[dayId]; this.heads.delete(dayId);
} }
// Get rid of any extra rows // Get rid of any extra rows