mirror of
https://github.com/Tencent/tmagic-editor.git
synced 2026-11-30 01:43:34 +08:00
Merge 201a3d432ac2aa61fbea45539e3c1717d5021087 into d017f435731ce7742f896268963ac1009b190bd5
This commit is contained in:
commit
d23be5b9fe
@ -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;
|
||||
}
|
||||
|
||||
@ -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;
|
||||
|
||||
|
||||
94
packages/data-source/tests/iterator-bindings.spec.ts
Normal file
94
packages/data-source/tests/iterator-bindings.spec.ts
Normal file
@ -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();
|
||||
});
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user