Skip to content

生命周期

install()捕获原生 History 方法,并让协调器常驻到当前 Document 卸载。安装时 命中 scope 的当前业务 entry 会被转换为:

text
前一 entry  ↔  当前 entry 的 edge  ↔  带私有锚点的当前业务 entry

转换后仍停留在相同 URL,业务 state 字段完整保留,history.length增加 1。后续每次 命中 scope 的业务 pushState()写入:

text
业务 A(锚点) ↔  内部 edge  ↔  业务 B(锚点)

Back 的顺序:

text
B → edge → B → Handler

Forward 不经过 Handler,协调器只透明跨过 edge:

text
A → edge → B

edge 的 popstate不会传给应用。Back 时 Guard 先使用一次 history.go()恢复来源 业务 entry,再调用栈顶 Handler。最后一层放行时使用 history.go(-2)跨过 edge; 最终业务 entry 的 popstate正常传给路由器。

没有 Handler 时,协调器直接跨过 edge。scope 只限制新增 edge;离开 scope 后, 已有 edge 仍能被识别和跨过。Guard停止只同步移除 Handler,不移除监听器、 不恢复原生方法,也不清理 History。

锚点和 edge 在同一私有字段中使用版本化 tuple,并区分 entryedge。锚点记录 token、当前逻辑位置和相邻 edge 的左右位置。普通刷新后,新 Document 的 install()复用该 token 和位置,不执行新的 pushState();因此原 edge 被直接接管, URL、业务字段和 history.length均不变。

受保护 entry 上调用公开 replaceState()时,Guard 先让原生方法处理调用方 state、 URL、结构化克隆和异常,再使用捕获的原生方法恢复锚点。内部 edge 写入绕过公开包装, 不会被误标为业务 entry。

Released under the MIT License.