mirror of
https://github.com/Tencent/tmagic-editor.git
synced 2025-09-18 19:40:03 +08:00
feat(data-source): 数据源数据变化事件监听响应支持立即执行
This commit is contained in:
parent
1031595a97
commit
849b561933
@ -113,10 +113,10 @@ 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();
|
||||
}
|
||||
@ -126,10 +126,10 @@ export default class EventHelper extends EventEmitter {
|
||||
|
||||
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[]) => {
|
||||
@ -143,9 +143,9 @@ export default class EventHelper extends EventEmitter {
|
||||
// 数据源自定义事件
|
||||
dataSource.on(prefix, handler);
|
||||
}
|
||||
});
|
||||
}
|
||||
this.dataSourceEventList.set(dataSource.id, dataSourceEvent);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public removeDataSourceEvents(dataSourceList: DataSource[]) {
|
||||
@ -154,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();
|
||||
}
|
||||
|
@ -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>();
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user