chore: cleanup

This commit is contained in:
Min Idzelis
2025-06-15 02:47:18 +00:00
parent 9cc2189ef7
commit c76e8da173
2 changed files with 75 additions and 70 deletions
+17 -20
View File
@@ -26,17 +26,14 @@ class ActivityManager {
constructor() { constructor() {
this.#subscribe = createSubscriber((update) => { this.#subscribe = createSubscriber((update) => {
debugger;
console.log('update', update);
const unsubscribe = websocketEvents.on('on_activity_change', ({ albumId, assetId }) => { const unsubscribe = websocketEvents.on('on_activity_change', ({ albumId, assetId }) => {
if (this.#albumId === albumId || this.#assetId === assetId) { if (this.#albumId === albumId || this.#assetId === assetId) {
handlePromiseError(this.refreshActivities(this.#albumId!, this.#assetId)); handlePromiseError(this.refreshActivities(this.#albumId!, this.#assetId));
update();
} }
}); });
// stop listening when all the effects are destroyed
return () => { return () => {
debugger;
unsubscribe(); unsubscribe();
}; };
}); });
@@ -68,7 +65,7 @@ class ActivityManager {
} }
async addActivity(dto: ActivityCreateDto) { async addActivity(dto: ActivityCreateDto) {
if (this.#albumId === undefined) { if (!this.#albumId) {
return; return;
} }
@@ -77,9 +74,7 @@ class ActivityManager {
if (activity.type === ReactionType.Comment) { if (activity.type === ReactionType.Comment) {
this.#commentCount++; this.#commentCount++;
} } else if (activity.type === ReactionType.Like) {
if (activity.type === ReactionType.Like) {
this.#likeCount++; this.#likeCount++;
} }
@@ -94,15 +89,15 @@ class ActivityManager {
if (activity.type === ReactionType.Comment) { if (activity.type === ReactionType.Comment) {
this.#commentCount--; this.#commentCount--;
} } else if (activity.type === ReactionType.Like) {
if (activity.type === ReactionType.Like) {
this.#likeCount--; this.#likeCount--;
} }
this.#activities = index if (index !== undefined) {
? this.#activities.splice(index, 1) this.#activities.splice(index, 1);
: this.#activities.filter(({ id }) => id !== activity.id); } else {
this.#activities = this.#activities.filter(({ id }) => id !== activity.id);
}
await deleteActivity({ id: activity.id }); await deleteActivity({ id: activity.id });
handlePromiseError(this.refreshActivities(this.#albumId, this.#assetId)); handlePromiseError(this.refreshActivities(this.#albumId, this.#assetId));
@@ -116,12 +111,17 @@ class ActivityManager {
if (this.#isLiked) { if (this.#isLiked) {
await this.deleteActivity(this.#isLiked); await this.deleteActivity(this.#isLiked);
this.#isLiked = null; this.#isLiked = null;
} else { return;
this.#isLiked = (await this.addActivity({ }
const newLike = await this.addActivity({
albumId: this.#albumId, albumId: this.#albumId,
assetId: this.#assetId, assetId: this.#assetId,
type: ReactionType.Like, type: ReactionType.Like,
}))!; });
if (newLike) {
this.#isLiked = newLike;
} }
} }
@@ -152,6 +152,3 @@ class ActivityManager {
} }
export const activityManager = new ActivityManager(); export const activityManager = new ActivityManager();
function on(arg0: any, arg1: string, update: () => void) {
throw new Error('Function not implemented.');
}
@@ -18,32 +18,38 @@ export type AssetFilter = (
// Filter functions // Filter functions
const checkVisibilityProperty: AssetFilter = (asset, timelineManager) => { const checkVisibilityProperty: AssetFilter = (asset, timelineManager) => {
if (timelineManager.options.visibility === undefined) {
return true;
}
const timelineAsset = toTimelineAsset(asset); const timelineAsset = toTimelineAsset(asset);
return ( return timelineManager.options.visibility === timelineAsset.visibility;
timelineManager.options.visibility === undefined || timelineManager.options.visibility === timelineAsset.visibility
);
}; };
const checkFavoriteProperty: AssetFilter = (asset, timelineManager) => { const checkFavoriteProperty: AssetFilter = (asset, timelineManager) => {
if (timelineManager.options.isFavorite === undefined) {
return true;
}
const timelineAsset = toTimelineAsset(asset); const timelineAsset = toTimelineAsset(asset);
return ( return timelineManager.options.isFavorite === timelineAsset.isFavorite;
timelineManager.options.isFavorite === undefined || timelineManager.options.isFavorite === timelineAsset.isFavorite
);
}; };
const checkTrashedProperty: AssetFilter = (asset, timelineManager) => { const checkTrashedProperty: AssetFilter = (asset, timelineManager) => {
if (timelineManager.options.isTrashed === undefined) {
return true;
}
const timelineAsset = toTimelineAsset(asset); const timelineAsset = toTimelineAsset(asset);
return ( return timelineManager.options.isTrashed === timelineAsset.isTrashed;
timelineManager.options.isTrashed === undefined || timelineManager.options.isTrashed === timelineAsset.isTrashed
);
}; };
const checkTagProperty: AssetFilter = (asset, timelineManager) => { const checkTagProperty: AssetFilter = (asset, timelineManager) => {
if (!timelineManager.options.tagId) { if (!timelineManager.options.tagId) {
return true; return true;
} }
const hasMatchingTag = asset.tags?.some((tag: { id: string }) => tag.id === timelineManager.options.tagId);
return !!hasMatchingTag; return asset.tags?.some((tag: { id: string }) => tag.id === timelineManager.options.tagId) ?? false;
}; };
const checkAlbumProperty: AssetFilter = async (asset, timelineManager) => { const checkAlbumProperty: AssetFilter = async (asset, timelineManager) => {
@@ -58,10 +64,8 @@ const checkPersonProperty: AssetFilter = (asset, timelineManager) => {
if (!timelineManager.options.personId) { if (!timelineManager.options.personId) {
return true; return true;
} }
const hasMatchingPerson = asset.people?.some(
(person: { id: string }) => person.id === timelineManager.options.personId, return asset.people?.some((person: { id: string }) => person.id === timelineManager.options.personId) ?? false;
);
return !!hasMatchingPerson;
}; };
export class WebsocketSupport { export class WebsocketSupport {
@@ -115,7 +119,7 @@ export class WebsocketSupport {
this.#pendingUpdates.trashed.push(...ids); this.#pendingUpdates.trashed.push(...ids);
this.#scheduleProcessing(); this.#scheduleProcessing();
}), }),
// this event is called when an person is added or removed from an asset // this event is called when a person is added or removed from an asset
websocketEvents.on('on_asset_person', (data) => { websocketEvents.on('on_asset_person', (data) => {
this.#pendingUpdates.personed.push(data); this.#pendingUpdates.personed.push(data);
this.#scheduleProcessing(); this.#scheduleProcessing();
@@ -125,15 +129,11 @@ export class WebsocketSupport {
this.#pendingUpdates.updated.push(...ids); this.#pendingUpdates.updated.push(...ids);
this.#scheduleProcessing(); this.#scheduleProcessing();
}), }),
// this event is called when an asseted is added or removed from an album // this event is called when an asset is added or removed from an album
websocketEvents.on('on_album_update', (data) => { websocketEvents.on('on_album_update', (data) => {
this.#pendingUpdates.album.push(data); this.#pendingUpdates.album.push(data);
this.#scheduleProcessing(); this.#scheduleProcessing();
}), }),
websocketEvents.on('on_asset_trash', (ids) => {
this.#pendingUpdates.trashed.push(...ids);
this.#scheduleProcessing();
}),
websocketEvents.on('on_asset_delete', (ids) => { websocketEvents.on('on_asset_delete', (ids) => {
this.#pendingUpdates.deleted.push(ids); this.#pendingUpdates.deleted.push(ids);
this.#scheduleProcessing(); this.#scheduleProcessing();
@@ -171,11 +171,11 @@ export class WebsocketSupport {
this.#processTimeoutId = setTimeout(() => { this.#processTimeoutId = setTimeout(() => {
this.#processTimeoutId = undefined; this.#processTimeoutId = undefined;
void this.#processPendingChanges(); void this.#applyPendingChanges();
}, PROCESS_DELAY_MS); }, PROCESS_DELAY_MS);
} }
async #processPendingChanges() { async #applyPendingChanges() {
if (this.#isProcessing || this.#pendingCount() === 0) { if (this.#isProcessing || this.#pendingCount() === 0) {
return; return;
} }
@@ -183,7 +183,7 @@ export class WebsocketSupport {
this.#isProcessing = true; this.#isProcessing = true;
try { try {
await this.#process(); await this.#processAllPendingUpdates();
} finally { } finally {
this.#isProcessing = false; this.#isProcessing = false;
@@ -193,22 +193,22 @@ export class WebsocketSupport {
} }
} }
async #process() { async #processAllPendingUpdates() {
const pendingUpdates = this.#pendingUpdates; const pendingUpdates = this.#pendingUpdates;
this.#pendingUpdates = this.#init(); this.#pendingUpdates = this.#init();
await this.#handleGeneric( await this.#filterAndUpdateAssets(
[...pendingUpdates.updated, ...pendingUpdates.trashed, ...pendingUpdates.restored], [...pendingUpdates.updated, ...pendingUpdates.trashed, ...pendingUpdates.restored],
[checkVisibilityProperty, checkFavoriteProperty, checkTrashedProperty, checkTagProperty, checkAlbumProperty], [checkVisibilityProperty, checkFavoriteProperty, checkTrashedProperty, checkTagProperty, checkAlbumProperty],
); );
await this.#handleUpdatedAssetsPerson(pendingUpdates.personed); await this.#handlePersonUpdates(pendingUpdates.personed);
await this.#handleUpdatedAssetsAlbum(pendingUpdates.album); await this.#handleAlbumUpdates(pendingUpdates.album);
this.#timelineManager.removeAssets(pendingUpdates.deleted); this.#timelineManager.removeAssets(pendingUpdates.deleted);
} }
async #handleGeneric(assetIds: string[], filters: AssetFilter[]) { async #filterAndUpdateAssets(assetIds: string[], filters: AssetFilter[]) {
if (assetIds.length === 0) { if (assetIds.length === 0) {
return; return;
} }
@@ -239,32 +239,34 @@ export class WebsocketSupport {
return true; return true;
} }
async #handleUpdatedAssetsPerson( async #handlePersonUpdates(
data: { assetId: string; personId: string | undefined; status: 'created' | 'removed' | 'removed_soft' }[], data: { assetId: string; personId: string | undefined; status: 'created' | 'removed' | 'removed_soft' }[],
) { ) {
if (data.length === 0) {
return;
}
const assetsToRemove: string[] = []; const assetsToRemove: string[] = [];
const personAssetsToAdd: string[] = []; const personAssetsToAdd: string[] = [];
const targetPersonId = this.#timelineManager.options.personId;
if (this.#timelineManager.options.personId === undefined) { if (targetPersonId === undefined) {
// If no person filter, we just add all assets with a person change // If no person filter, add all assets with person changes
personAssetsToAdd.push(...data.map((d) => d.assetId)); personAssetsToAdd.push(...data.map((d) => d.assetId));
} else { } else {
for (const { assetId, personId, status } of data) { for (const { assetId, personId, status } of data) {
if (status === 'created' && personId === this.#timelineManager.options.personId) { if (status === 'created' && personId === targetPersonId) {
personAssetsToAdd.push(assetId); personAssetsToAdd.push(assetId);
} else if ( } else if ((status === 'removed' || status === 'removed_soft') && personId === targetPersonId) {
(status === 'removed' || status === 'removed_soft') &&
personId === this.#timelineManager.options.personId
) {
assetsToRemove.push(assetId); assetsToRemove.push(assetId);
} }
} }
} }
this.#timelineManager.removeAssets(assetsToRemove); this.#timelineManager.removeAssets(assetsToRemove);
// At this point, personAssetsToAdd contains assets that now have the target person,
// but we need to check if they still match other filters. // Filter and add assets that now have the target person
await this.#handleGeneric(personAssetsToAdd, [ await this.#filterAndUpdateAssets(personAssetsToAdd, [
checkVisibilityProperty, checkVisibilityProperty,
checkFavoriteProperty, checkFavoriteProperty,
checkTrashedProperty, checkTrashedProperty,
@@ -273,18 +275,24 @@ export class WebsocketSupport {
]); ]);
} }
async #handleUpdatedAssetsAlbum(data: { albumId: string; assetId: string[]; status: 'added' | 'removed' }[]) { async #handleAlbumUpdates(data: { albumId: string; assetId: string[]; status: 'added' | 'removed' }[]) {
if (data.length === 0) {
return;
}
const assetsToAdd: string[] = []; const assetsToAdd: string[] = [];
const assetsToRemove: string[] = []; const assetsToRemove: string[] = [];
const targetAlbumId = this.#timelineManager.options.albumId;
if (this.#timelineManager.options.albumId === undefined) { if (targetAlbumId === undefined) {
// If no album filter, we just add all assets with an album change // If no album filter, add all assets with album changes
assetsToAdd.push(...data.flatMap((d) => d.assetId)); assetsToAdd.push(...data.flatMap((d) => d.assetId));
} else { } else {
for (const { albumId, assetId, status } of data) { for (const { albumId, assetId, status } of data) {
if (albumId !== this.#timelineManager.options.albumId) { if (albumId !== targetAlbumId) {
continue; continue;
} }
if (status === 'added') { if (status === 'added') {
assetsToAdd.push(...assetId); assetsToAdd.push(...assetId);
} else if (status === 'removed') { } else if (status === 'removed') {
@@ -294,9 +302,9 @@ export class WebsocketSupport {
} }
this.#timelineManager.removeAssets(assetsToRemove); this.#timelineManager.removeAssets(assetsToRemove);
// At this point, assetsToAdd contains assets that now have the target person,
// but we need to check if they still match other filters. // Filter and add assets that are now in the target album
await this.#handleGeneric(assetsToAdd, [ await this.#filterAndUpdateAssets(assetsToAdd, [
checkVisibilityProperty, checkVisibilityProperty,
checkFavoriteProperty, checkFavoriteProperty,
checkTrashedProperty, checkTrashedProperty,