Skip to content

Commit

Permalink
fix: external store initialMessages sync (#631)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yonom authored Aug 4, 2024
1 parent 07f76c8 commit a7e8ef6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/poor-brooms-fetch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@assistant-ui/react": patch
---

refactor: rewrite external store sync
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,30 @@ export class ExternalStoreThreadRuntime implements ReactThreadRuntime {
public isRunning = false;
public converter = new ThreadMessageConverter();

private _store;
private _store!: ExternalStoreAdapter<any>;

constructor(store: ExternalStoreAdapter<any>) {
this._store = store;
this.store = store;
}

public set store(store: ExternalStoreAdapter<any>) {
const oldStore = this._store;
this._store = store;
const oldStore = this._store as ExternalStoreAdapter<any> | undefined;

// flush the converter cache when the convertMessage prop changes
if (oldStore.convertMessage !== store.convertMessage) {
this.converter = new ThreadMessageConverter();
} else if (
oldStore.isDisabled === store.isDisabled &&
oldStore.isRunning === store.isRunning &&
oldStore.messages === store.messages
) {
// no update needed
return;
if (oldStore) {
if (oldStore.convertMessage !== store.convertMessage) {
this.converter = new ThreadMessageConverter();
} else if (
oldStore.isDisabled === store.isDisabled &&
oldStore.isRunning === store.isRunning &&
oldStore.messages === store.messages
) {
// no update needed
return;
}
}

this._store = store;
const isRunning = store.isRunning ?? false;
const isDisabled = store.isDisabled ?? false;

Expand Down Expand Up @@ -122,7 +124,6 @@ export class ExternalStoreThreadRuntime implements ReactThreadRuntime {
this.messages = this.repository.getMessages();
this.isDisabled = isDisabled;
this.isRunning = isRunning;

for (const callback of this._subscriptions) callback();
}

Expand Down

0 comments on commit a7e8ef6

Please sign in to comment.