mirror of
https://github.com/Tencent/tmagic-editor.git
synced 2025-09-19 03:55:50 +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() {
|
public removeNodeEvents() {
|
||||||
Array.from(this.nodeEventList.keys()).forEach((handler) => {
|
for (const handler of Array.from(this.nodeEventList.keys())) {
|
||||||
const name = this.nodeEventList.get(handler);
|
const name = this.nodeEventList.get(handler);
|
||||||
name && this.off(name, handler);
|
name && this.off(name, handler);
|
||||||
});
|
}
|
||||||
|
|
||||||
this.nodeEventList.clear();
|
this.nodeEventList.clear();
|
||||||
}
|
}
|
||||||
@ -126,10 +126,10 @@ export default class EventHelper extends EventEmitter {
|
|||||||
|
|
||||||
this.removeDataSourceEvents(dataSourceList);
|
this.removeDataSourceEvents(dataSourceList);
|
||||||
|
|
||||||
dataSourceList.forEach((dataSource) => {
|
for (const dataSource of dataSourceList) {
|
||||||
const dataSourceEvent = this.dataSourceEventList.get(dataSource.id) ?? new Map<string, (args: any) => void>();
|
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('.') || [];
|
const [prefix, ...path] = event.name?.split('.') || [];
|
||||||
if (!prefix) return;
|
if (!prefix) return;
|
||||||
const handler = (...args: any[]) => {
|
const handler = (...args: any[]) => {
|
||||||
@ -143,9 +143,9 @@ export default class EventHelper extends EventEmitter {
|
|||||||
// 数据源自定义事件
|
// 数据源自定义事件
|
||||||
dataSource.on(prefix, handler);
|
dataSource.on(prefix, handler);
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
this.dataSourceEventList.set(dataSource.id, dataSourceEvent);
|
this.dataSourceEventList.set(dataSource.id, dataSourceEvent);
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public removeDataSourceEvents(dataSourceList: DataSource[]) {
|
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)!;
|
const dataSourceEvent = this.dataSourceEventList.get(dataSource.id)!;
|
||||||
|
|
||||||
if (!dataSourceEvent) return;
|
if (!dataSourceEvent) return;
|
||||||
|
|
||||||
Array.from(dataSourceEvent.keys()).forEach((eventName) => {
|
for (const eventName of Array.from(dataSourceEvent.keys())) {
|
||||||
const [prefix, ...path] = eventName.split('.');
|
const [prefix, ...path] = eventName.split('.');
|
||||||
if (prefix === DATA_SOURCE_FIELDS_CHANGE_EVENT_PREFIX) {
|
if (prefix === DATA_SOURCE_FIELDS_CHANGE_EVENT_PREFIX) {
|
||||||
dataSource.offDataChange(path.join('.'), dataSourceEvent.get(eventName)!);
|
dataSource.offDataChange(path.join('.'), dataSourceEvent.get(eventName)!);
|
||||||
} else {
|
} else {
|
||||||
dataSource.off(prefix, dataSourceEvent.get(eventName)!);
|
dataSource.off(prefix, dataSourceEvent.get(eventName)!);
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
});
|
}
|
||||||
|
|
||||||
this.dataSourceEventList.clear();
|
this.dataSourceEventList.clear();
|
||||||
}
|
}
|
||||||
|
@ -23,9 +23,9 @@ export class DeepObservedData extends ObservedData {
|
|||||||
update = (data: any, path?: string) => {
|
update = (data: any, path?: string) => {
|
||||||
this.state?.update(path ?? '', data);
|
this.state?.update(path ?? '', data);
|
||||||
};
|
};
|
||||||
on = (path: string, callback: (newVal: any) => void) => {
|
on = (path: string, callback: (newVal: any) => void, options?: { immediate?: boolean }) => {
|
||||||
// subscribe 会立即执行一次,ignoreFirstCall 会忽略第一次执行
|
// subscribe 会立即执行一次,ignoreFirstCall 会忽略第一次执行
|
||||||
const unsubscribe = this.state!.subscribe(path, ignoreFirstCall(callback));
|
const unsubscribe = this.state!.subscribe(path, options?.immediate ? callback : ignoreFirstCall(callback));
|
||||||
|
|
||||||
// 把取消监听的函数保存下来,供 off 时调用
|
// 把取消监听的函数保存下来,供 off 时调用
|
||||||
const pathSubscribers = this.subscribers.get(path) ?? new Map<Function, () => void>();
|
const pathSubscribers = this.subscribers.get(path) ?? new Map<Function, () => void>();
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
export abstract class ObservedData {
|
export abstract class ObservedData {
|
||||||
abstract update(data: any, path?: string): void;
|
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;
|
abstract off(path: string, callback: (newVal: any) => void): void;
|
||||||
|
|
||||||
|
@ -32,7 +32,10 @@ export class SimpleObservedData extends ObservedData {
|
|||||||
this.event.emit('', changeEvent);
|
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);
|
this.event.on(path, callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user