Skip to content

Commit

Permalink
fix: initialize thread on import
Browse files Browse the repository at this point in the history
  • Loading branch information
Yonom committed Dec 14, 2024
1 parent fd1623f commit 5d07137
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
10 changes: 10 additions & 0 deletions packages/react/src/runtimes/core/BaseThreadRuntimeCore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,18 @@ export class ExternalStoreThreadRuntimeCore
)
return cache;

const messageLike = store.convertMessage(m, idx);
const newMessage = fromThreadMessageLike(
store.convertMessage(m, idx),
messageLike,
idx.toString(),
autoStatus,
);
(newMessage as any)[symbolInnerMessage] = m;
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];
Expand Down
13 changes: 2 additions & 11 deletions packages/react/src/runtimes/local/LocalThreadRuntimeCore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ export class LocalThreadRuntimeCore
extends BaseThreadRuntimeCore
implements ThreadRuntimeCore
{
private _isInitialized = false;

public readonly capabilities = {
switchToBranch: true,
edit: true,
Expand Down Expand Up @@ -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<void> {
this._ensureInitialized();
this.ensureInitialized();

const newMessage = fromCoreMessage(message, {
attachments: message.attachments,
Expand All @@ -108,7 +99,7 @@ export class LocalThreadRuntimeCore
}

public async startRun(parentId: string | null): Promise<void> {
this._ensureInitialized();
this.ensureInitialized();

this.repository.resetHead(parentId);

Expand Down

0 comments on commit 5d07137

Please sign in to comment.