diff --git a/packages/data-source/src/DataSourceManager.ts b/packages/data-source/src/DataSourceManager.ts index a5666499..689f247e 100644 --- a/packages/data-source/src/DataSourceManager.ts +++ b/packages/data-source/src/DataSourceManager.ts @@ -32,7 +32,7 @@ import { import { SimpleObservedData } from './observed-data/SimpleObservedData'; import { DataSource, HttpDataSource } from './data-sources'; -import { getDeps } from './depsCache'; +import { clearDepsCache, getDeps } from './depsCache'; import type { ChangeEvent, DataSourceManagerData, @@ -263,6 +263,11 @@ class DataSourceManager extends EventEmitter { * @returns {MNode} 编译后的组件dsl */ public compiledNode(n: MNode, sourceId?: Id, deep = false) { + if (deep && this.app.platform === 'editor') { + // 编辑属性不会改变节点 ID,更新配置后需重收集依赖,再供同批迭代项复用。 + clearDepsCache(); + } + if (n[NODE_DISABLE_DATA_SOURCE_KEY]) { return n; } diff --git a/packages/data-source/src/depsCache.ts b/packages/data-source/src/depsCache.ts index d11444a0..ea9d5727 100644 --- a/packages/data-source/src/depsCache.ts +++ b/packages/data-source/src/depsCache.ts @@ -10,6 +10,8 @@ import { const cache = new Map(); +export const clearDepsCache = () => cache.clear(); + export const getDeps = (ds: DataSourceSchema, nodes: MNode[], inEditor: boolean) => { let cacheKey: string; diff --git a/packages/data-source/tests/iterator-bindings.spec.ts b/packages/data-source/tests/iterator-bindings.spec.ts new file mode 100644 index 00000000..571499fb --- /dev/null +++ b/packages/data-source/tests/iterator-bindings.spec.ts @@ -0,0 +1,94 @@ +import { afterEach, describe, expect, test, vi } from 'vitest'; + +import TMagicApp, { type MNode, NodeType, Watcher } from '@tmagic/core'; + +import { DataSourceManager } from '@data-source/index'; + +describe('迭代器编辑后的数据源绑定', () => { + let app: TMagicApp; + let manager: DataSourceManager; + + const createManager = (platform = 'editor') => { + app = new TMagicApp({ + platform, + config: { + type: NodeType.ROOT, + id: 'binding_app', + items: [], + dataSources: [ + { + id: 'binding_ds', + type: 'base', + fields: [ + { + name: 'list', + type: 'array', + fields: [{ name: 'label', type: 'string' }], + defaultValue: [{ label: 'Alice' }], + }, + ], + methods: [], + events: [], + }, + ], + }, + }); + manager = new DataSourceManager({ app }); + }; + + const compileRows = (nodes: MNode[]) => + ['Alice', 'Bob'].map((label) => manager.compliedIteratorItems({ label }, nodes, ['binding_ds', 'list'])); + + afterEach(() => { + manager.destroy(); + app.destroy(); + vi.restoreAllMocks(); + }); + + test('节点 ID 不变时立即编译新增绑定,并在同批迭代项间复用依赖', () => { + createManager(); + const nodes: MNode[] = [{ id: 'new_binding', type: 'text', text: 'Text' }]; + expect(compileRows(nodes).map((row) => row[0].text)).toEqual(['Text', 'Text']); + + nodes[0] = manager.compiledNode({ ...nodes[0], text: '${binding_ds.list.label}' }, undefined, true); + const collect = vi.spyOn(Watcher.prototype, 'collect'); + + expect(compileRows(nodes).map((row) => row[0].text)).toEqual(['Alice', 'Bob']); + expect(collect).toHaveBeenCalledTimes(1); + }); + + test('编辑嵌套节点的新绑定属性时刷新依赖', () => { + createManager(); + const nodes = [ + { + id: 'nested_container', + type: 'container', + items: [{ id: 'nested_text', type: 'text', text: '${binding_ds.list.label}', title: 'Title' }], + }, + ]; + expect(compileRows(nodes).map((row) => row[0].items[0].title)).toEqual(['Title', 'Title']); + + const updated = manager.compiledNode( + { + ...nodes[0], + items: [{ ...nodes[0].items[0], title: '${binding_ds.list.label}' }], + }, + undefined, + true, + ); + + expect(compileRows([updated]).map((row) => row[0].items[0].title)).toEqual(['Alice', 'Bob']); + }); + + test('运行时深度编译保持现有的迭代依赖缓存', () => { + createManager('mobile'); + const nodes = [{ id: 'runtime_binding', type: 'text', text: '${binding_ds.list.label}' }]; + expect(compileRows(nodes).map((row) => row[0].text)).toEqual(['Alice', 'Bob']); + const collect = vi.spyOn(Watcher.prototype, 'collect'); + + manager.compiledNode({ id: 'unrelated', type: 'container', items: [] }, undefined, true); + + expect(compileRows(nodes).map((row) => row[0].text)).toEqual(['Alice', 'Bob']); + expect(collect).not.toHaveBeenCalled(); + }); +});