From d7fe9091ce6e00cb9134f2be7d2c244c97204afe Mon Sep 17 00:00:00 2001 From: Zias van Nes Date: Wed, 29 Oct 2025 00:14:26 +0100 Subject: [PATCH] fix:new child subscription this fix makes sure that when the client initializes a new child that needs to subscribe for updates, it first checks whether this item already has a currrent state and uses this, so that a new child always starts in sync with the global state. --- src/lib/components/LikeButton.svelte | 41 ++++++++++++++---------- src/lib/stores/api-sync.ts | 48 ++++++++++++++++++++++++++-- 2 files changed, 70 insertions(+), 19 deletions(-) diff --git a/src/lib/components/LikeButton.svelte b/src/lib/components/LikeButton.svelte index 0981220..21e3ecb 100644 --- a/src/lib/components/LikeButton.svelte +++ b/src/lib/components/LikeButton.svelte @@ -21,7 +21,7 @@ class: className = '' }: Props = $props(); - // Local state stores for this like button + // Local state stores for this like button - start with props but will be overridden by global state const likeState = writable({ isLiked: isLiked, likeCount: likeCount, @@ -38,21 +38,30 @@ const module = await import('$lib/stores/api-sync'); apiSync = module.apiSync; - // Initialize the find's like state with props data - apiSync.setEntityState('find', findId, { - id: findId, - isLikedByUser: isLiked, - likeCount: likeCount, - // Minimal data needed for like functionality - title: '', - latitude: '', - longitude: '', - isPublic: true, - createdAt: new Date(), - userId: '', - username: '', - isFromFriend: false - }); + // Check if global state already exists for this find + const existingState = apiSync.getEntityState('find', findId); + + if (existingState) { + // Use existing global state - it's more current than props + console.log(`Using existing global state for find ${findId}`, existingState); + } else { + // Initialize with minimal data only if no global state exists + console.log(`Initializing new minimal state for find ${findId}`); + apiSync.setEntityState('find', findId, { + id: findId, + isLikedByUser: isLiked, + likeCount: likeCount, + // Minimal data needed for like functionality + title: '', + latitude: '', + longitude: '', + isPublic: true, + createdAt: new Date(), + userId: '', + username: '', + isFromFriend: false + }); + } // Subscribe to global state for this find const globalLikeState = apiSync.subscribeFindLikes(findId); diff --git a/src/lib/stores/api-sync.ts b/src/lib/stores/api-sync.ts index 7436af2..f84d65d 100644 --- a/src/lib/stores/api-sync.ts +++ b/src/lib/stores/api-sync.ts @@ -158,7 +158,40 @@ class APISync { } /** - * Initialize entity state with server data + * Check if entity state exists + */ + hasEntityState(entityType: string, entityId: string): boolean { + const store = this.getEntityStore(entityType); + let exists = false; + + const unsubscribe = store.subscribe(($entities) => { + exists = $entities.has(entityId); + }); + unsubscribe(); + + return exists; + } + + /** + * Get current entity state + */ + getEntityState(entityType: string, entityId: string): T | null { + const store = this.getEntityStore(entityType); + let currentState: T | null = null; + + const unsubscribe = store.subscribe(($entities) => { + const entity = $entities.get(entityId); + if (entity?.data) { + currentState = entity.data as T; + } + }); + unsubscribe(); + + return currentState; + } + + /** + * Initialize entity state with server data (only if no existing state) */ setEntityState(entityType: string, entityId: string, data: T, isLoading = false): void { const store = this.getEntityStore(entityType); @@ -175,6 +208,15 @@ class APISync { }); } + /** + * Initialize entity state only if it doesn't exist yet + */ + initializeEntityState(entityType: string, entityId: string, data: T, isLoading = false): void { + if (!this.hasEntityState(entityType, entityId)) { + this.setEntityState(entityType, entityId, data, isLoading); + } + } + /** * Update entity loading state */ @@ -426,11 +468,11 @@ class APISync { } /** - * Initialize find data from server + * Initialize find data from server (only if no existing state) */ initializeFindData(finds: FindState[]): void { for (const find of finds) { - this.setEntityState('find', find.id, find); + this.initializeEntityState('find', find.id, find); } }