{"version":3,"names":["useStalled","time","getLength","opts","delay","delayMs","ramp","rampMs","lastLength","lastChangeTime","smoothIntensity","lastSmoothTime","isStalled","t","len","intensity","idle","target","Math","min","dt","diff","abs"],"sources":["use-stalled.ts"],"sourcesContent":["import type { Accessor } from \"solid-js\";\n\n/**\n * Tracks whether a streaming response has stalled (no new tokens for a while)\n * and produces a smooth 0→1 intensity value suitable for color interpolation\n * (e.g. fading the spinner glyph toward red).\n *\n * Driven by an external clock signal rather than its own interval, so it\n * automatically slows down when the terminal is backgrounded (no ticks = no\n * updates) and doesn't create a second timer alongside the animation clock.\n *\n * ```ts\n * const time = useClock(50);\n * const stalled = useStalled(time, () => responseLength());\n * // stalled.isStalled()  — boolean\n * // stalled.intensity()  — 0..1\n * ```\n */\n\nexport type StalledState = {\n\tisStalled: Accessor<boolean>;\n\tintensity: Accessor<number>;\n};\n\nexport function useStalled(\n\ttime: Accessor<number>,\n\tgetLength: Accessor<number>,\n\topts?: { delayMs?: number; rampMs?: number },\n): StalledState {\n\tconst delay = opts?.delayMs ?? 3000;\n\tconst ramp = opts?.rampMs ?? 2000;\n\n\tlet lastLength = getLength();\n\tlet lastChangeTime = 0;\n\tlet smoothIntensity = 0;\n\tlet lastSmoothTime = 0;\n\n\tconst isStalled = (): boolean => {\n\t\tconst t = time(); // subscribe to clock\n\t\tconst len = getLength();\n\n\t\tif (len > lastLength) {\n\t\t\tlastLength = len;\n\t\t\tlastChangeTime = t;\n\t\t}\n\n\t\treturn t - lastChangeTime > delay;\n\t};\n\n\tconst intensity = (): number => {\n\t\tconst t = time(); // subscribe to clock\n\t\tconst len = getLength();\n\n\t\tif (len > lastLength) {\n\t\t\tlastLength = len;\n\t\t\tlastChangeTime = t;\n\t\t\tsmoothIntensity = 0;\n\t\t\tlastSmoothTime = t;\n\t\t\treturn 0;\n\t\t}\n\n\t\tconst idle = t - lastChangeTime;\n\t\tconst target = idle > delay ? Math.min((idle - delay) / ramp, 1) : 0;\n\n\t\t// Smooth approach — step at ~50ms granularity\n\t\tconst dt = t - lastSmoothTime;\n\t\tif (dt >= 50 && (target > 0 || smoothIntensity > 0)) {\n\t\t\tconst diff = target - smoothIntensity;\n\t\t\tif (Math.abs(diff) < 0.01) {\n\t\t\t\tsmoothIntensity = target;\n\t\t\t} else {\n\t\t\t\tsmoothIntensity += diff * 0.1;\n\t\t\t}\n\t\t\tlastSmoothTime = t;\n\t\t}\n\n\t\treturn smoothIntensity;\n\t};\n\n\treturn { isStalled, intensity };\n}\n"],"mappings":"AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAOA,OAAO,SAASA,UAAUA,CACzBC,IAAsB,EACtBC,SAA2B,EAC3BC,IAA4C,EAC7B;EACf,MAAMC,KAAK,GAAGD,IAAI,EAAEE,OAAO,IAAI,IAAI;EACnC,MAAMC,IAAI,GAAGH,IAAI,EAAEI,MAAM,IAAI,IAAI;EAEjC,IAAIC,UAAU,GAAGN,SAAS,CAAC,CAAC;EAC5B,IAAIO,cAAc,GAAG,CAAC;EACtB,IAAIC,eAAe,GAAG,CAAC;EACvB,IAAIC,cAAc,GAAG,CAAC;EAEtB,MAAMC,SAAS,GAAGA,CAAA,KAAe;IAChC,MAAMC,CAAC,GAAGZ,IAAI,CAAC,CAAC,CAAC,CAAC;IAClB,MAAMa,GAAG,GAAGZ,SAAS,CAAC,CAAC;IAEvB,IAAIY,GAAG,GAAGN,UAAU,EAAE;MACrBA,UAAU,GAAGM,GAAG;MAChBL,cAAc,GAAGI,CAAC;IACnB;IAEA,OAAOA,CAAC,GAAGJ,cAAc,GAAGL,KAAK;EAClC,CAAC;EAED,MAAMW,SAAS,GAAGA,CAAA,KAAc;IAC/B,MAAMF,CAAC,GAAGZ,IAAI,CAAC,CAAC,CAAC,CAAC;IAClB,MAAMa,GAAG,GAAGZ,SAAS,CAAC,CAAC;IAEvB,IAAIY,GAAG,GAAGN,UAAU,EAAE;MACrBA,UAAU,GAAGM,GA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