quad/.claude/audit/handtrace-round10-reference-impl/spikes/t11_effect_deps.luau
qwreey d2d67c7aeb
qa: 10라운드 광범위 탐사 완료 — 발견 H-150~H-157 (🔴 0 / 🟡 5 / 🟢 3), §4 배치 문항 7건 회신 대기
- 신선한 탐사자(fable) 단일 컨텍스트, 지시서 -round10-brief.md §2 레인 A·C 완료, B 부분, D ALL PASS
- audit/handtrace-round10-reference-impl/: round7 참조 구현을 현재 계약으로 갱신·재실행
  (부수: round7/ref9 _recompute 첫 인자 오류 발견·정정)
- base/·인덱스 레이어는 미변경 — 결정은 사용자 배치 회신 뒤 -round10-followup.md로

Co-authored-by: qwreey <me@qwreey.moe>
Claude-Session: https://claude.ai/code/session_01546hjsYNLSMZdHdPyTZaGb
2026-08-28 01:03:05 +09:00

42 lines
2 KiB
Text

--!nocheck
local q = require("./core9")
local inst = q.newInst("inst")
-- (1) Ref dep: onRefFire → fire(ref) → Update(ref) → Rerun
local r = q.Ref(nil)
local runs, cleanups = 0, 0
local e = q.Effect(function(self) runs += 1; return function() cleanups += 1 end end, r)
print("생성 직후 runs", runs, "(기대 1)")
r:Set(1) -- 아직 바인드 안 됨 → canExecute 거짓 → 무시
print("미바인드 Set 후 runs", runs, "(기대 1)")
q.bindLifetime(inst, e) -- 캐치업: Refresh()가 r의 리비전 변화를 봄 → Rerun
print("바인드 캐치업 후 runs", runs, "(기대 2) cleanups", cleanups, "(기대 1)")
r:Set(2)
print("바인드 뒤 Set 후 runs", runs, "(기대 3)")
r:Set(2) -- 같은 값이라도 Set은 새 리비전 → 한 번 더
print("같은 값 Set 후 runs", runs, "(기대 4)")
-- (2) 다이아몬드: A → b, A → c, Effect(fn, b, c) — A:Set 한 번에 fn 한 번
local A = q.Source(1)
local b = A:Compute(function(_) return A:Get() + 1 end)
local c = A:Compute(function(_) return A:Get() * 2 end)
local n = 0
local e2 = q.Effect(function() n += 1 end, b, c)
q.bindLifetime(inst, e2)
A:Set(5)
print("다이아몬드 A:Set 후 n", n, "(기대 2 = 설치 1 + 파동 1)")
-- (3) 혼합 deps: Ref + State 한 Effect에서, 각각 독립 발화
local m = 0
local e3 = q.Effect(function() m += 1 end, r, b)
q.bindLifetime(inst, e3)
r:Set(3); A:Set(6)
print("혼합 deps r:Set + A:Set 후 m", m, "(기대 3)")
-- (4) 무인자 state:Observer() 내부 콜백이 3-인자 루프에서 살아남는가
local s = q.Source(0)
local o = s:Observer() -- function(targetState) targetState:Get() end
o:Subscribe()
local ok, err = pcall(function() s:Set(1) end)
print("무인자 Observer 전파:", if ok then "OK" else "ERROR " .. tostring(err))
-- (5) leaf 죽음 → cleanup 1회, 이후 Ref:Set 무시
q.destroyInst(inst)
print("destroy 후 cleanups", cleanups, "(기대: 직전 + 1)")
local before = runs; r:Set(9)
print("죽은 뒤 Set → runs 변화", runs - before, "(기대 0)")