@augmentcode/ag-redux-toolkit
Version:
Redux + Saga state management for Svelte apps — designed for AI agents. Includes runtime TypeScript source, AI skills, and reference documentation for robust, observable, high-performance state management.
43 lines (42 loc) • 1.28 kB
JavaScript
import { call, delay, race } from "typed-redux-saga";
//#region src/utils/sagas/wrap-async-generator.ts
var StreamTimeoutError = class extends Error {
constructor(timeoutMs) {
super(timeoutMs === void 0 ? "Stream timed out" : `Stream timed out after ${timeoutMs}ms`);
this.name = "StreamTimeoutError";
}
};
var normalizeOptions = (options) => {
return typeof options === "number" ? { timeoutMs: options } : options ?? {};
};
function* wrapStreamingGenerator(generator, onEvent, options) {
const { timeoutMs, onError } = normalizeOptions(options);
const next = () => generator.next();
function* streamLoop() {
let result = yield* call(next);
while (result.done === false) {
yield* onEvent(result.value);
result = yield* call(next);
}
if (result.value !== void 0 && result.value !== null) yield* onEvent(result.value);
}
try {
if (timeoutMs === void 0) {
yield* streamLoop();
return;
}
const { timedOut } = yield* race({
stream: call(streamLoop),
timedOut: delay(timeoutMs)
});
if (timedOut) throw new StreamTimeoutError(timeoutMs);
} catch (error) {
try {
onError?.(error);
} catch {}
throw error;
}
}
//#endregion
export { StreamTimeoutError, wrapStreamingGenerator };
//# sourceMappingURL=wrap-async-generator.js.map