feat(sdk-js): add option to manually provide implementation for shared modules (#4042)

This commit is contained in:
David Duong
2025-03-28 14:04:09 +01:00
committed by GitHub
2 changed files with 22 additions and 1 deletions
+17
View File
@@ -109,6 +109,7 @@ class ComponentStore {
const COMPONENT_STORE = new ComponentStore();
const EXT_STORE_SYMBOL = Symbol.for("LGUI_EXT_STORE");
const REQUIRE_SYMBOL = Symbol.for("LGUI_REQUIRE");
const REQUIRE_EXTRA_SYMBOL = Symbol.for("LGUI_REQUIRE_EXTRA");
interface LoadExternalComponentProps
extends Pick<React.HTMLAttributes<HTMLDivElement>, "style" | "className"> {
@@ -197,9 +198,17 @@ declare global {
interface Window {
[EXT_STORE_SYMBOL]: ComponentStore;
[REQUIRE_SYMBOL]: (name: string) => unknown;
[REQUIRE_EXTRA_SYMBOL]: Record<string, unknown>;
}
}
export function experimental_loadShare(name: string, module: unknown) {
if (typeof window === "undefined") return;
window[REQUIRE_EXTRA_SYMBOL] ??= {};
window[REQUIRE_EXTRA_SYMBOL][name] = module;
}
export function bootstrapUiContext() {
if (typeof window === "undefined") {
console.warn(
@@ -224,6 +233,14 @@ export function bootstrapUiContext() {
};
}
if (
window[REQUIRE_EXTRA_SYMBOL] != null &&
typeof window[REQUIRE_EXTRA_SYMBOL] === "object" &&
name in window[REQUIRE_EXTRA_SYMBOL]
) {
return window[REQUIRE_EXTRA_SYMBOL][name];
}
throw new Error(`Unknown module...: ${name}`);
};
}
+5 -1
View File
@@ -1,7 +1,11 @@
import { bootstrapUiContext } from "./client.js";
bootstrapUiContext();
export { useStreamContext, LoadExternalComponent } from "./client.js";
export {
useStreamContext,
LoadExternalComponent,
experimental_loadShare,
} from "./client.js";
export {
uiMessageReducer,
type UIMessage,