
import event, { EVENTS } from './event';
import global from './global';

let inited = false;

export default {
  init() {
    if (inited) return;
    const { adapter } = global.platform || {};
    const { error } = adapter || {};
    if (error && error.initListener) {
      error.initListener(this.emit.bind(this));
      inited = true;
    }
  },

  /**
   * 触发全局报错
   * @param {Object} err 
   */
  emit(err) {
    event.emit(EVENTS.ERR_UNC, err);
  },

  /**
   * 监听全局报错
   * @param {Function} fun 
   */
  onError(fun) {
    return event.on(EVENTS.ERR_UNC, fun);
  },

  /**
   * 取消全局报错
   * @param {Function} fun 
   */
  offError(fun) {
    return event.off(EVENTS.ERR_UNC, fun);
  },
}
