1
0
mirror of synced 2025-11-06 04:21:02 +08:00

Compare commits

...

3 Commits

Author SHA1 Message Date
roymondchen
abab44ad24 chore: update deps 2025-09-16 19:49:48 +08:00
roymondchen
849b561933 feat(data-source): 数据源数据变化事件监听响应支持立即执行 2025-09-16 19:44:16 +08:00
roymondchen
1031595a97 fix(core): 异步加载数据源时,数据源事件配置失效 2025-09-16 19:34:15 +08:00
8 changed files with 310 additions and 257 deletions

View File

@@ -55,7 +55,7 @@
"cz-conventional-changelog": "^3.3.0",
"element-plus": "^2.9.11",
"enquirer": "^2.4.1",
"eslint": "^9.34.0",
"eslint": "^9.35.0",
"execa": "^4.1.0",
"highlight.js": "^11.11.1",
"husky": "^9.1.7",
@@ -76,7 +76,7 @@
"vitepress": "^1.6.4",
"vitest": "^3.2.4",
"vue": "catalog:",
"vue-tsc": "^3.0.6"
"vue-tsc": "^3.0.7"
},
"config": {
"commitizen": {

View File

@@ -167,8 +167,13 @@ class App extends EventEmitter {
this.setPage(pageId);
if (this.dataSourceManager) {
const dataSourceList = Array.from(this.dataSourceManager.dataSourceMap.values());
this.eventHelper?.bindDataSourceEvents(dataSourceList);
if (this.dataSourceManager.isAllDataSourceRegistered()) {
this.eventHelper?.bindDataSourceEvents();
} else {
this.dataSourceManager.once('registered-all', () => {
this.eventHelper?.bindDataSourceEvents();
});
}
}
}

View File

@@ -113,21 +113,23 @@ export default class EventHelper extends EventEmitter {
}
public removeNodeEvents() {
Array.from(this.nodeEventList.keys()).forEach((handler) => {
for (const handler of Array.from(this.nodeEventList.keys())) {
const name = this.nodeEventList.get(handler);
name && this.off(name, handler);
});
}
this.nodeEventList.clear();
}
public bindDataSourceEvents(dataSourceList: DataSource[]) {
public bindDataSourceEvents() {
const dataSourceList = Array.from(this.app.dataSourceManager?.dataSourceMap.values() || []);
this.removeDataSourceEvents(dataSourceList);
dataSourceList.forEach((dataSource) => {
for (const dataSource of dataSourceList) {
const dataSourceEvent = this.dataSourceEventList.get(dataSource.id) ?? new Map<string, (args: any) => void>();
(dataSource.schema.events || []).forEach((event) => {
for (const event of dataSource.schema.events || []) {
const [prefix, ...path] = event.name?.split('.') || [];
if (!prefix) return;
const handler = (...args: any[]) => {
@@ -141,9 +143,9 @@ export default class EventHelper extends EventEmitter {
// 数据源自定义事件
dataSource.on(prefix, handler);
}
});
}
this.dataSourceEventList.set(dataSource.id, dataSourceEvent);
});
}
}
public removeDataSourceEvents(dataSourceList: DataSource[]) {
@@ -152,20 +154,20 @@ export default class EventHelper extends EventEmitter {
}
// 先清掉之前注册的事件,重新注册
dataSourceList.forEach((dataSource) => {
for (const dataSource of dataSourceList) {
const dataSourceEvent = this.dataSourceEventList.get(dataSource.id)!;
if (!dataSourceEvent) return;
Array.from(dataSourceEvent.keys()).forEach((eventName) => {
for (const eventName of Array.from(dataSourceEvent.keys())) {
const [prefix, ...path] = eventName.split('.');
if (prefix === DATA_SOURCE_FIELDS_CHANGE_EVENT_PREFIX) {
dataSource.offDataChange(path.join('.'), dataSourceEvent.get(eventName)!);
} else {
dataSource.off(prefix, dataSourceEvent.get(eventName)!);
}
});
});
}
}
this.dataSourceEventList.clear();
}

View File

@@ -23,9 +23,9 @@ export class DeepObservedData extends ObservedData {
update = (data: any, path?: string) => {
this.state?.update(path ?? '', data);
};
on = (path: string, callback: (newVal: any) => void) => {
on = (path: string, callback: (newVal: any) => void, options?: { immediate?: boolean }) => {
// subscribe 会立即执行一次ignoreFirstCall 会忽略第一次执行
const unsubscribe = this.state!.subscribe(path, ignoreFirstCall(callback));
const unsubscribe = this.state!.subscribe(path, options?.immediate ? callback : ignoreFirstCall(callback));
// 把取消监听的函数保存下来,供 off 时调用
const pathSubscribers = this.subscribers.get(path) ?? new Map<Function, () => void>();

View File

@@ -1,7 +1,7 @@
export abstract class ObservedData {
abstract update(data: any, path?: string): void;
abstract on(path: string, callback: (newVal: any) => void): void;
abstract on(path: string, callback: (newVal: any) => void, options?: { immediate?: boolean }): void;
abstract off(path: string, callback: (newVal: any) => void): void;

View File

@@ -32,7 +32,10 @@ export class SimpleObservedData extends ObservedData {
this.event.emit('', changeEvent);
}
on(path: string, callback: (newVal: any) => void): void {
on(path: string, callback: (newVal: any) => void, options?: { immediate?: boolean }): void {
if (options?.immediate) {
callback(this.getData(path));
}
this.event.on(path, callback);
}

515
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

View File

@@ -7,8 +7,8 @@ packages:
- 'eslint-config'
catalog:
vue: ^3.5.20
vue: ^3.5.21
'@vue/compiler-sfc': ^3.5.20
vite: ^7.1.3
vite: ^7.1.5
typescript: "^5.9.2"