保存
This commit is contained in:
27
src/services/windowForm/utils.ts
Normal file
27
src/services/windowForm/utils.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import {
|
||||
EWindowFormState,
|
||||
type IWindowFormEvents,
|
||||
type IWindowFormInstance
|
||||
} from '@/services/windowForm/WindowFormDataManager.ts'
|
||||
import type { IEventBuilder } from '@/events/IEventBuilder.ts'
|
||||
|
||||
/**
|
||||
* 订阅事件,并自动取消订阅
|
||||
* @param win
|
||||
* @param eventBus
|
||||
* @param event
|
||||
* @param handler
|
||||
*/
|
||||
export function safeSubscribe(
|
||||
win: IWindowFormInstance,
|
||||
eventBus: IEventBuilder<IWindowFormEvents>,
|
||||
event: keyof IWindowFormEvents,
|
||||
handler: (...args: any[]) => void
|
||||
) {
|
||||
const unsubscribe = eventBus.subscribe(event, (...args: any[]) => {
|
||||
// 若窗口已销毁则不再执行
|
||||
if (win.state === EWindowFormState.DESTROYED) return
|
||||
handler(...args)
|
||||
})
|
||||
win.subscriptions.push(() => unsubscribe())
|
||||
}
|
||||
Reference in New Issue
Block a user