refactor: 清掉指向不存在的 Dispatch 的文档,删掉为它留的死导出

Dispatch 在仓库里出现 7 次,全是注释和错误文案,没有任何实现。三处错误文案
写着「需要回调语义请用 Dispatch」——使用者按这句去查会找不到东西,真正该指的
是 WithCall。caller.go 那句还指向不存在的子包 jscriptx/dispatch。

连带删掉四个为它留的导出(全仓库零调用):

  Target                   统一 Script/Instance 的接口。有未导出方法 owner(),
                           外部实现不了;也没有任何函数以它为参数或返回值
  ErrUnsupportedSignature  哨兵错误,库自己从不产生它
  KindSignature            错误分类,全仓库唯一一次出现就是它自己的声明。
                           留着会让写 switch 的人为一个永不出现的分支写代码
  OverlayLoader.Loaders    零调用的 getter,连测试都没有

另外删掉 Instance.IdleFor 和 lastUsed 字段:它是给「空闲回收」用的,而
doc.go 明确写着本库不代管实例生命周期、没有空闲回收——字段注释和包文档直接
对立。代价是每次 Call 白付两次 time.Now() + atomic store。业务侧真要自己回收,
记一个时间戳是一行的事。

caller 的示例原来拿 ErrUnsupportedSignature 当哨兵,改成自己声明一个——
回调签名的约定本来就是调用方定的,哨兵该归调用方。

验证:framework-v2 和 lx-bid 都仍能编译。
This commit is contained in:
2026-09-10 15:15:10 +08:00
parent ad722b12f9
commit 9b3509ae29
7 changed files with 28 additions and 59 deletions
+2 -13
View File
@@ -4,7 +4,6 @@ import (
"context"
"sync"
"sync/atomic"
"time"
)
// Instance 是脚本导出的 class 的一个实例,独占一个 VM,用起来跟普通 Go 对象差不多:
@@ -40,9 +39,8 @@ type Instance struct {
vm *vmHandle
done bool
lastUsed atomic.Int64 // UnixNano,空闲回收用
calls atomic.Int64
resets atomic.Int64
calls atomic.Int64
resets atomic.Int64
}
// New 实例化脚本导出的 class,构造参数直接传给 constructor。返回的实例独占一个 VM,
@@ -63,7 +61,6 @@ func (s *Script) New(ctx context.Context, ctorArgs ...any) (*Instance, error) {
i.scope = sc
i.label = "作用域 " + strconvQuote(sc.key) + " 的实例"
}
i.touch()
if err := i.warmup(ctx); err != nil {
return nil, err
}
@@ -79,11 +76,6 @@ func (i *Instance) Calls() int64 { return i.calls.Load() }
// Resets 返回这个实例的 VM 被重建过几次。每重建一次,脚本里 this 上的状态就归零一次。
func (i *Instance) Resets() int64 { return i.resets.Load() }
// IdleFor 返回这个实例空闲了多久。
func (i *Instance) IdleFor() time.Duration {
return time.Since(time.Unix(0, i.lastUsed.Load()))
}
// Call 在这个实例上调用方法,语义跟 Script.Call 一致,只是 this 绑定到本实例。
func (i *Instance) Call(ctx context.Context, fn string, args ...any) (any, error) {
return callAny(ctx, i, fn, args)
@@ -111,7 +103,6 @@ func (i *Instance) Has(fn string) bool {
func (i *Instance) Close() { i.shutdown() }
func (i *Instance) owner() *Script { return i.script }
func (i *Instance) touch() { i.lastUsed.Store(time.Now().UnixNano()) }
// warmup 提前建好 VM,让 constructor 的错误在 New 阶段就暴露出来。
func (i *Instance) warmup(ctx context.Context) error {
@@ -149,7 +140,6 @@ func (i *Instance) acquire(ctx context.Context) (*vmHandle, error) {
i.mu.Unlock()
return nil, err
}
i.touch()
i.calls.Add(1)
return i.vm, nil
}
@@ -161,7 +151,6 @@ func (i *Instance) finish(_ *vmHandle, healthy bool) {
i.vm = nil
i.resets.Add(1)
}
i.touch()
i.mu.Unlock()
}