import { Subject, Subscription } from 'rxjs'; /** * 使用RxJS Subject创建一个实例 * * ```ts * import { Subject } from 'rxjs' * import { Channel } from 'sunny-js' * const _c = Channel.createChannel>(Subject) * _c.subscribe(console.log) * _c.next('发送消息') * ``` * * @see {@link shareChannel}创建共享实例 */ export declare function createChannel>(ChanelClass: new () => TSubject): TSubject; /** * 创建一个共享的单实例的频道 * * ```ts * import { Subject } from 'rxjs' * import { Channel } from 'sunny-js' * const _c1 = Channel.shareChannel>(Subject) * _c1.subscribe(console.log) * const _c2 = Channel.shareChannel>() * _c2.subscribe(console.log) * // _c1, _c2都能收到消息 * _c1.next('发送消息') * ``` * * @see {@link createChannel} */ export declare function shareChannel>(ChanelClass?: new () => TSubject): TSubject; /** * 绑定on的channel上下文,简化on的用法 * @see {@link on} * @see {@link communicateWith}返回的send方法 */ export declare function bindOn(channel: Subject>): (event: string, callback: (payload: TValue) => void, key?: string) => ReturnType; export interface ChannelAction { type: string; payload?: TPayload; } /** * 创建用于发送一个事件的消息体数据结构 * @param type 事件名称 * @param payload 事件数据 */ export declare function createAction(type: string, payload?: TPayload): ChannelAction; /** * 订阅频道事件 * * 对subject的subscribe方法封装类似事件绑定接口, * 会通过callback.toString()作为存储函数引用的key来防止重复绑定 * * @param channel 频道实例 * @param event 事件名称 * @param callback 注意在使用Function.prototype.bind绑定的callback函数,要求传入key * @param key 默认以callback.toString()作为值,用来预防重复绑定 * @see {@link bindOn} */ export declare function on(channel: Subject>, event: string, callback: (payload: TValue) => void, key?: string): Subscription; /** * 提供用于广播的listen, send工具函数,简化事件监听和发送的用法 * * ```ts * import { Subject } from 'rxjs' * import { Channel } from 'sunny-js' * const _c = * Channel.createChannel>>(Subject) * const { listen, send } = Channel.communicateWith(_c) * const _subscription = listen('greet', console.log) * send('greet', 'hello') * // 注销监听函数 * _subscription.unsubscribe() * send('greet', 'hi') * ``` * * @see {@link bindOn} * @see {@link createAction} */ export declare function communicateWith(channel: Subject>): { listen: (event: string, callback: (payload: TValue) => void, key?: string) => Subscription; send: (type: string, payload?: TValue) => void; };