Skip to content

定义 effect cleanup 生命周期 / Define the effect cleanup lifecycle - #22

Merged
tiye merged 1 commit into
mainfrom
codex/effect-cleanup-lifecycle
Aug 31, 2026
Merged

定义 effect cleanup 生命周期 / Define the effect cleanup lifecycle#22
tiye merged 1 commit into
mainfrom
codex/effect-cleanup-lifecycle

Conversation

@tiye

@tiye tiye commented Aug 31, 2026

Copy link
Copy Markdown
Member

中文

用户可见结果

  • 保留简洁的 state_effect("name", deps) { ... },用于 focus、flash、日志等无需释放资源的 render 后工作。
  • 新增 state_resource(name = ..., deps = ..., cleanup = ..., action = ...),让 subscription、observer、timer handle 等 setup/cleanup 在严格类型下保持清晰,不要求 callback 返回两种不同形状。
  • Effects demo 增加 lifecycle 示例与可观察 setup/cleanup 日志。

安全与生命周期

  • deps 变化时,旧 cleanup 恰好执行一次并先于新 setup。
  • ordinary component(...) unmount 与 reset_feature(...) 会释放对应 live resources。
  • persistent feature_root(...) 仅因 route 暂时隐藏时保留 snapshot 与 live resource;显式 reset/runtime dispose 才释放。
  • cleanup closure 只保存在 host memory registry,不进入 state tree、snapshot 或 localStorage。
  • HMR 固定先 flush snapshot,再 dispose 旧 runtime,最后 boot 新 runtime;cleanup error 写入 [explore-react effect] 日志且不会阻断后续 setup/boot。

文档

  • 新增 docs/effect-lifecycle.md,完整说明 author API、生命周期顺序、persistent feature policy 与 HMR hand-off。
  • 同步 README、quick start、component author API、component lifecycle、PLAN 与开发指南。

验证

  • yarn test:koka
  • yarn build
  • yarn check
  • 浏览器验证 deps change 的 cleanup → setup 顺序。
  • 浏览器验证 persistent route hide 不 cleanup。
  • 真实 Vite HMR 验证旧 runtime cleanup、typed editor draft snapshot 恢复,以及返回 feature 后新 runtime setup。

关联 issue

Closes #18

English

User-visible result

  • Keeps the concise state_effect("name", deps) { ... } form for focus, flash, logging, and other post-render work with nothing to release.
  • Adds state_resource(name = ..., deps = ..., cleanup = ..., action = ...) so subscriptions, observers, timer handles, and other setup/cleanup lifecycles stay explicit under strict typing without requiring one callback to return two different shapes.
  • Adds an Effects demo lifecycle case with observable setup/cleanup logging.

Safety and lifecycle

  • On dependency changes, the old cleanup runs exactly once and before the new setup.
  • Ordinary component(...) unmount and reset_feature(...) release their corresponding live resources.
  • A persistent feature_root(...) retains both snapshot and live resource while temporarily hidden by routing; explicit reset or runtime disposal releases it.
  • Cleanup closures stay only in the host memory registry and never enter the state tree, snapshot, or localStorage.
  • HMR flushes the snapshot first, disposes the old runtime second, and boots the new runtime last. Cleanup errors are logged through [explore-react effect] and cannot block the following setup or boot.

Documentation

  • Adds docs/effect-lifecycle.md with the complete author API, lifecycle order, persistent-feature policy, and HMR hand-off contract.
  • Synchronizes the README, quick start, component author API, component lifecycle, PLAN, and developer guide.

Verification

  • yarn test:koka
  • yarn build
  • yarn check
  • Browser verification of cleanup → setup ordering on dependency changes.
  • Browser verification that persistent route hiding does not clean up the resource.
  • Real Vite HMR verification of old-runtime cleanup, typed editor-draft snapshot recovery, and new-runtime setup after returning to the feature.

Related issue

Closes #18

@tiye
tiye merged commit 01ea1d9 into main Aug 31, 2026
1 check passed
@tiye
tiye deleted the codex/effect-cleanup-lifecycle branch August 31, 2026 06:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

定义组件 effect cleanup 生命周期 / Define the component effect cleanup lifecycle

1 participant