28 lines
738 B
TypeScript
28 lines
738 B
TypeScript
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())
|
|
}
|