Skip to content

Commit

Permalink
feat(event): add event.prependListener api
Browse files Browse the repository at this point in the history
  • Loading branch information
liujuping committed Aug 16, 2023
1 parent 942972c commit ffe97cd
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
13 changes: 13 additions & 0 deletions docs/docs/api/event.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@ on(event: string, listener: (...args: any[]) => void): IPublicTypeDisposable;
```
相关类型:[IPublicTypeDisposable](https://github.com/alibaba/lowcode-engine/blob/main/packages/types/src/shell/type/disposable.ts)

### prependListener
监听事件,会在其他回调函数之前执行

```typescript
/**
* 监听事件,会在其他回调函数之前执行
* @param event 事件名称
* @param listener 事件回调
*/
prependListener(event: string, listener: (...args: any[]) => void): IPublicTypeDisposable;
```
相关类型:[IPublicTypeDisposable](https://github.com/alibaba/lowcode-engine/blob/main/packages/types/src/shell/type/disposable.ts)

### off
取消监听事件

Expand Down
8 changes: 8 additions & 0 deletions packages/editor-core/src/event-bus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ export class EventBus implements IEventBus {
};
}

prependListener(event: string, listener: (...args: any[]) => void): () => void {
this.eventEmitter.prependListener(event, listener);
this.getLogger().debug(`${this.getMsgPrefix('prependListener')} ${event}`);
return () => {
this.off(event, listener);
};
}

/**
* 取消监听事件
* @param event 事件名称
Expand Down
14 changes: 14 additions & 0 deletions packages/shell/src/api/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@ export class Event implements IPublicApiEvent {
}
}

/**
* 监听事件,会在其他回调函数之前执行
* @param event 事件名称
* @param listener 事件回调
*/
prependListener(event: string, listener: (...args: any[]) => void): IPublicTypeDisposable {
if (isPluginEventName(event)) {
return this[eventBusSymbol].prependListener(event, listener);
} else {
logger.warn(`fail to prependListener event ${event}, event should have a prefix like 'somePrefix:eventName'`);
return () => {};
}
}

/**
* 取消监听事件
* @param event 事件名称
Expand Down
7 changes: 7 additions & 0 deletions packages/types/src/shell/api/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ export interface IPublicApiEvent {
*/
on(event: string, listener: (...args: any[]) => void): IPublicTypeDisposable;

/**
* 监听事件,会在其他回调函数之前执行
* add monitor to a event
* @param event 事件名称
* @param listener 事件回调
*/
prependListener(event: string, listener: (...args: any[]) => void): IPublicTypeDisposable;

/**
* 取消监听事件
Expand Down

0 comments on commit ffe97cd

Please sign in to comment.