
-- isLoading 없이 onClick 에서 yield 만 한다 — 시간이 코드의 한 방향(↓)으로만 흐를 때
-- 멀티 단계 loading · progressive search · optimistic mutation(optimistic | reconciled | rolledBack)
data Status = Idle | Running | Errored | Done
data Event a = Start | Yield a | Finish | Throw Error | Cancel
step :: State a -> Event a -> State a
step s Start = s { status = Running, error = Nothing } -- 상태와 에러 초기화가 한 번에 — 따로 set 하다 어긋날 일이 없다
step s (Yield v) = s { value = v } -- status 는 두고 값만
step s Finish = s { status = Done }
step s (Throw e) = s { status = Errored, error = Just e }
step s Cancel = s { status = Idle } -- 없으면 Running 에 멈춘다
-- 핸들러는 값의 흐름(async generator), 훅은 그걸 접는다
state = foldl step initial (run fn args)
run :: (Signal -> args -> Stream a) -> args -> [Event a]
run fn args = Start : map Yield (untilAborted (fn signal args)) ++ end
where end | aborted = [] -- abort 로 생긴 throw 는 에러가 아니다
| otherwise = [Finish] -- 본문 어디서 throw 하든 Throw e
-- run 은 직전 실행을 abort 하고 다시 시작한다 · unmount 도 abort
-- 재진입 정책은 쓰는 쪽이 고른다
-- 버튼: Running 동안 disabled, yield 한 JSX 가 곧 라벨
-- 검색: run 마다 재시작 — tier 경계(await wait)가 곧 취소 지점이라 debounce 가 필요 없다
searchTiers q = do wait 100; yield [q ++ ":local"]
wait 300; yield [q ++ ":local", q ++ ":remote"]
-- 확인: a 를 치고 150ms 에 ab
shown == [["a:local"], ["ab:local"], ["ab:local", "ab:remote"]] -- a:remote 는 끼어들지 않는다