nx: only fetch day for haslocal

Signed-off-by: Varun Patil <radialapps@gmail.com>
pull/807/merge
Varun Patil 2023-10-02 16:27:53 -07:00
parent a87bc2e14b
commit 58760b1c53
3 changed files with 69 additions and 42 deletions

View File

@ -691,7 +691,7 @@ export default defineComponent({
try { try {
if ((cache = await utils.getCachedData(cacheUrl))) { if ((cache = await utils.getCachedData(cacheUrl))) {
if (this.routeHasNative) { if (this.routeHasNative) {
await nativex.extendDaysWithLocal(cache); nativex.mergeDays(cache, await nativex.getLocalDays());
} }
await this.processDays(cache); await this.processDays(cache);
@ -714,7 +714,7 @@ export default defineComponent({
// Extend with native days // Extend with native days
if (this.routeHasNative) { if (this.routeHasNative) {
await nativex.extendDaysWithLocal(data); nativex.mergeDays(data, await nativex.getLocalDays());
} }
// Make sure we're still on the same page // Make sure we're still on the same page
@ -863,8 +863,8 @@ export default defineComponent({
const cache = await utils.getCachedData<IPhoto[]>(cacheUrl); const cache = await utils.getCachedData<IPhoto[]>(cacheUrl);
if (cache) { if (cache) {
// Cache only contains remote images; update from local too // Cache only contains remote images; update from local too
if (this.routeHasNative) { if (this.routeHasNative && head.day?.haslocal) {
await nativex.extendDayWithLocal(dayId, cache); nativex.mergeDay(cache, await nativex.getLocalDay(dayId));
} }
// Process the cache // Process the cache
@ -936,8 +936,10 @@ export default defineComponent({
// Get them all together for each day here. // Get them all together for each day here.
if (this.routeHasNative) { if (this.routeHasNative) {
await Promise.all( await Promise.all(
Array.from(dayMap.entries()).map(([dayId, photos]) => { Array.from(dayMap.entries()).map(async ([dayId, photos]) => {
return nativex.extendDayWithLocal(dayId, photos); if (this.heads[dayId]?.day?.haslocal) {
nativex.mergeDay(photos, await nativex.getLocalDay(dayId));
}
}) })
); );
} }

View File

@ -4,59 +4,82 @@ import { has } from './basic';
import type { IDay, IPhoto } from '../types'; import type { IDay, IPhoto } from '../types';
/** /**
* Extend a list of days with local days. * Merge incoming days into current days.
* Fetches the local days from the native interface. * @param current Response to update
* @param incoming Incoming response
* @return touched or added days
*/ */
export async function extendDaysWithLocal(days: IDay[]) { export function mergeDays(current: IDay[], incoming: IDay[]) {
if (!has()) return; const currentMap = new Map(current.map((d) => [d.dayid, d]));
const touched: IDay[] = [];
// Query native part for (const day of incoming) {
const res = await fetch(NAPI.DAYS()); const curr = currentMap.get(day.dayid);
if (!res.ok) return; if (curr) {
const local: IDay[] = await res.json(); curr.count = Math.max(curr.count, day.count);
const remoteMap = new Map(days.map((d) => [d.dayid, d]));
// Merge local days into remote days // Copy over some flags
for (const day of local) { curr.haslocal ||= day.haslocal;
const remote = remoteMap.get(day.dayid);
if (remote) {
remote.count = Math.max(remote.count, day.count);
} else { } else {
days.push(day); current.push(day);
} }
} }
// TODO: sort depends on view // TODO: sort depends on view
// (but we show it for only timeline anyway for now) // (but we use this for only timeline anyway for now)
days.sort((a, b) => b.dayid - a.dayid); current.sort((a, b) => b.dayid - a.dayid);
} }
/** /**
* Extend a list of photos with local photos. * Merge incoming photos into current photos.
* Fetches the local photos from the native interface and filters out duplicates. * @param current Response to update
* * @param incoming Incoming response
* @param dayId Day ID to append local photos to * @returns added photos
* @param photos List of photos to append to (duplicates will not be added)
* @returns
*/ */
export async function extendDayWithLocal(dayId: number, photos: IPhoto[]) { export function mergeDay(current: IPhoto[], incoming: IPhoto[]): IPhoto[] {
if (!has()) return;
// Query native part
const res = await fetch(NAPI.DAY(dayId));
if (!res.ok) return;
// Merge local photos into remote photos // Merge local photos into remote photos
const localPhotos: IPhoto[] = await res.json(); const currentAUIDs = new Set(current.map((p) => p.auid));
const serverAUIDs = new Set(photos.map((p) => p.auid));
// Filter out files that are only available locally // Filter out files that are only available locally
const localOnly = localPhotos.filter((p) => !serverAUIDs.has(p.auid)); const added = incoming.filter((p) => !currentAUIDs.has(p.auid));
localOnly.forEach((p) => (p.islocal = true)); current.push(...added);
photos.push(...localOnly);
// Sort by epoch value // Sort by epoch value
photos.sort((a, b) => (b.epoch ?? 0) - (a.epoch ?? 0)); current.sort((a, b) => (b.epoch ?? 0) - (a.epoch ?? 0));
return added;
}
/**
* Get the local days response
*/
export async function getLocalDays(): Promise<IDay[]> {
if (!has()) return [];
const res = await fetch(NAPI.DAYS());
if (!res.ok) return [];
const days: IDay[] = await res.json();
days.forEach((d) => (d.haslocal = true));
return days;
}
/**
* Fetches the local photos from the native interface
* @param dayId Day ID to get local photos for
* @returns
*/
export async function getLocalDay(dayId: number): Promise<IPhoto[]> {
if (!has()) return [];
const res = await fetch(NAPI.DAY(dayId));
if (!res.ok) return [];
const photos: IPhoto[] = await res.json();
photos.forEach((p) => (p.islocal = true));
return photos;
} }
/** /**

View File

@ -20,6 +20,8 @@ export type IDay = {
rows?: IRow[]; rows?: IRow[];
/** List of photos for this day */ /** List of photos for this day */
detail?: IPhoto[]; detail?: IPhoto[];
/** This day has some local photos */
haslocal?: boolean;
}; };
export type IPhoto = { export type IPhoto = {