From 4e39d413622624ffc2d32ae2fed122a8e09299b8 Mon Sep 17 00:00:00 2001 From: LIlGG <1103069291@qq.com> Date: Mon, 13 Oct 2025 12:42:56 +0800 Subject: [PATCH] fix: ensure nanostores listeners trigger on artifact updates Refactored artifact update logic to create new Map instances when modifying artifacts, ensuring nanostores listeners are properly triggered. This prevents issues where listeners do not react to in-place mutations of existing Map objects. --- app/.client/stores/chat.ts | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/app/.client/stores/chat.ts b/app/.client/stores/chat.ts index 0c1888c..3570fea 100644 --- a/app/.client/stores/chat.ts +++ b/app/.client/stores/chat.ts @@ -120,15 +120,16 @@ export class ChatStore { }; const artifactsByMessageId = this.artifacts.get(); - let artifactsByPageName = artifactsByMessageId.get(messageId); - if (!artifactsByPageName) { - artifactsByPageName = new Map(); - artifactsByMessageId.set(messageId, artifactsByPageName); - } + const existingArtifactsByPageName = artifactsByMessageId.get(messageId); + const artifactsByPageName = existingArtifactsByPageName ? new Map(existingArtifactsByPageName) : new Map(); artifactsByPageName.set(name, newArtifact); - this.artifacts.set(artifactsByMessageId); + // create new outer Map instance to trigger nanostores listener + const newArtifactsByMessageId = new Map(artifactsByMessageId); + newArtifactsByMessageId.set(messageId, artifactsByPageName); + + this.artifacts.set(newArtifactsByMessageId); const bridge = await editorBridge; bridge.updatePageAttributes(name, { title }); } @@ -140,14 +141,19 @@ export class ChatStore { } const artifactsByMessageId = this.artifacts.get(); - const artifactsByPageName = artifactsByMessageId.get(messageId); - if (!artifactsByPageName) { + const existingArtifactsByPageName = artifactsByMessageId.get(messageId); + if (!existingArtifactsByPageName) { return; } - artifactsByPageName.set(name, { ...artifact, ...state }); - artifactsByMessageId.set(messageId, artifactsByPageName); - this.artifacts.set(artifactsByMessageId); + const artifactsByPageName = new Map(existingArtifactsByPageName); + artifactsByPageName.set(name, { ...artifact, ...state }); + + // create new outer Map instance to trigger nanostores listener + const newArtifactsByMessageId = new Map(artifactsByMessageId); + newArtifactsByMessageId.set(messageId, artifactsByPageName); + + this.artifacts.set(newArtifactsByMessageId); } private getArtifact(messageId: string, pageName: string) {