Skip to content

Commit

Permalink
fix: sync RuntimeSynchronizer (#342)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yonom authored Jun 27, 2024
1 parent 7be2b6c commit 1131680
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
21 changes: 18 additions & 3 deletions packages/react/src/context/providers/ThreadProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import type { FC, PropsWithChildren } from "react";
import { useEffect, useInsertionEffect, useRef, useState } from "react";
import {
useCallback,
useEffect,
useInsertionEffect,
useRef,
useState,
useSyncExternalStore,
} from "react";
import type { ReactThreadRuntime } from "../../runtime/core/ReactThreadRuntime";
import type { ThreadRuntime } from "../../runtime/core/ThreadRuntime";
import type { ThreadContextValue } from "../react/ThreadContext";
Expand Down Expand Up @@ -52,8 +59,16 @@ export const ThreadProvider: FC<PropsWithChildren<ThreadProviderProps>> = ({
return runtime.subscribe(onRuntimeUpdate);
}, [context, runtime]);

const RuntimeSynchronizer = (runtime as ReactThreadRuntime)
.unstable_synchronizer;
const subscribe = useCallback(
(c: () => void) => runtime.subscribe(c),
[runtime],
);

const RuntimeSynchronizer = useSyncExternalStore(
subscribe,
() => (runtime as ReactThreadRuntime).unstable_synchronizer,
() => undefined,
);

return (
<ThreadContext.Provider value={context}>
Expand Down
1 change: 1 addition & 0 deletions packages/react/src/runtime/core/BaseAssistantRuntime.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export abstract class BaseAssistantRuntime<
this._unsubscribe();
this._thread = thread;
this._unsubscribe = this._thread.subscribe(this.subscriptionHandler);
this.subscriptionHandler();
}

public abstract registerModelConfigProvider(
Expand Down

0 comments on commit 1131680

Please sign in to comment.