From 5d071373b93163662dbcf856c4e9f6d59ffb561b Mon Sep 17 00:00:00 2001 From: Simon Farshid Date: Sat, 14 Dec 2024 14:44:59 -0800 Subject: [PATCH] fix: initialize thread on import --- .../src/runtimes/core/BaseThreadRuntimeCore.tsx | 10 ++++++++++ .../ExternalStoreThreadRuntimeCore.tsx | 5 ++++- .../src/runtimes/local/LocalThreadRuntimeCore.tsx | 13 ++----------- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/packages/react/src/runtimes/core/BaseThreadRuntimeCore.tsx b/packages/react/src/runtimes/core/BaseThreadRuntimeCore.tsx index d6bccc42e4..995ba7a634 100644 --- a/packages/react/src/runtimes/core/BaseThreadRuntimeCore.tsx +++ b/packages/react/src/runtimes/core/BaseThreadRuntimeCore.tsx @@ -32,6 +32,7 @@ type BaseThreadAdapters = { export abstract class BaseThreadRuntimeCore implements ThreadRuntimeCore { private _subscriptions = new Set<() => void>(); + private _isInitialized = false; protected readonly repository = new MessageRepository(); public abstract get adapters(): BaseThreadAdapters | undefined; @@ -161,11 +162,20 @@ export abstract class BaseThreadRuntimeCore implements ThreadRuntimeCore { this._notifySubscribers(); } + protected ensureInitialized() { + if (!this._isInitialized) { + this._isInitialized = true; + this._notifyEventSubscribers("initialize"); + } + } + public export() { return this.repository.export(); } public import(data: ExportedMessageRepository) { + this.ensureInitialized(); + this.repository.import(data); this._notifySubscribers(); } diff --git a/packages/react/src/runtimes/external-store/ExternalStoreThreadRuntimeCore.tsx b/packages/react/src/runtimes/external-store/ExternalStoreThreadRuntimeCore.tsx index 6bf1a32138..92152c138b 100644 --- a/packages/react/src/runtimes/external-store/ExternalStoreThreadRuntimeCore.tsx +++ b/packages/react/src/runtimes/external-store/ExternalStoreThreadRuntimeCore.tsx @@ -133,8 +133,9 @@ export class ExternalStoreThreadRuntimeCore ) return cache; + const messageLike = store.convertMessage(m, idx); const newMessage = fromThreadMessageLike( - store.convertMessage(m, idx), + messageLike, idx.toString(), autoStatus, ); @@ -142,6 +143,8 @@ export class ExternalStoreThreadRuntimeCore return newMessage; }); + if (messages.length > 0) this.ensureInitialized(); + for (let i = 0; i < messages.length; i++) { const message = messages[i]!; const parent = messages[i - 1]; diff --git a/packages/react/src/runtimes/local/LocalThreadRuntimeCore.tsx b/packages/react/src/runtimes/local/LocalThreadRuntimeCore.tsx index 3a5d6ada43..572c9b7c90 100644 --- a/packages/react/src/runtimes/local/LocalThreadRuntimeCore.tsx +++ b/packages/react/src/runtimes/local/LocalThreadRuntimeCore.tsx @@ -19,8 +19,6 @@ export class LocalThreadRuntimeCore extends BaseThreadRuntimeCore implements ThreadRuntimeCore { - private _isInitialized = false; - public readonly capabilities = { switchToBranch: true, edit: true, @@ -83,15 +81,8 @@ export class LocalThreadRuntimeCore if (hasUpdates) this._notifySubscribers(); } - private _ensureInitialized() { - if (!this._isInitialized) { - this._isInitialized = true; - this._notifyEventSubscribers("initialize"); - } - } - public async append(message: AppendMessage): Promise { - this._ensureInitialized(); + this.ensureInitialized(); const newMessage = fromCoreMessage(message, { attachments: message.attachments, @@ -108,7 +99,7 @@ export class LocalThreadRuntimeCore } public async startRun(parentId: string | null): Promise { - this._ensureInitialized(); + this.ensureInitialized(); this.repository.resetHead(parentId);