Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix/web/render timeline after adding new assets #12353

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/docs/guides/template-backup-script.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,4 @@ borg mount "$REMOTE_HOST:$REMOTE_BACKUP_PATH"/immich-borg /tmp/immich-mountpoint
cd /tmp/immich-mountpoint
```

You can find available snapshots in seperate sub-directories at `/tmp/immich-mountpoint`. Restore the files you need, and unmount the Borg repository using `borg umount /tmp/immich-mountpoint`
You can find available snapshots in separate sub-directories at `/tmp/immich-mountpoint`. Restore the files you need, and unmount the Borg repository using `borg umount /tmp/immich-mountpoint`
30 changes: 9 additions & 21 deletions web/src/lib/actions/intersection-observer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ type TrackedProperties = {
left?: string;
};
type OnIntersectCallback = (entryOrElement: IntersectionObserverEntry | HTMLElement) => unknown;
type OnSeperateCallback = (element: HTMLElement) => unknown;
type OnSeparateCallback = (element: HTMLElement) => unknown;
type IntersectionObserverActionProperties = {
key?: string;
onSeparate?: OnSeperateCallback;
onSeparate?: OnSeparateCallback;
onIntersect?: OnIntersectCallback;

root?: Element | Document | null;
Expand All @@ -22,8 +22,6 @@ type IntersectionObserverActionProperties = {
right?: string;
bottom?: string;
left?: string;

disabled?: boolean;
};
type TaskKey = HTMLElement | string;

Expand Down Expand Up @@ -92,11 +90,7 @@ function _intersectionObserver(
element: HTMLElement,
properties: IntersectionObserverActionProperties,
) {
if (properties.disabled) {
properties.onIntersect?.(element);
} else {
configure(key, element, properties);
}
configure(key, element, properties);
return {
update(properties: IntersectionObserverActionProperties) {
const config = elementToConfig.get(key);
Expand All @@ -106,20 +100,14 @@ function _intersectionObserver(
if (isEquivalent(config, properties)) {
return;
}

configure(key, element, properties);
},
destroy: () => {
if (properties.disabled) {
properties.onSeparate?.(element);
} else {
const config = elementToConfig.get(key);
const { observer, onSeparate } = config || {};
observer?.unobserve(element);
elementToConfig.delete(key);
if (onSeparate) {
onSeparate?.(element);
}
}
const config = elementToConfig.get(key);
const { observer } = config || {};
observer?.unobserve(element);
elementToConfig.delete(key);
},
};
}
Expand Down Expand Up @@ -148,5 +136,5 @@ export function intersectionObserver(
},
};
}
return _intersectionObserver(element, element, properties);
return _intersectionObserver(properties.key || element, element, properties);
}
3 changes: 1 addition & 2 deletions web/src/lib/components/photos-page/asset-date-group.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
$: dateGroups = bucket.dateGroups;

const {
DATEGROUP: { INTERSECTION_DISABLED, INTERSECTION_ROOT_TOP, INTERSECTION_ROOT_BOTTOM },
DATEGROUP: { INTERSECTION_ROOT_TOP, INTERSECTION_ROOT_BOTTOM },
} = TUNABLES;
/* TODO figure out a way to calculate this*/
const TITLE_HEIGHT = 51;
Expand Down Expand Up @@ -116,7 +116,6 @@
top: INTERSECTION_ROOT_TOP,
bottom: INTERSECTION_ROOT_BOTTOM,
root: assetGridElement,
disabled: INTERSECTION_DISABLED,
}}
data-display={display}
data-date-group={dateGroup.date}
Expand Down
3 changes: 2 additions & 1 deletion web/src/lib/components/photos-page/asset-grid.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -804,12 +804,13 @@
class:invisible={showSkeleton}
style:height={$assetStore.timelineHeight + 'px'}
>
{#each $assetStore.buckets as bucket (bucket.bucketDate)}
{#each $assetStore.buckets as bucket (bucket.viewId)}
{@const isPremeasure = preMeasure.includes(bucket)}
{@const display = bucket.intersecting || bucket === $assetStore.pendingScrollBucket || isPremeasure}
<div
id="bucket"
use:intersectionObserver={{
key: bucket.viewId,
onIntersect: () => handleIntersect(bucket),
onSeparate: () => handleSeparate(bucket),
top: BUCKET_INTERSECTION_ROOT_TOP,
Expand Down
64 changes: 51 additions & 13 deletions web/src/lib/stores/assets.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { locale } from '$lib/stores/preferences.store';
import { getKey } from '$lib/utils';
import { AssetGridTaskManager } from '$lib/utils/asset-store-task-manager';
import { getAssetRatio } from '$lib/utils/asset-utils';
import { generateId } from '$lib/utils/generate-id';
import type { AssetGridRouteSearchParams } from '$lib/utils/navigation';
import { calculateWidth, fromLocalDateTime, splitBucketIntoDateGroups, type DateGroup } from '$lib/utils/timeline-util';
import { TimeBucketSize, getAssetInfo, getTimeBucket, getTimeBuckets, type AssetResponseDto } from '@immich/sdk';
Expand All @@ -12,7 +13,6 @@ import { t } from 'svelte-i18n';
import { get, writable, type Unsubscriber } from 'svelte/store';
import { handleError } from '../utils/handle-error';
import { websocketEvents } from './websocket';

type AssetApiGetTimeBucketsRequest = Parameters<typeof getTimeBuckets>[0];
export type AssetStoreOptions = Omit<AssetApiGetTimeBucketsRequest, 'size'>;

Expand Down Expand Up @@ -70,7 +70,10 @@ export class AssetBucket {
Object.assign(this, props);
this.init();
}

/** The svelte key for this view model object */
get viewId() {
return this.store.viewId + '-' + this.bucketDate;
}
private init() {
// create a promise, and store its resolve/reject callbacks. The loadedSignal callback
// will be incoked when a bucket is loaded, fulfilling the promise. The canceledSignal
Expand Down Expand Up @@ -205,21 +208,23 @@ export class AssetStore {
private assetToBucket: Record<string, AssetLookup> = {};
private pendingChanges: PendingChange[] = [];
private unsubscribers: Unsubscriber[] = [];
private options: AssetApiGetTimeBucketsRequest;
private options!: AssetApiGetTimeBucketsRequest;
private viewport: Viewport = {
height: 0,
width: 0,
};
private initializedSignal!: () => void;
private store$ = writable(this);
/** The svelte key for this view model object */
viewId = generateId();

lastScrollTime: number = 0;
subscribe = this.store$.subscribe;
/**
* A promise that resolves once the store is initialized.
*/
taskManager = new AssetGridTaskManager(this);
complete!: Promise<void>;
taskManager = new AssetGridTaskManager(this);
initialized = false;
timelineHeight = 0;
buckets: AssetBucket[] = [];
Expand All @@ -234,13 +239,23 @@ export class AssetStore {
options: AssetStoreOptions,
private albumId?: string,
) {
this.setOptions(options);
this.createInitializationSignal();
this.store$.set(this);
}

private setOptions(options: AssetStoreOptions) {
this.options = { ...options, size: TimeBucketSize.Month };
}

private createInitializationSignal() {
// create a promise, and store its resolve callbacks. The initializedSignal callback
// will be invoked when a the assetstore is initialized.
this.complete = new Promise((resolve) => {
this.initializedSignal = resolve;
});
this.store$.set(this);
// uncaught rejection go away
this.complete.catch(() => void 0);
}

private addPendingChanges(...changes: PendingChange[]) {
Expand Down Expand Up @@ -273,6 +288,7 @@ export class AssetStore {
for (const unsubscribe of this.unsubscribers) {
unsubscribe();
}
this.unsubscribers = [];
}

private getPendingChangeBatches() {
Expand Down Expand Up @@ -360,8 +376,10 @@ export class AssetStore {
if (bucketListener) {
this.addListener(bucketListener);
}
// uncaught rejection go away
this.complete.catch(() => void 0);
await this.initialiazeTimeBuckets();
}

async initialiazeTimeBuckets() {
this.timelineHeight = 0;
this.buckets = [];
this.assets = [];
Expand All @@ -379,29 +397,49 @@ export class AssetStore {
this.initialized = true;
}

async updateOptions(options: AssetStoreOptions) {
if (!this.initialized) {
this.setOptions(options);
return;
}
// TODO: don't call updateObjects frequently after
// init - cancelation of the initialize tasks isn't
// performed right now, and will cause issues if
// multiple updateOptions() calls are interleved.
await this.complete;
this.taskManager.destroy();
this.taskManager = new AssetGridTaskManager(this);
this.initialized = false;
this.viewId = generateId();
this.createInitializationSignal();
this.setOptions(options);
await this.initialiazeTimeBuckets();
this.emit(true);
await this.initialLayout(true);
}

public destroy() {
this.taskManager.destroy();
this.listeners = [];
this.initialized = false;
}

async updateViewport(viewport: Viewport, force?: boolean) {
if (!this.initialized) {
return;
}
if (viewport.height === 0 && viewport.width === 0) {
return;
}

if (!force && this.viewport.height === viewport.height && this.viewport.width === viewport.width) {
return;
}

await this.complete;
// changing width invalidates the actual height, and needs to be remeasured, since width changes causes
// layout reflows.
const changedWidth = this.viewport.width != viewport.width;
this.viewport = { ...viewport };
await this.initialLayout(changedWidth);
}

private async initialLayout(changedWidth: boolean) {
for (const bucket of this.buckets) {
this.updateGeometry(bucket, changedWidth);
}
Expand All @@ -410,7 +448,7 @@ export class AssetStore {
const loaders = [];
let height = 0;
for (const bucket of this.buckets) {
if (height >= viewport.height) {
if (height >= this.viewport.height) {
break;
}
height += bucket.bucketHeight;
Expand Down
4 changes: 2 additions & 2 deletions web/src/lib/utils/asset-store-task-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ class IntersectionTask {
return { task: execTask, cleanup };
}

trackSeperatedTask(componentId: string, task: Task) {
trackSeparatedTask(componentId: string, task: Task) {
const execTask = () => {
if (this.intersected) {
return;
Expand Down Expand Up @@ -363,7 +363,7 @@ class IntersectionTask {
return;
}

const { task, cleanup } = this.trackSeperatedTask(componentId, separated);
const { task, cleanup } = this.trackSeparatedTask(componentId, separated);
this.internalTaskManager.queueSeparateTask({
task,
cleanup,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,7 @@
await refreshAlbum();

timelineInteractionStore.clearMultiselect();
viewMode = ViewMode.VIEW;
await navigate(
{ targetRoute: 'current', assetId: null, assetGridRouteSearchParams: $gridScrollTarget },
{ replaceState: true, forceNavigate: true },
);
await setModeToView();
} catch (error) {
handleError(error, $t('errors.error_adding_assets_to_album'));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,16 @@
return Object.fromEntries(tags.map((tag) => [tag.value, tag]));
};

const assetStore = new AssetStore({});

$: tags = data.tags;
$: tagsMap = buildMap(tags);
$: tag = currentPath ? tagsMap[currentPath] : null;
$: tagId = tag?.id;
$: tree = buildTree(tags.map((tag) => tag.value));
$: {
void assetStore.updateOptions({ tagId });
}

const handleNavigation = async (tag: string) => {
await navigateToView(normalizeTreePath(`${data.path || ''}/${tag}`));
Expand Down Expand Up @@ -169,20 +175,13 @@
<Breadcrumbs {pathSegments} icon={mdiTagMultiple} title={$t('tags')} {getLink} />

<section class="mt-2 h-full">
{#key $page.url.href}
{#if tag}
<AssetGrid
enableRouting={true}
assetStore={new AssetStore({ tagId: tag.id })}
{assetInteractionStore}
removeAction={AssetAction.UNARCHIVE}
>
<TreeItemThumbnails items={data.children} icon={mdiTag} onClick={handleNavigation} slot="empty" />
</AssetGrid>
{:else}
<TreeItemThumbnails items={Object.keys(tree)} icon={mdiTag} onClick={handleNavigation} />
{/if}
{/key}
{#if tag}
<AssetGrid enableRouting={true} {assetStore} {assetInteractionStore} removeAction={AssetAction.UNARCHIVE}>
<TreeItemThumbnails items={data.children} icon={mdiTag} onClick={handleNavigation} slot="empty" />
</AssetGrid>
{:else}
<TreeItemThumbnails items={Object.keys(tree)} icon={mdiTag} onClick={handleNavigation} />
{/if}
</section>
</UserPageLayout>

Expand Down
Loading