| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- /** 一个事件能存储多个回调 */
- interface EnentNames {
- loadComplete: string; //加载状态名
- spotTrade: string; // 挂牌成功,通知报价大厅更新数据
- blocsTrade: string; // 贸易圈 挂牌成功
- moneyChangedNtf_UI: string; // /接收到资金变化通知 UI 成 使用
- financing_manager: string; // 融资摘牌
- changeTheme: string; // 切换主题
- }
- /** 一个事件只能存储一个回调 */
- interface EnentNamesOnlyOneValue {
- logout: string; //登出名:确认登出时不能进行其它操作了 暂时1s登出状态
- loginSuccess: string; // 登录成功
- loadAddressList: string; //加载地址列表
- loadMylieList: string; //加载闲置列表
- custOfflineNtf: string; //接收到账户离线通知
- userLogout: string; //接收到用户登出应答
- posChangedNtf: string; //接收到头寸变化通知
- moneyChangedNtf: string; //接收到资金变化通知
- orderCanceledNtf: string; //接收到委托单撤单通知
- quoteReceiveNtf: string; // 接收实时行情推送
- }
- // 事件触发器
- export default new (class Bus {
- // 收集订阅信息,调度中心
- private subscribe: { [key: string]: Array<Function> } = {};
- /** 注册事件(一个事件可以存储多个回调) */
- $on(enentType: keyof EnentNames, hander: Function): void {
- this.subscribe[enentType] = this.subscribe[enentType] || [];
- this.subscribe[enentType].push(hander);
- }
- /** 注册事件(一个事件只能存储一个回调) */
- $onOnly(enentType: keyof EnentNamesOnlyOneValue, hander: Function): void {
- this.subscribe[enentType] = [];
- this.subscribe[enentType].push(hander);
- }
- /** 发布事件 立即执行*/
- $emit(enentType: keyof EnentNames | keyof EnentNamesOnlyOneValue, data?: any): void {
- const sub = this.subscribe[enentType];
- if (sub) {
- sub.forEach((hander: Function) => hander(data));
- }
- }
- // 取消部分订阅
- $off(enentType: keyof EnentNames | keyof EnentNamesOnlyOneValue, hander: Function): void {
- const sub = this.subscribe[enentType];
- if (sub) {
- const findIndex = sub.findIndex((fn) => fn === hander);
- this.subscribe[enentType].splice(findIndex, 1);
- }
- }
- // 取消所有订阅
- $offall(enentType: keyof EnentNames): void {
- const sub = this.subscribe[enentType];
- if (sub) {
- delete this.subscribe[enentType];
- }
- }
- })();
|