import { Emitter } from '@dcloudio/uni-runtime' import { $On, $Once, $Off, $Emit, } from '../interface.uts' import { API_$_ON, API_$_ONCE, API_$_OFF, API_$_EMIT, } from '../protocol.uts' interface IUniEventEmitter { on: (eventName: string, callback: Function) => void once: (eventName: string, callback: Function) => void off: (eventName: string, callback: Function) => void emit: (eventName: string, ...args: (Object | undefined | null)[]) => void } const emitter: IUniEventEmitter = new Emitter() as IUniEventEmitter export const $on: $On = defineSyncApi( API_$_ON, (eventName: string, callback: Function) => { emitter.on(eventName, callback) } ) as $On export const $once: $Once = defineSyncApi( API_$_ONCE, (eventName: string, callback: Function) => { emitter.once(eventName, callback) } ) as $Once export const $off: $Off = defineSyncApi( API_$_OFF, (eventName: string, callback: Function) => { emitter.off(eventName, callback) } ) as $Off export const $emit: $Emit = defineSyncApi( API_$_EMIT, (eventName: string, ...args: (Object | undefined | null)[]) => { emitter.emit(eventName, ...args) } ) as $Emit