@langchain/langgraph-sdk
Version:
Client library for interacting with the LangGraph API
45 lines (44 loc) • 1.14 kB
JavaScript
//#region src/stream/projections/runtime.ts
/**
* Shared async subscription lifecycle for projection runtimes.
*/
function openProjectionSubscription({ thread, channels, namespace, depth = 1, resumeOnPause = false, onSubscribe, onEvent, onFinally }) {
let handle;
let disposed = false;
const start = async () => {
try {
const subscribeParams = {
channels: [...channels],
namespaces: namespace.length > 0 ? [[...namespace]] : [[]],
depth
};
const subscription = await thread.subscribe(subscribeParams);
handle = subscription;
if (disposed) {
await subscription.unsubscribe();
return;
}
onSubscribe?.();
do {
for await (const event of subscription) {
if (disposed) break;
onEvent(event);
}
if (disposed || !resumeOnPause || !subscription.isPaused) break;
await subscription.waitForResume();
} while (!disposed);
} catch {} finally {
onFinally?.();
}
};
start();
return { async dispose() {
disposed = true;
try {
await handle?.unsubscribe();
} catch {}
} };
}
//#endregion
export { openProjectionSubscription };
//# sourceMappingURL=runtime.js.map