docs: 逐模块细化中文逻辑注释并覆盖测试与示例
This commit is contained in:
+23
-2
@@ -18,15 +18,20 @@ use crate::{block, event_dispatch::Subscription, ui_bridge::attach_desktop_type}
|
||||
#[derive(Debug, Clone)]
|
||||
/// 上次绑定计算的读取观察,用于判断是否需要重新执行源计算。
|
||||
struct Cached {
|
||||
// 每项保存读取对象/属性的版本观察;刷新时向对象存储核对,而非复制业务值。
|
||||
reads: Vec<endor_core::ReadDependency>,
|
||||
// 只借用上次结果的身份做相等比较;不能解引用其中可能已被回收的对象。
|
||||
output: Value,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
/// 托管属性连接,强持有计算/目标所需引用,并保留只读计算的版本缓存。
|
||||
pub struct Binding {
|
||||
// 目标属性名;目标 Block 通过 bindings 列表拥有本连接。
|
||||
field: String,
|
||||
// Some 表示连接有效且拥有计算函数;取消/替换后置 None,让 trace 停止保活函数。
|
||||
pub compute: Option<ObjectRef>,
|
||||
// payload 克隆共享同一缓存,刷新局部副本也能更新缓存;首次计算前为 None。
|
||||
reads: Arc<Mutex<Option<Cached>>>,
|
||||
}
|
||||
impl ObjectPayload for Binding {
|
||||
@@ -58,12 +63,13 @@ pub fn connect(
|
||||
compute: ObjectRef,
|
||||
desktop: ObjectRef,
|
||||
) -> Result<ObjectRef, VmError> {
|
||||
// 在触碰连接列表前拒绝非法名称,尤其禁止绑定运行时类型属性。
|
||||
if field.is_empty() || field.len() > 128 || field == "type" {
|
||||
return Err(VmError::InvalidArgument("invalid binding property"));
|
||||
}
|
||||
let (reference, data) = block::resolve(cx.store(), target)?;
|
||||
let mut data = data.clone();
|
||||
// Only actual fields can be bound; method names are not writable targets.
|
||||
// 只允许属性存储里已存在的字段;方法名即使可读取,也不能作为绑定写入目标。
|
||||
let properties = data
|
||||
.properties
|
||||
.ok_or(VmError::InvalidArgument("missing property storage"))?;
|
||||
@@ -73,6 +79,7 @@ pub fn connect(
|
||||
"binding compute must be a Function",
|
||||
));
|
||||
}
|
||||
// 刷新统一以零参数调用 compute,因此必须有参数元数据并且接受零实参。
|
||||
if !cx
|
||||
.store()
|
||||
.call_arity(compute)?
|
||||
@@ -85,6 +92,8 @@ pub fn connect(
|
||||
"binding compute requires zero unbound parameters",
|
||||
));
|
||||
}
|
||||
// 同一字段只保留一个有效计算。其他字段连接沿用;旧同字段连接失效并清缓存,
|
||||
// 使外部仍持有的 Subscription 不会继续驱动被替换的计算。
|
||||
let mut retained = vec![];
|
||||
for connection in &data.bindings {
|
||||
let mut binding = cx
|
||||
@@ -112,11 +121,12 @@ pub fn connect(
|
||||
})?;
|
||||
retained.push(connection);
|
||||
data.bindings = retained;
|
||||
// Generic text bindings replace the legacy textOf derivation.
|
||||
// text 改由通用绑定负责后,清除旧 textOf 推导入口,避免两套机制竞争同一属性。
|
||||
if field == "text" {
|
||||
data.text_binding = None;
|
||||
}
|
||||
cx.replace_payload(reference, data)?;
|
||||
// 返回的取消令牌仅弱引用连接;连接生命期由目标 Block 的 bindings 决定。
|
||||
let token = cx.allocate(Subscription {
|
||||
connection: cx.store().downgrade(connection)?,
|
||||
})?;
|
||||
@@ -130,14 +140,17 @@ pub fn refresh(
|
||||
nodes: &[WeakObjectRef],
|
||||
host_desktops: &HashSet<ObjectRef>,
|
||||
) -> Result<usize> {
|
||||
// work 跨所有轮次累计。属性 A 的写入可能使已检查的 B 失效,所以不能只扫描一次。
|
||||
let mut work = 0;
|
||||
loop {
|
||||
let mut evaluated = false;
|
||||
// 每轮按解析后的真实 Block 身份去重,多个包装对象共享数据时只处理一次。
|
||||
let mut seen = HashSet::new();
|
||||
for weak in nodes {
|
||||
let Some(target) = vm.upgrade(*weak)? else {
|
||||
continue;
|
||||
};
|
||||
// 用闭包暂存失败,让下方 release 无论处理成功与否都能释放 upgrade 的强引用。
|
||||
let result: Result<()> = (|| {
|
||||
let (reference, data) = block::resolve(vm.object_store(), target)?;
|
||||
if !seen.insert(reference) {
|
||||
@@ -154,6 +167,8 @@ pub fn refresh(
|
||||
.lock()
|
||||
.map_err(|_| anyhow::anyhow!("binding cache poisoned"))?
|
||||
.clone();
|
||||
// 两项都成立才能跳过:读取依赖版本未变,且目标仍等于上次输出。
|
||||
// 后一项让外部直接改写目标后,绑定仍能恢复其计算值。
|
||||
if cached.as_ref().is_some_and(|cached| {
|
||||
cached
|
||||
.reads
|
||||
@@ -165,11 +180,13 @@ pub fn refresh(
|
||||
}) {
|
||||
continue;
|
||||
}
|
||||
// 防止相互写入的绑定持续振荡;预算跨轮累计,超限向宿主报告不稳定。
|
||||
work += 1;
|
||||
if work > 10_000 {
|
||||
bail!("property bindings did not stabilize within the execution budget");
|
||||
}
|
||||
evaluated = true;
|
||||
// 计算阶段禁止写入,精确记录本次实际读取;单次调用另设帧数与指令上限。
|
||||
let (value, reads) = vm
|
||||
.call_read_only_precise_with_limits(
|
||||
compute,
|
||||
@@ -181,12 +198,14 @@ pub fn refresh(
|
||||
)
|
||||
.map_err(|error| crate::endor_host::runtime_error(vm, error))?;
|
||||
let applied: Result<()> = (|| {
|
||||
// 宿主桌面对象不纳入此缓存依赖;保留业务读取,避免宿主发布状态驱动重算。
|
||||
let reads = reads
|
||||
.dependencies
|
||||
.into_iter()
|
||||
.filter(|read| !host_desktops.contains(&read.object()))
|
||||
.collect();
|
||||
let (_, data) = block::resolve(vm.object_store(), target)?;
|
||||
// 先在数据副本上验证属性类型/范围,验证通过后才写回托管属性。
|
||||
block::apply_visual_property(&mut data.clone(), &binding.field, &value)?;
|
||||
if vm.get_attr(target, &binding.field)? != value {
|
||||
vm.set_attr(target, &binding.field, value.clone())?;
|
||||
@@ -201,6 +220,7 @@ pub fn refresh(
|
||||
});
|
||||
Ok(())
|
||||
})();
|
||||
// 缓存不拥有输出对象;释放调用返回值后再传播应用错误,避免失败路径漏引用。
|
||||
vm.release_value(value)?;
|
||||
applied?;
|
||||
}
|
||||
@@ -209,6 +229,7 @@ pub fn refresh(
|
||||
vm.release(target)?;
|
||||
result?;
|
||||
}
|
||||
// 整轮无需计算才达到固定点;即使某次计算输出未变,也再扫描一轮确认依赖有效。
|
||||
if !evaluated {
|
||||
return Ok(work);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user