import { EventBusDemo } from "../message/EventBusDemo";
import { listenerKey,receiveKey } from "./injectAjax";
export class InjectAjaxBus extends EventBusDemo{
  listener(){
    let _this=this;
    window.addEventListener('message', function (e) {  // 监听 message 事件
      const {origin='*',data={}}=e
      //监听处理ajax
      const {key='',act='getList',params={}}=data;
      if(key==receiveKey){
        _this.$emitSuper(act,params)
      }
    })
  }
  /**
   * 提交到上级方便直接处理
   * @param type 
   * @param args 
   */
  async $emitSuper(type: string, ...args: any): Promise<any> {
    return super.$emit(type,...args)
  }
  /**
   * 提交到
   * @param type 
   * @param args 
   */
  async $emit(type: string, ...args: any): Promise<any> {
    parent.postMessage({key:listenerKey,act:type,params:args}, '*')
    return true;
  }
  /**
   * 提交需要获取请求列表
   * @param args 
   */
  async $emitGetList(...args:any): Promise<any>{
    this.$emit('getList',...args)
  }
  /**
   * 提交
   * @param args 
   */
  async $emitOnRequest(...args:any): Promise<any>{
    this.$emit('onRequest',...args)
  }
  /**
   * 提交
   * @param args 
   */
  async $emitOnResponse(...args:any): Promise<any>{
    this.$emit('onResponse',...args)
  }
  /**
   * 获取列表
   * @param callback 
   */
  getList(callback:Function){
    this.$on('getList',callback)
  }
  /**
   * 发起请求时调用
   * @param callback 
   */
  onRequest(callback:Function){
    this.$on('onRequest',callback)
  }
  /**
   * 响应时调用
   * @param callback 
   */
  onResponse(callback:Function){
    this.$on('onResponse',callback)
  }
  
  
}

