import EventManager from '../../../../manager/eventManager';
import topicListener from '../../common/topicListener';
import TOPICS from '../../const/topic';

const { LIFE_LOAD } = TOPICS;

/**
 * 生命周期
 */
class AppState {
  constructor() {
    this.eventer = new EventManager();

    this._loaded = false;
    topicListener.on(LIFE_LOAD, data => {
      this._loaded = true;
      this.eventer.emit(LIFE_LOAD, data);
    });
  }

  onLoad(handler) {
    if (this._loaded) {
      return setTimeout(() => handler(), 0);
    }
    return this.eventer.on(LIFE_LOAD, handler);
  }
}

export default new AppState();